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
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-18 11:17 +0000
1"""Electric Current Range characteristic implementation."""
3from __future__ import annotations
5import msgspec
7from ..constants import UINT16_MAX
8from ..context import CharacteristicContext
9from .base import BaseCharacteristic
10from .utils import DataParser
13class ElectricCurrentRangeData(msgspec.Struct, frozen=True, kw_only=True): # pylint: disable=too-few-public-methods
14 """Data class for electric current range."""
16 min: float # Minimum current in Amperes
17 max: float # Maximum current in Amperes
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")
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)")
32class ElectricCurrentRangeCharacteristic(BaseCharacteristic[ElectricCurrentRangeData]):
33 """Electric Current Range characteristic (0x2AEF).
35 org.bluetooth.characteristic.electric_current_range
37 Electric Current Range characteristic.
39 Specifies lower and upper current bounds (2x uint16).
40 """
42 # Validation attributes
43 expected_length: int = 4 # 2x uint16
44 min_length: int = 4
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).
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)
56 Returns:
57 ElectricCurrentRangeData with 'min' and 'max' current values in Amperes.
59 Raises:
60 ValueError: If data is insufficient.
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)
67 return ElectricCurrentRangeData(min=min_current_raw * 0.01, max=max_current_raw * 0.01)
69 def _encode_value(self, data: ElectricCurrentRangeData) -> bytearray:
70 """Encode electric current range value back to bytes.
72 Args:
73 data: ElectricCurrentRangeData instance with 'min' and 'max' current values in Amperes
75 Returns:
76 Encoded bytes representing the current range (2x uint16, 0.01 A resolution)
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)
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")
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))
97 return result