Coverage for src/bluetooth_sig/gatt/descriptors/value_trigger_setting.py: 84%
37 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-30 00:10 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-30 00:10 +0000
1"""Value Trigger Setting Descriptor implementation."""
3from __future__ import annotations
5from enum import IntEnum
7import msgspec
9from ..characteristics.utils import DataParser
10from .base import BaseDescriptor
13class TriggerCondition(IntEnum):
14 """Trigger condition values for Value Trigger Setting."""
16 NONE = 0x00
17 EQUAL_TO = 0x01
18 NOT_EQUAL_TO = 0x02
19 LESS_THAN = 0x03
20 LESS_THAN_OR_EQUAL_TO = 0x04
21 GREATER_THAN = 0x05
22 GREATER_THAN_OR_EQUAL_TO = 0x06
25class ValueTriggerSettingData(msgspec.Struct, frozen=True, kw_only=True):
26 """Value Trigger Setting descriptor data."""
28 condition: int
29 value: int # This would typically be the same format as the characteristic value
32class ValueTriggerSettingDescriptor(BaseDescriptor):
33 """Value Trigger Setting Descriptor (0x290A).
35 Defines trigger conditions for value-based notifications.
36 Contains condition and reference value for triggering.
37 """
39 def _has_structured_data(self) -> bool:
40 return True
42 def _get_data_format(self) -> str:
43 return "struct"
45 def _parse_descriptor_value(self, data: bytes) -> ValueTriggerSettingData:
46 """Parse Value Trigger Setting value.
48 Format depends on the characteristic's value format.
49 Basic implementation assumes: condition (1 byte) + value (same as characteristic).
51 Args:
52 data: Raw bytes containing condition and value
54 Returns:
55 ValueTriggerSettingData with condition and value
57 Raises:
58 ValueError: If data is too short
59 """
60 if len(data) < 2:
61 raise ValueError(f"Value Trigger Setting data must be at least 2 bytes, got {len(data)}")
63 condition = DataParser.parse_int8(data, offset=0)
65 # For simplicity, assume the value is a uint8 following the condition
66 # In practice, this should match the characteristic's value format
67 value = DataParser.parse_int8(data, offset=1)
69 return ValueTriggerSettingData(
70 condition=condition,
71 value=value,
72 )
74 def get_condition(self, data: bytes) -> int:
75 """Get the trigger condition."""
76 parsed = self._parse_descriptor_value(data)
77 return parsed.condition
79 def get_trigger_value(self, data: bytes) -> int:
80 """Get the trigger reference value."""
81 parsed = self._parse_descriptor_value(data)
82 return parsed.value
84 def is_condition_equal_to(self, data: bytes) -> bool:
85 """Check if condition is 'equal to'."""
86 return self.get_condition(data) == TriggerCondition.EQUAL_TO
88 def is_condition_greater_than(self, data: bytes) -> bool:
89 """Check if condition is 'greater than'."""
90 return self.get_condition(data) == TriggerCondition.GREATER_THAN