Coverage for src / bluetooth_sig / gatt / descriptors / valid_range_and_accuracy.py: 81%
21 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-18 11:17 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-18 11:17 +0000
1"""Valid Range and Accuracy Descriptor implementation."""
3from __future__ import annotations
5import msgspec
7from ..characteristics.utils import DataParser
8from .base import BaseDescriptor, RangeDescriptorMixin
11class ValidRangeAndAccuracyData(msgspec.Struct, frozen=True, kw_only=True):
12 """Valid Range and Accuracy descriptor data."""
14 min_value: int | float
15 max_value: int | float
16 accuracy: int | float
19class ValidRangeAndAccuracyDescriptor(BaseDescriptor, RangeDescriptorMixin):
20 """Valid Range and Accuracy Descriptor (0x2911).
22 Defines the valid range and accuracy for characteristic values.
23 Contains minimum value, maximum value, and accuracy information.
24 """
26 def _has_structured_data(self) -> bool:
27 return True
29 def _get_data_format(self) -> str:
30 return "struct"
32 def _parse_descriptor_value(self, data: bytes) -> ValidRangeAndAccuracyData:
33 """Parse Valid Range and Accuracy descriptor value.
35 The format depends on the characteristic's value type.
36 For simplicity, this implementation assumes uint16 values.
38 Args:
39 data: Raw bytes containing min, max, and accuracy values
41 Returns:
42 ValidRangeAndAccuracyData with range and accuracy info
44 Raises:
45 ValueError: If data length is incorrect
46 """
47 # Valid Range and Accuracy format: min_value + max_value + accuracy
48 # For now, assume 6 bytes total (2 bytes each for uint16)
50 min_value = DataParser.parse_int16(data, offset=0, endian="little")
51 max_value = DataParser.parse_int16(data, offset=2, endian="little")
52 accuracy = DataParser.parse_int16(data, offset=4, endian="little")
54 return ValidRangeAndAccuracyData(
55 min_value=min_value,
56 max_value=max_value,
57 accuracy=accuracy,
58 )
60 def get_accuracy(self, data: bytes) -> int | float:
61 """Get the accuracy value.
63 Args:
64 data: Raw descriptor data
66 Returns:
67 Accuracy value for the characteristic
68 """
69 parsed = self._parse_descriptor_value(data)
70 return parsed.accuracy