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

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 (RTUS v1.0, Section 3.2).""" 

23 

24 IDLE = 0x00 

25 UPDATE_PENDING = 0x01 

26 

27 

28class TimeUpdateResult(IntEnum): 

29 """Time Update Result values.""" 

30 

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 

37 

38 

39class TimeUpdateStateCharacteristic(BaseCharacteristic[TimeUpdateState]): 

40 """Time Update State characteristic. 

41 

42 Indicates the current state of time update operations. 

43 

44 Value: 2 bytes 

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

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

47 """ 

48 

49 min_length: int = 2 

50 max_length: int = 2 

51 

52 def __init__(self) -> None: 

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

54 super().__init__() 

55 

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]) 

62 

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

64 

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

66 """Encode TimeUpdateState to bytes.""" 

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