Coverage for src / bluetooth_sig / gatt / characteristics / temperature.py: 100%

12 statements  

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

1"""Temperature characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5from ..context import CharacteristicContext 

6from .base import BaseCharacteristic 

7from .utils.data_parser import DataParser 

8 

9 

10class TemperatureCharacteristic(BaseCharacteristic[float]): 

11 """Temperature characteristic (0x2A6E). 

12 

13 org.bluetooth.characteristic.temperature 

14 

15 Temperature measurement characteristic. 

16 """ 

17 

18 expected_type: type | None = float 

19 

20 def _decode_value( 

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

22 ) -> float: 

23 """Decode temperature characteristic. 

24 

25 Decodes a 16-bit signed integer representing temperature in 0.01°C increments 

26 per Bluetooth SIG Temperature characteristic specification. 

27 

28 Args: 

29 data: Raw bytes from BLE characteristic (exactly 2 bytes, little-endian) 

30 ctx: Optional context for parsing (device info, flags, etc.) 

31 validate: Whether to validate ranges (default True) 

32 

33 Returns: 

34 Temperature in degrees Celsius, or None if value is unknown (-32768) 

35 

36 Raises: 

37 InsufficientDataError: If data is not exactly 2 bytes 

38 """ 

39 raw_value = DataParser.parse_int16(data, 0, signed=True) 

40 return raw_value * 0.01 

41 

42 def _encode_value(self, data: float) -> bytearray: 

43 """Encode temperature value.""" 

44 raw_value = int(data / 0.01) 

45 return DataParser.encode_int16(raw_value, signed=True)