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

1"""Boolean characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5from ...types.gatt_enums import ValueType 

6from ..context import CharacteristicContext 

7from .base import BaseCharacteristic 

8 

9 

10class BooleanCharacteristic(BaseCharacteristic[bool]): 

11 """Boolean characteristic (0x2AE2). 

12 

13 org.bluetooth.characteristic.boolean 

14 

15 The Boolean characteristic is used to represent predefined Boolean values (0 or 1). 

16 """ 

17 

18 _manual_value_type = ValueType.BOOL 

19 expected_length = 1 

20 

21 def _decode_value(self, data: bytearray, ctx: CharacteristicContext | None = None) -> bool: 

22 """Parse boolean value. 

23 

24 Args: 

25 data: Raw bytearray from BLE characteristic (1 byte, validated by base class). 

26 ctx: Optional CharacteristicContext. 

27 

28 Returns: 

29 True if value is 1, False if value is 0. 

30 """ 

31 return bool(data[0]) 

32 

33 def _encode_value(self, data: bool) -> bytearray: 

34 """Encode boolean value back to bytes. 

35 

36 Args: 

37 data: Boolean value to encode 

38 

39 Returns: 

40 Encoded bytes (1 byte: 0x01 for True, 0x00 for False) 

41 """ 

42 return bytearray([1 if data else 0])