Coverage for src / bluetooth_sig / gatt / characteristics / appearance.py: 100%
15 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"""Appearance characteristic implementation."""
3from __future__ import annotations
5from ...registry.core.appearance_values import appearance_values_registry
6from ...types.appearance import AppearanceData
7from ..context import CharacteristicContext
8from .base import BaseCharacteristic
9from .utils import DataParser
12class AppearanceCharacteristic(BaseCharacteristic[AppearanceData]):
13 """Appearance characteristic (0x2A01).
15 org.bluetooth.characteristic.gap.appearance
17 Appearance characteristic with human-readable device type information.
18 """
20 _manual_value_type = "AppearanceData"
21 expected_length = 2
23 def _decode_value(self, data: bytearray, ctx: CharacteristicContext | None = None) -> AppearanceData:
24 """Parse appearance value with human-readable info.
26 Args:
27 data: Raw bytearray from BLE characteristic (2 bytes).
28 ctx: Optional CharacteristicContext providing surrounding context (may be None).
30 Returns:
31 AppearanceData with raw value and optional human-readable info.
32 """
33 raw_value = DataParser.parse_int16(data, 0, signed=False)
34 appearance_info = appearance_values_registry.get_appearance_info(raw_value)
36 return AppearanceData(
37 raw_value=raw_value,
38 info=appearance_info,
39 )
41 def _encode_value(self, data: AppearanceData) -> bytearray:
42 """Encode appearance value back to bytes.
44 Args:
45 data: Appearance value as AppearanceData
47 Returns:
48 Encoded bytes representing the appearance
49 """
50 return DataParser.encode_int16(data.raw_value, signed=False)