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

1"""Boolean characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5from ..context import CharacteristicContext 

6from .base import BaseCharacteristic 

7 

8 

9class BooleanCharacteristic(BaseCharacteristic[bool]): 

10 """Boolean characteristic (0x2AE2). 

11 

12 org.bluetooth.characteristic.boolean 

13 

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

15 """ 

16 

17 expected_length = 1 

18 

19 def _decode_value( 

20 self, data: bytearray, ctx: CharacteristicContext | None = None, *, validate: bool = True 

21 ) -> 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 validate: Whether to validate ranges (default True) 

28 

29 Returns: 

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

31 """ 

32 return bool(data[0]) 

33 

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

35 """Encode boolean value back to bytes. 

36 

37 Args: 

38 data: Boolean value to encode 

39 

40 Returns: 

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

42 """ 

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