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

19 statements  

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

1"""Object Last-Modified characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5from datetime import datetime 

6 

7from ..context import CharacteristicContext 

8from .base import BaseCharacteristic 

9from .utils import DataParser 

10 

11 

12class ObjectLastModifiedCharacteristic(BaseCharacteristic[datetime]): 

13 """Object Last-Modified characteristic (0x2AC2). 

14 

15 org.bluetooth.characteristic.object_last_modified 

16 

17 Represents the date/time when an OTS object was last modified. 

18 Uses the standard Date Time format: year (uint16), month (uint8), 

19 day (uint8), hours (uint8), minutes (uint8), seconds (uint8). 

20 """ 

21 

22 expected_length: int = 7 

23 min_length: int = 7 

24 

25 def _decode_value( 

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

27 ) -> datetime: 

28 """Parse object last-modified date/time. 

29 

30 Args: 

31 data: Raw bytes (7 bytes, Date Time format). 

32 ctx: Optional CharacteristicContext. 

33 validate: Whether to validate ranges (default True). 

34 

35 Returns: 

36 Python datetime object. 

37 

38 """ 

39 return datetime( 

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

41 month=data[2], 

42 day=data[3], 

43 hour=data[4], 

44 minute=data[5], 

45 second=data[6], 

46 ) 

47 

48 def _encode_value(self, data: datetime) -> bytearray: 

49 """Encode datetime to Date Time format bytes. 

50 

51 Args: 

52 data: Python datetime object. 

53 

54 Returns: 

55 Encoded bytes (7 bytes). 

56 

57 """ 

58 result = bytearray() 

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

60 result.append(data.month) 

61 result.append(data.day) 

62 result.append(data.hour) 

63 result.append(data.minute) 

64 result.append(data.second) 

65 return result