Coverage for src / bluetooth_sig / gatt / characteristics / unread_alert_status.py: 100%
22 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-11 20:14 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-11 20:14 +0000
1"""Unread Alert Status characteristic (0x2A45) implementation.
3Represents the number of unread alerts in a specific category.
4Used by Alert Notification Service (0x1811).
6Based on Bluetooth SIG GATT Specification:
7- Unread Alert Status: 2 bytes (Category ID + Unread Count)
8"""
10from __future__ import annotations
12import msgspec
14from ...types import AlertCategoryID
15from ..context import CharacteristicContext
16from .base import BaseCharacteristic
17from .utils import DataParser
20class UnreadAlertStatusData(msgspec.Struct):
21 """Unread Alert Status characteristic data structure."""
23 category_id: AlertCategoryID
24 unread_count: int # 0-254, 255 means >254
27class UnreadAlertStatusCharacteristic(BaseCharacteristic[UnreadAlertStatusData]):
28 """Unread Alert Status characteristic (0x2A45).
30 Represents the number of unread alerts in a specific category.
32 Structure (2 bytes):
33 - Category ID: uint8 (0=Simple Alert, 1=Email, etc.)
34 - Unread Count: uint8 (0-254, 255 means more than 254 unread alerts)
36 Used by Alert Notification Service (0x1811).
37 """
39 expected_length: int | None = 2
41 def _decode_value(self, data: bytearray, ctx: CharacteristicContext | None = None) -> UnreadAlertStatusData:
42 """Decode Unread Alert Status data from bytes.
44 Args:
45 data: Raw characteristic data (2 bytes)
46 ctx: Optional characteristic context
48 Returns:
49 UnreadAlertStatusData with all fields
51 Raises:
52 ValueError: If data contains invalid values
54 """
55 # Parse Category ID (1 byte)
56 category_id_raw = DataParser.parse_int8(data, 0, signed=False)
57 category_id = AlertCategoryID(category_id_raw)
59 # Parse Unread Count (1 byte)
60 unread_count = DataParser.parse_int8(data, 1, signed=False)
62 return UnreadAlertStatusData(
63 category_id=category_id,
64 unread_count=unread_count,
65 )
67 def _encode_value(self, data: UnreadAlertStatusData) -> bytearray:
68 """Encode Unread Alert Status data to bytes.
70 Args:
71 data: UnreadAlertStatusData to encode
73 Returns:
74 Encoded unread alert status (2 bytes)
76 Raises:
77 ValueError: If data contains invalid values
79 """
80 result = bytearray()
82 # Encode Category ID (1 byte)
83 category_id_value = int(data.category_id)
84 result.append(category_id_value)
86 # Encode Unread Count (1 byte)
87 result.append(data.unread_count)
89 return result