Coverage for src / bluetooth_sig / gatt / characteristics / magnetic_declination.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-18 11:17 +0000

1"""Magnetic Declination characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5from ...types.units import AngleUnit 

6from .base import BaseCharacteristic 

7from .templates import ScaledUint16Template 

8 

9 

10class MagneticDeclinationCharacteristic(BaseCharacteristic[float]): 

11 """Magnetic Declination characteristic (0x2A2C). 

12 

13 org.bluetooth.characteristic.magnetic_declination 

14 

15 Magnetic declination characteristic. 

16 

17 Represents the magnetic declination - the angle on the horizontal plane 

18 between the direction of True North (geographic) and the direction of 

19 Magnetic North, measured clockwise from True North to Magnetic North. 

20 """ 

21 

22 _template = ScaledUint16Template.from_letter_method(M=1, d=-2, b=0) 

23 

24 _characteristic_name: str = "Magnetic Declination" 

25 _manual_unit: str = AngleUnit.DEGREES.value # Override template's "units" default 

26 

27 # Template configuration 

28 resolution: float = 0.01 # 0.01 degree resolution 

29 

30 expected_type: type = float 

31 

32 def _encode_value(self, data: float) -> bytearray: 

33 """Encode magnetic declination value back to bytes. 

34 

35 Args: 

36 data: Magnetic declination in degrees 

37 

38 Returns: 

39 Encoded bytes representing the magnetic declination (uint16, 0.01 degrees resolution) 

40 

41 """ 

42 declination = float(data) 

43 

44 # Normalize to 0-360 range if needed (magnetic declination can be 0-360) 

45 declination = declination % 360.0 

46 

47 # Use template encoding after normalization 

48 return super()._encode_value(declination)