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