Coverage for src/bluetooth_sig/gatt/descriptors/number_of_digitals.py: 89%

19 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-30 00:10 +0000

1"""Number of Digitals Descriptor implementation.""" 

2 

3from __future__ import annotations 

4 

5import msgspec 

6 

7from ..characteristics.utils import DataParser 

8from .base import BaseDescriptor 

9 

10 

11class NumberOfDigitalsData(msgspec.Struct, frozen=True, kw_only=True): 

12 """Number of Digitals descriptor data.""" 

13 

14 number_of_digitals: int 

15 

16 

17class NumberOfDigitalsDescriptor(BaseDescriptor): 

18 """Number of Digitals Descriptor (0x2909). 

19 

20 Specifies the number of digital states supported by a characteristic. 

21 Used in sensor applications. 

22 """ 

23 

24 def _has_structured_data(self) -> bool: 

25 return True 

26 

27 def _get_data_format(self) -> str: 

28 return "uint8" 

29 

30 def _parse_descriptor_value(self, data: bytes) -> NumberOfDigitalsData: 

31 """Parse Number of Digitals value. 

32 

33 Args: 

34 data: Raw bytes (should be 1 byte for uint8) 

35 

36 Returns: 

37 NumberOfDigitalsData with number of digitals 

38 

39 Raises: 

40 ValueError: If data is not exactly 1 byte 

41 """ 

42 if len(data) != 1: 

43 raise ValueError(f"Number of Digitals data must be exactly 1 byte, got {len(data)}") 

44 

45 number_of_digitals = DataParser.parse_int8(data) 

46 

47 return NumberOfDigitalsData(number_of_digitals=number_of_digitals) 

48 

49 def get_number_of_digitals(self, data: bytes) -> int: 

50 """Get the number of digitals.""" 

51 parsed = self._parse_descriptor_value(data) 

52 return parsed.number_of_digitals