Coverage for src / bluetooth_sig / gatt / characteristics / supported_new_alert_category.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-18 11:17 +0000

1"""Supported New Alert Category characteristic (0x2A47) implementation. 

2 

3Represents which alert categories the server supports for new alerts. 

4Used by Alert Notification Service (0x1811). 

5 

6Based on Bluetooth SIG GATT Specification: 

7- Supported New Alert Category: 2 bytes (16-bit Category ID Bit Mask) 

8""" 

9 

10from __future__ import annotations 

11 

12from ...types import AlertCategoryBitMask 

13from ..context import CharacteristicContext 

14from .base import BaseCharacteristic 

15from .utils import DataParser 

16 

17 

18class SupportedNewAlertCategoryCharacteristic(BaseCharacteristic[AlertCategoryBitMask]): 

19 """Supported New Alert Category characteristic (0x2A47). 

20 

21 Represents which alert categories the server supports for new alerts 

22 using a 16-bit bitmask. 

23 

24 Structure (2 bytes): 

25 - Category ID Bit Mask: uint16 (bit 0=Simple Alert, bit 1=Email, etc.) 

26 Bits 10-15 reserved for future use 

27 

28 Used by Alert Notification Service (0x1811). 

29 """ 

30 

31 # YAML specifies size: 1 or 2 (variable length struct) 

32 min_length: int | None = 1 

33 max_length: int | None = 2 

34 

35 def _decode_value( 

36 self, data: bytearray, ctx: CharacteristicContext | None = None, *, validate: bool = True 

37 ) -> AlertCategoryBitMask: 

38 """Decode Supported New Alert Category data from bytes. 

39 

40 Args: 

41 data: Raw characteristic data (2 bytes) 

42 ctx: Optional characteristic context 

43 validate: Whether to validate ranges (default True) 

44 

45 Returns: 

46 AlertCategoryBitMask flags 

47 

48 Raises: 

49 ValueError: If data is insufficient 

50 

51 """ 

52 mask_value = DataParser.parse_int16(data, 0, signed=False) 

53 return AlertCategoryBitMask(mask_value) 

54 

55 def _encode_value(self, data: AlertCategoryBitMask | int) -> bytearray: 

56 """Encode Supported New Alert Category data to bytes. 

57 

58 Args: 

59 data: AlertCategoryBitMask or int value 

60 

61 Returns: 

62 Encoded supported new alert category (2 bytes) 

63 

64 """ 

65 int_value = int(data) if isinstance(data, AlertCategoryBitMask) else data 

66 return DataParser.encode_int16(int_value, signed=False)