Coverage for src / bluetooth_sig / types / battery.py: 76%

50 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-11 20:14 +0000

1"""Battery-related enumerations for power state characteristics. 

2 

3Defines enums for battery charge states, charge levels, charging types, 

4and fault reasons to replace string usage with type-safe alternatives. 

5""" 

6 

7from __future__ import annotations 

8 

9from enum import IntEnum 

10 

11 

12class PowerConnectionState(IntEnum): 

13 """Power connection state enumeration.""" 

14 

15 NO = 0 

16 YES = 1 

17 UNKNOWN = 2 

18 RFU = 3 

19 

20 

21class BatteryChargeState(IntEnum): 

22 """Battery charge state enumeration.""" 

23 

24 UNKNOWN = 0 

25 CHARGING = 1 

26 DISCHARGING = 2 

27 NOT_CHARGING = 3 

28 

29 @classmethod 

30 def from_byte(cls, byte_val: int) -> BatteryChargeState: 

31 """Create enum from byte value with fallback.""" 

32 try: 

33 return cls(byte_val) 

34 except ValueError: 

35 return cls.UNKNOWN 

36 

37 

38class BatteryChargeLevel(IntEnum): 

39 """Battery charge level enumeration.""" 

40 

41 UNKNOWN = 0 

42 GOOD = 1 

43 LOW = 2 

44 CRITICALLY_LOW = 3 

45 

46 @classmethod 

47 def from_byte(cls, byte_val: int) -> BatteryChargeLevel: 

48 """Create enum from byte value with fallback.""" 

49 try: 

50 return cls(byte_val) 

51 except ValueError: 

52 return cls.UNKNOWN 

53 

54 

55class BatteryChargingType(IntEnum): 

56 """Battery charging type enumeration.""" 

57 

58 UNKNOWN = 0 

59 CONSTANT_CURRENT = 1 

60 CONSTANT_VOLTAGE = 2 

61 TRICKLE = 3 

62 FLOAT = 4 

63 

64 @classmethod 

65 def from_byte(cls, byte_val: int) -> BatteryChargingType: 

66 """Create enum from byte value with fallback.""" 

67 try: 

68 return cls(byte_val) 

69 except ValueError: 

70 return cls.UNKNOWN 

71 

72 

73class ServiceRequiredState(IntEnum): 

74 """Service required state enumeration.""" 

75 

76 FALSE = 0 

77 TRUE = 1 

78 UNKNOWN = 2 

79 RFU = 3 

80 

81 

82class BatteryFaultReason(IntEnum): 

83 """Battery fault reason enumeration.""" 

84 

85 BATTERY_FAULT = 0 

86 EXTERNAL_POWER_FAULT = 1 

87 OTHER_FAULT = 2