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

37 statements  

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

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

2 

3from __future__ import annotations 

4 

5import msgspec 

6 

7from ...types.gatt_enums import ValueType 

8from ..constants import UINT16_MAX 

9from ..context import CharacteristicContext 

10from .base import BaseCharacteristic 

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): 

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 # Override since decode_value returns structured ElectricCurrentRangeData 

43 _manual_value_type: ValueType | str | None = ValueType.DICT 

44 

45 def decode_value(self, data: bytearray, ctx: CharacteristicContext | None = None) -> ElectricCurrentRangeData: 

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

47 

48 Args: 

49 data: Raw bytes from the characteristic read. 

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

51 

52 Returns: 

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

54 

55 Raises: 

56 ValueError: If data is insufficient. 

57 

58 """ 

59 if len(data) < 4: 

60 raise ValueError("Electric current range data must be at least 4 bytes") 

61 

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

63 min_current_raw = int.from_bytes(data[:2], byteorder="little", signed=False) 

64 max_current_raw = int.from_bytes(data[2:4], byteorder="little", signed=False) 

65 

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

67 

68 def encode_value(self, data: ElectricCurrentRangeData) -> bytearray: 

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

70 

71 Args: 

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

73 

74 Returns: 

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

76 

77 """ 

78 if not isinstance(data, ElectricCurrentRangeData): 

79 raise TypeError( 

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

81 ) 

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

83 min_current_raw = round(data.min * 100) 

84 max_current_raw = round(data.max * 100) 

85 

86 # Validate range for uint16 (0 to UINT16_MAX) 

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

88 if not 0 <= value <= UINT16_MAX: 

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

90 

91 # Encode as 2 uint16 values (little endian) 

92 result = bytearray() 

93 result.extend(min_current_raw.to_bytes(2, byteorder="little", signed=False)) 

94 result.extend(max_current_raw.to_bytes(2, byteorder="little", signed=False)) 

95 

96 return result