Coverage for src / bluetooth_sig / gatt / characteristics / cycling_power_feature.py: 97%
35 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"""Cycling Power Feature characteristic implementation."""
3from __future__ import annotations
5from enum import IntFlag
7import msgspec
9from ..context import CharacteristicContext
10from .base import BaseCharacteristic
11from .utils import DataParser
14class CyclingPowerFeatures(IntFlag):
15 """Cycling Power Feature flags as per Bluetooth SIG specification."""
17 PEDAL_POWER_BALANCE_SUPPORTED = 0x01
18 ACCUMULATED_ENERGY_SUPPORTED = 0x02
19 WHEEL_REVOLUTION_DATA_SUPPORTED = 0x04
20 CRANK_REVOLUTION_DATA_SUPPORTED = 0x08
23class CyclingPowerFeatureData(msgspec.Struct, frozen=True, kw_only=True): # pylint: disable=too-few-public-methods
24 """Parsed data from Cycling Power Feature characteristic."""
26 features: CyclingPowerFeatures
27 pedal_power_balance_supported: bool
28 accumulated_energy_supported: bool
29 wheel_revolution_data_supported: bool
30 crank_revolution_data_supported: bool
33class CyclingPowerFeatureCharacteristic(BaseCharacteristic[CyclingPowerFeatureData]):
34 """Cycling Power Feature characteristic (0x2A65).
36 Used to expose the supported features of a cycling power sensor.
37 Contains a 32-bit bitmask indicating supported measurement
38 capabilities.
39 """
41 expected_length: int = 4
43 def _decode_value(self, data: bytearray, ctx: CharacteristicContext | None = None) -> CyclingPowerFeatureData:
44 """Parse cycling power feature data.
46 Format: 32-bit feature bitmask (little endian).
48 Args:
49 data: Raw bytearray from BLE characteristic.
50 ctx: Optional CharacteristicContext providing surrounding context (may be None).
52 Returns:
53 CyclingPowerFeatureData containing parsed feature flags.
55 Raises:
56 ValueError: If data format is invalid.
58 """
59 if len(data) < 4:
60 raise ValueError("Cycling Power Feature data must be at least 4 bytes")
62 # Parse 32-bit unsigned integer (little endian)
63 feature_mask: int = DataParser.parse_int32(data, 0, signed=False)
65 # Parse feature flags according to specification
66 return CyclingPowerFeatureData(
67 features=CyclingPowerFeatures(feature_mask),
68 pedal_power_balance_supported=bool(feature_mask & CyclingPowerFeatures.PEDAL_POWER_BALANCE_SUPPORTED),
69 accumulated_energy_supported=bool(feature_mask & CyclingPowerFeatures.ACCUMULATED_ENERGY_SUPPORTED),
70 wheel_revolution_data_supported=bool(feature_mask & CyclingPowerFeatures.WHEEL_REVOLUTION_DATA_SUPPORTED),
71 crank_revolution_data_supported=bool(feature_mask & CyclingPowerFeatures.CRANK_REVOLUTION_DATA_SUPPORTED),
72 )
74 def _encode_value(self, data: CyclingPowerFeatureData) -> bytearray:
75 """Encode cycling power feature value back to bytes.
77 Args:
78 data: CyclingPowerFeatureData containing cycling power feature data
80 Returns:
81 Encoded bytes representing the cycling power features (uint32)
83 """
84 # Reconstruct the features bitmap from individual flags
85 features_bitmap = 0
86 if data.pedal_power_balance_supported:
87 features_bitmap |= CyclingPowerFeatures.PEDAL_POWER_BALANCE_SUPPORTED
88 if data.accumulated_energy_supported:
89 features_bitmap |= CyclingPowerFeatures.ACCUMULATED_ENERGY_SUPPORTED
90 if data.wheel_revolution_data_supported:
91 features_bitmap |= CyclingPowerFeatures.WHEEL_REVOLUTION_DATA_SUPPORTED
92 if data.crank_revolution_data_supported:
93 features_bitmap |= CyclingPowerFeatures.CRANK_REVOLUTION_DATA_SUPPORTED
95 return DataParser.encode_int32(features_bitmap, signed=False)