Coverage for src / bluetooth_sig / gatt / characteristics / electric_current_range.py: 86%

36 statements  

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

1"""Electric Current Range characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5import msgspec 

6 

7from ..constants import UINT16_MAX 

8from ..context import CharacteristicContext 

9from .base import BaseCharacteristic 

10from .utils import DataParser 

11 

12 

13class ElectricCurrentRangeData(msgspec.Struct, frozen=True, kw_only=True): # pylint: disable=too-few-public-methods 

14 """Data class for electric current range.""" 

15 

16 min: float # Minimum current in Amperes 

17 max: float # Maximum current in Amperes 

18 

19 def __post_init__(self) -> None: 

20 """Validate current range data.""" 

21 if self.min > self.max: 

22 raise ValueError(f"Minimum current {self.min} A cannot be greater than maximum {self.max} A") 

23 

24 # Validate range for uint16 with 0.01 A resolution (0 to 655.35 A) 

25 max_current_value = UINT16_MAX * 0.01 

26 if not 0.0 <= self.min <= max_current_value: 

27 raise ValueError(f"Minimum current {self.min} A is outside valid range (0.0 to {max_current_value} A)") 

28 if not 0.0 <= self.max <= max_current_value: 

29 raise ValueError(f"Maximum current {self.max} A is outside valid range (0.0 to {max_current_value} A)") 

30 

31 

32class ElectricCurrentRangeCharacteristic(BaseCharacteristic[ElectricCurrentRangeData]): 

33 """Electric Current Range characteristic (0x2AEF). 

34 

35 org.bluetooth.characteristic.electric_current_range 

36 

37 Electric Current Range characteristic. 

38 

39 Specifies lower and upper current bounds (2x uint16). 

40 """ 

41 

42 # Validation attributes 

43 expected_length: int = 4 # 2x uint16 

44 min_length: int = 4 

45 

46 def _decode_value( 

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

48 ) -> ElectricCurrentRangeData: 

49 """Parse current range data (2x uint16 in units of 0.01 A). 

50 

51 Args: 

52 data: Raw bytes from the characteristic read. 

53 ctx: Optional CharacteristicContext providing surrounding context (may be None). 

54 validate: Whether to validate ranges (default True) 

55 

56 Returns: 

57 ElectricCurrentRangeData with 'min' and 'max' current values in Amperes. 

58 

59 Raises: 

60 ValueError: If data is insufficient. 

61 

62 """ 

63 # Convert 2x uint16 (little endian) to current range in Amperes 

64 min_current_raw = DataParser.parse_int16(data, 0, signed=False) 

65 max_current_raw = DataParser.parse_int16(data, 2, signed=False) 

66 

67 return ElectricCurrentRangeData(min=min_current_raw * 0.01, max=max_current_raw * 0.01) 

68 

69 def _encode_value(self, data: ElectricCurrentRangeData) -> bytearray: 

70 """Encode electric current range value back to bytes. 

71 

72 Args: 

73 data: ElectricCurrentRangeData instance with 'min' and 'max' current values in Amperes 

74 

75 Returns: 

76 Encoded bytes representing the current range (2x uint16, 0.01 A resolution) 

77 

78 """ 

79 if not isinstance(data, ElectricCurrentRangeData): 

80 raise TypeError( 

81 f"Electric current range data must be an ElectricCurrentRangeData, got {type(data).__name__}" 

82 ) 

83 # Convert Amperes to raw values (multiply by 100 for 0.01 A resolution) 

84 min_current_raw = round(data.min * 100) 

85 max_current_raw = round(data.max * 100) 

86 

87 # Validate range for uint16 (0 to UINT16_MAX) 

88 for name, value in [("minimum", min_current_raw), ("maximum", max_current_raw)]: 

89 if not 0 <= value <= UINT16_MAX: 

90 raise ValueError(f"Current {name} value {value} exceeds uint16 range") 

91 

92 # Encode as 2 uint16 values (little endian) 

93 result = bytearray() 

94 result.extend(DataParser.encode_int16(min_current_raw, signed=False)) 

95 result.extend(DataParser.encode_int16(max_current_raw, signed=False)) 

96 

97 return result