Coverage for src / bluetooth_sig / gatt / characteristics / boolean.py: 100%
9 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-18 11:17 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-18 11:17 +0000
1"""Boolean characteristic implementation."""
3from __future__ import annotations
5from ..context import CharacteristicContext
6from .base import BaseCharacteristic
9class BooleanCharacteristic(BaseCharacteristic[bool]):
10 """Boolean characteristic (0x2AE2).
12 org.bluetooth.characteristic.boolean
14 The Boolean characteristic is used to represent predefined Boolean values (0 or 1).
15 """
17 expected_length = 1
19 def _decode_value(
20 self, data: bytearray, ctx: CharacteristicContext | None = None, *, validate: bool = True
21 ) -> bool:
22 """Parse boolean value.
24 Args:
25 data: Raw bytearray from BLE characteristic (1 byte, validated by base class).
26 ctx: Optional CharacteristicContext.
27 validate: Whether to validate ranges (default True)
29 Returns:
30 True if value is 1, False if value is 0.
31 """
32 return bool(data[0])
34 def _encode_value(self, data: bool) -> bytearray:
35 """Encode boolean value back to bytes.
37 Args:
38 data: Boolean value to encode
40 Returns:
41 Encoded bytes (1 byte: 0x01 for True, 0x00 for False)
42 """
43 return bytearray([1 if data else 0])