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

1"""Time Update State characteristic (0x2A17) implementation.""" 

2 

3from __future__ import annotations 

4 

5from enum import IntEnum 

6 

7import msgspec 

8 

9from bluetooth_sig.types.context import CharacteristicContext 

10 

11from .base import BaseCharacteristic 

12 

13 

14class TimeUpdateState(msgspec.Struct, kw_only=True): 

15 """Time Update State data structure.""" 

16 

17 current_state: TimeUpdateCurrentState 

18 result: TimeUpdateResult 

19 

20 

21class TimeUpdateCurrentState(IntEnum): 

22 """Time Update Current State values.""" 

23 

24 IDLE = 0x00 

25 PENDING = 0x01 

26 UPDATING = 0x02 

27 

28 

29class TimeUpdateResult(IntEnum): 

30 """Time Update Result values.""" 

31 

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 

38 

39 

40class TimeUpdateStateCharacteristic(BaseCharacteristic[TimeUpdateState]): 

41 """Time Update State characteristic. 

42 

43 Indicates the current state of time update operations. 

44 

45 Value: 2 bytes 

46 - Current State: uint8 (0=Idle, 1=Pending, 2=Updating) 

47 - Result: uint8 (0=Successful, 1=Canceled, etc.) 

48 """ 

49 

50 def __init__(self) -> None: 

51 """Initialize the Time Update State characteristic.""" 

52 super().__init__() 

53 

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)}") 

58 

59 current_state = TimeUpdateCurrentState(data[0]) 

60 result = TimeUpdateResult(data[1]) 

61 

62 return TimeUpdateState(current_state=current_state, result=result) 

63 

64 def _encode_value(self, data: TimeUpdateState) -> bytearray: 

65 """Encode TimeUpdateState to bytes.""" 

66 return bytearray([int(data.current_state), int(data.result)])