Coverage for src / bluetooth_sig / gatt / characteristics / time_update_state.py: 100%
30 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-11 20:14 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-11 20:14 +0000
1"""Time Update State characteristic (0x2A17) implementation."""
3from __future__ import annotations
5from enum import IntEnum
7import msgspec
9from bluetooth_sig.types.context import CharacteristicContext
11from .base import BaseCharacteristic
14class TimeUpdateState(msgspec.Struct, kw_only=True):
15 """Time Update State data structure."""
17 current_state: TimeUpdateCurrentState
18 result: TimeUpdateResult
21class TimeUpdateCurrentState(IntEnum):
22 """Time Update Current State values."""
24 IDLE = 0x00
25 PENDING = 0x01
26 UPDATING = 0x02
29class TimeUpdateResult(IntEnum):
30 """Time Update Result values."""
32 SUCCESSFUL = 0x00
33 CANCELED = 0x01
34 NO_CONNECTION_TO_REFERENCE = 0x02
35 REFERENCE_RESPONDED_WITH_ERROR = 0x03
36 TIMEOUT = 0x04
37 UPDATE_NOT_ATTEMPTED_AFTER_RESET = 0x05
40class TimeUpdateStateCharacteristic(BaseCharacteristic[TimeUpdateState]):
41 """Time Update State characteristic.
43 Indicates the current state of time update operations.
45 Value: 2 bytes
46 - Current State: uint8 (0=Idle, 1=Pending, 2=Updating)
47 - Result: uint8 (0=Successful, 1=Canceled, etc.)
48 """
50 def __init__(self) -> None:
51 """Initialize the Time Update State characteristic."""
52 super().__init__()
54 def _decode_value(self, data: bytearray, ctx: CharacteristicContext | None = None) -> TimeUpdateState:
55 """Decode the raw data to TimeUpdateState."""
56 if len(data) != 2:
57 raise ValueError(f"Time Update State requires 2 bytes, got {len(data)}")
59 current_state = TimeUpdateCurrentState(data[0])
60 result = TimeUpdateResult(data[1])
62 return TimeUpdateState(current_state=current_state, result=result)
64 def _encode_value(self, data: TimeUpdateState) -> bytearray:
65 """Encode TimeUpdateState to bytes."""
66 return bytearray([int(data.current_state), int(data.result)])