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

25 statements  

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

1"""Day Date Time 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 DayDateTimeData(msgspec.Struct, frozen=True, kw_only=True): 

15 """Day, date and time data structure with day of week. 

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 """ 

21 

22 dt: datetime 

23 day_of_week: int 

24 

25 

26class DayDateTimeCharacteristic(BaseCharacteristic[DayDateTimeData]): 

27 """Day Date Time characteristic (0x2A0A). 

28 

29 org.bluetooth.characteristic.day_date_time 

30 

31 Represents date, time and day of week in 8-byte format. 

32 """ 

33 

34 _manual_value_type = "DayDateTimeData" 

35 expected_length = 8 

36 

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

38 """Parse day date time value. 

39 

40 Args: 

41 data: Raw bytearray from BLE characteristic (8 bytes). 

42 ctx: Optional CharacteristicContext. 

43 

44 Returns: 

45 DayDateTimeData with datetime and day_of_week. 

46 """ 

47 dt = datetime( 

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

49 month=data[2], 

50 day=data[3], 

51 hour=data[4], 

52 minute=data[5], 

53 second=data[6], 

54 ) 

55 return DayDateTimeData(dt=dt, day_of_week=data[7]) 

56 

57 def _encode_value(self, data: DayDateTimeData) -> bytearray: 

58 """Encode day date time value back to bytes. 

59 

60 Args: 

61 data: DayDateTimeData to encode 

62 

63 Returns: 

64 Encoded bytes (8 bytes) 

65 """ 

66 result = bytearray() 

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

68 result.append(data.dt.month) 

69 result.append(data.dt.day) 

70 result.append(data.dt.hour) 

71 result.append(data.dt.minute) 

72 result.append(data.dt.second) 

73 result.append(data.day_of_week) 

74 return result