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

27 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-11 20:14 +0000

1"""Exact Time 256 characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5from datetime import datetime 

6 

7import msgspec 

8 

9from ..context import CharacteristicContext 

10from .base import BaseCharacteristic 

11from .utils import DataParser 

12 

13 

14class ExactTime256Data(msgspec.Struct, frozen=True, kw_only=True): 

15 """Exact time with 1/256 second resolution. 

16 

17 Attributes: 

18 dt: Python datetime for date and time components 

19 day_of_week: Day of week (1=Monday, 7=Sunday, 0=Unknown) 

20 fractions256: Fractions of a second (1/256 resolution, 0-255) 

21 """ 

22 

23 dt: datetime 

24 day_of_week: int 

25 fractions256: int 

26 

27 

28class ExactTime256Characteristic(BaseCharacteristic[ExactTime256Data]): 

29 """Exact Time 256 characteristic (0x2A0C). 

30 

31 org.bluetooth.characteristic.exact_time_256 

32 

33 Represents exact time with 1/256 second resolution in 9-byte format. 

34 """ 

35 

36 _manual_value_type = "ExactTime256Data" 

37 expected_length = 9 

38 

39 def _decode_value(self, data: bytearray, ctx: CharacteristicContext | None = None) -> ExactTime256Data: 

40 """Parse exact time 256 value. 

41 

42 Args: 

43 data: Raw bytearray from BLE characteristic (9 bytes). 

44 ctx: Optional CharacteristicContext. 

45 

46 Returns: 

47 ExactTime256Data with datetime, day_of_week, and fractions256. 

48 """ 

49 dt = datetime( 

50 year=DataParser.parse_int16(data, 0, signed=False), 

51 month=data[2], 

52 day=data[3], 

53 hour=data[4], 

54 minute=data[5], 

55 second=data[6], 

56 ) 

57 return ExactTime256Data(dt=dt, day_of_week=data[7], fractions256=data[8]) 

58 

59 def _encode_value(self, data: ExactTime256Data) -> bytearray: 

60 """Encode exact time 256 value back to bytes. 

61 

62 Args: 

63 data: ExactTime256Data to encode 

64 

65 Returns: 

66 Encoded bytes (9 bytes) 

67 """ 

68 result = bytearray() 

69 result.extend(DataParser.encode_int16(data.dt.year, signed=False)) 

70 result.append(data.dt.month) 

71 result.append(data.dt.day) 

72 result.append(data.dt.hour) 

73 result.append(data.dt.minute) 

74 result.append(data.dt.second) 

75 result.append(data.day_of_week) 

76 result.append(data.fractions256) 

77 return result