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

24 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-18 11:17 +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 expected_length = 8 

35 

36 def _decode_value( 

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

38 ) -> DayDateTimeData: 

39 """Parse day date time value. 

40 

41 Args: 

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

43 ctx: Optional CharacteristicContext. 

44 validate: Whether to validate ranges (default True) 

45 

46 Returns: 

47 DayDateTimeData with datetime and day_of_week. 

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 DayDateTimeData(dt=dt, day_of_week=data[7]) 

58 

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

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

61 

62 Args: 

63 data: DayDateTimeData to encode 

64 

65 Returns: 

66 Encoded bytes (8 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 return result