Coverage for src / bluetooth_sig / gatt / characteristics / exact_time_256.py: 100%
26 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-18 11:17 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-18 11:17 +0000
1"""Exact Time 256 characteristic implementation."""
3from __future__ import annotations
5from datetime import datetime
7import msgspec
9from ..context import CharacteristicContext
10from .base import BaseCharacteristic
11from .utils import DataParser
14class ExactTime256Data(msgspec.Struct, frozen=True, kw_only=True):
15 """Exact time with 1/256 second resolution.
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 """
23 dt: datetime
24 day_of_week: int
25 fractions256: int
28class ExactTime256Characteristic(BaseCharacteristic[ExactTime256Data]):
29 """Exact Time 256 characteristic (0x2A0C).
31 org.bluetooth.characteristic.exact_time_256
33 Represents exact time with 1/256 second resolution in 9-byte format.
34 """
36 expected_length = 9
38 def _decode_value(
39 self, data: bytearray, ctx: CharacteristicContext | None = None, *, validate: bool = True
40 ) -> ExactTime256Data:
41 """Parse exact time 256 value.
43 Args:
44 data: Raw bytearray from BLE characteristic (9 bytes).
45 ctx: Optional CharacteristicContext.
46 validate: Whether to validate ranges (default True)
48 Returns:
49 ExactTime256Data with datetime, day_of_week, and fractions256.
50 """
51 dt = datetime(
52 year=DataParser.parse_int16(data, 0, signed=False),
53 month=data[2],
54 day=data[3],
55 hour=data[4],
56 minute=data[5],
57 second=data[6],
58 )
59 return ExactTime256Data(dt=dt, day_of_week=data[7], fractions256=data[8])
61 def _encode_value(self, data: ExactTime256Data) -> bytearray:
62 """Encode exact time 256 value back to bytes.
64 Args:
65 data: ExactTime256Data to encode
67 Returns:
68 Encoded bytes (9 bytes)
69 """
70 result = bytearray()
71 result.extend(DataParser.encode_int16(data.dt.year, signed=False))
72 result.append(data.dt.month)
73 result.append(data.dt.day)
74 result.append(data.dt.hour)
75 result.append(data.dt.minute)
76 result.append(data.dt.second)
77 result.append(data.day_of_week)
78 result.append(data.fractions256)
79 return result