Coverage for src / bluetooth_sig / gatt / characteristics / time_update_state.py: 100%
29 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-03 16:41 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-03 16:41 +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 (RTUS v1.0, Section 3.2)."""
24 IDLE = 0x00
25 UPDATE_PENDING = 0x01
28class TimeUpdateResult(IntEnum):
29 """Time Update Result values."""
31 SUCCESSFUL = 0x00
32 CANCELED = 0x01
33 NO_CONNECTION_TO_REFERENCE = 0x02
34 REFERENCE_RESPONDED_WITH_ERROR = 0x03
35 TIMEOUT = 0x04
36 UPDATE_NOT_ATTEMPTED_AFTER_RESET = 0x05
39class TimeUpdateStateCharacteristic(BaseCharacteristic[TimeUpdateState]):
40 """Time Update State characteristic.
42 Indicates the current state of time update operations.
44 Value: 2 bytes
45 - Current State: uint8 (0=Idle, 1=Pending, 2=Updating)
46 - Result: uint8 (0=Successful, 1=Canceled, etc.)
47 """
49 min_length: int = 2
50 max_length: int = 2
52 def __init__(self) -> None:
53 """Initialize the Time Update State characteristic."""
54 super().__init__()
56 def _decode_value(
57 self, data: bytearray, ctx: CharacteristicContext | None = None, *, validate: bool = True
58 ) -> TimeUpdateState:
59 """Decode the raw data to TimeUpdateState."""
60 current_state = TimeUpdateCurrentState(data[0])
61 result = TimeUpdateResult(data[1])
63 return TimeUpdateState(current_state=current_state, result=result)
65 def _encode_value(self, data: TimeUpdateState) -> bytearray:
66 """Encode TimeUpdateState to bytes."""
67 return bytearray([int(data.current_state), int(data.result)])