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

35 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-30 00:10 +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 Enum 

10 

11 

12class BatteryChargeState(Enum): 

13 """Battery charge state enumeration.""" 

14 

15 UNKNOWN = "unknown" 

16 CHARGING = "charging" 

17 DISCHARGING = "discharging" 

18 NOT_CHARGING = "not_charging" 

19 

20 @classmethod 

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

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

23 mapping = { 

24 0: cls.UNKNOWN, 

25 1: cls.CHARGING, 

26 2: cls.DISCHARGING, 

27 3: cls.NOT_CHARGING, 

28 } 

29 return mapping.get(byte_val, cls.UNKNOWN) 

30 

31 

32class BatteryChargeLevel(Enum): 

33 """Battery charge level enumeration.""" 

34 

35 UNKNOWN = "unknown" 

36 GOOD = "good" 

37 LOW = "low" 

38 CRITICALLY_LOW = "critically_low" 

39 

40 @classmethod 

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

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

43 mapping = { 

44 0: cls.UNKNOWN, 

45 1: cls.GOOD, 

46 2: cls.LOW, 

47 3: cls.CRITICALLY_LOW, 

48 } 

49 return mapping.get(byte_val, cls.UNKNOWN) 

50 

51 

52class BatteryChargingType(Enum): 

53 """Battery charging type enumeration.""" 

54 

55 UNKNOWN = "unknown" 

56 CONSTANT_CURRENT = "constant_current" 

57 CONSTANT_VOLTAGE = "constant_voltage" 

58 TRICKLE = "trickle" 

59 FLOAT = "float" 

60 CONSTANT_POWER = "constant_power" 

61 

62 @classmethod 

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

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

65 mapping = { 

66 0: cls.UNKNOWN, 

67 1: cls.CONSTANT_CURRENT, 

68 2: cls.CONSTANT_VOLTAGE, 

69 3: cls.TRICKLE, 

70 4: cls.FLOAT, 

71 5: cls.CONSTANT_POWER, 

72 } 

73 return mapping.get(byte_val, cls.UNKNOWN) 

74 

75 

76class BatteryFaultReason(Enum): 

77 """Battery fault reason enumeration.""" 

78 

79 BATTERY_FAULT = "battery_fault" 

80 EXTERNAL_POWER_FAULT = "external_power_fault" 

81 OTHER_FAULT = "other_fault"