Coverage for src / bluetooth_sig / gatt / characteristics / electric_current_statistics.py: 92%

36 statements  

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

1"""Electric Current Statistics 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 ElectricCurrentStatisticsData(msgspec.Struct, frozen=True, kw_only=True): # pylint: disable=too-few-public-methods 

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

15 

16 minimum: float # Minimum current in Amperes 

17 maximum: float # Maximum current in Amperes 

18 average: float # Average current in Amperes 

19 

20 def __post_init__(self) -> None: 

21 """Validate current statistics data.""" 

22 # Validate logical order 

23 if self.minimum > self.maximum: 

24 raise ValueError(f"Minimum current {self.minimum} A cannot be greater than maximum {self.maximum} A") 

25 if not self.minimum <= self.average <= self.maximum: 

26 raise ValueError( 

27 f"Average current {self.average} A must be between " 

28 f"minimum {self.minimum} A and maximum {self.maximum} A" 

29 ) 

30 

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

32 max_current_value = UINT16_MAX * 0.01 

33 for name, current in [ 

34 ("minimum", self.minimum), 

35 ("maximum", self.maximum), 

36 ("average", self.average), 

37 ]: 

38 if not 0.0 <= current <= max_current_value: 

39 raise ValueError( 

40 f"{name.capitalize()} current {current} A is outside valid range (0.0 to {max_current_value} A)" 

41 ) 

42 

43 

44class ElectricCurrentStatisticsCharacteristic(BaseCharacteristic[ElectricCurrentStatisticsData]): 

45 """Electric Current Statistics characteristic (0x2AF1). 

46 

47 org.bluetooth.characteristic.electric_current_statistics 

48 

49 Electric Current Statistics characteristic. 

50 

51 Provides statistical current data (min, max, average over time). 

52 """ 

53 

54 # Validation attributes 

55 expected_length: int = 6 # 3x uint16 

56 min_length: int = 6 

57 

58 def _decode_value( 

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

60 ) -> ElectricCurrentStatisticsData: 

61 """Parse current statistics data (3x uint16 in units of 0.01 A). 

62 

63 Args: 

64 data: Raw bytes from the characteristic read. 

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

66 validate: Whether to validate ranges (default True) 

67 

68 Returns: 

69 ElectricCurrentStatisticsData with 'minimum', 'maximum', and 'average' current values in Amperes. 

70 

71 Raises: 

72 ValueError: If data is insufficient. 

73 

74 """ 

75 # Convert 3x uint16 (little endian) to current statistics in Amperes 

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

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

78 avg_current_raw = DataParser.parse_int16(data, 4, signed=False) 

79 

80 return ElectricCurrentStatisticsData( 

81 minimum=min_current_raw * 0.01, 

82 maximum=max_current_raw * 0.01, 

83 average=avg_current_raw * 0.01, 

84 ) 

85 

86 def _encode_value(self, data: ElectricCurrentStatisticsData) -> bytearray: 

87 """Encode electric current statistics value back to bytes. 

88 

89 Args: 

90 data: ElectricCurrentStatisticsData instance 

91 

92 Returns: 

93 Encoded bytes representing the current statistics (3x uint16, 0.01 A resolution) 

94 

95 """ 

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

97 min_current_raw = round(data.minimum * 100) 

98 max_current_raw = round(data.maximum * 100) 

99 avg_current_raw = round(data.average * 100) 

100 

101 # Encode as 3 uint16 values (little endian) 

102 result = bytearray() 

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

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

105 result.extend(DataParser.encode_int16(avg_current_raw, signed=False)) 

106 

107 return result