- Emit the enum member name (e.g. "SLOW_RECHARGE") as a stable identifier that consumers can match on, instead of a lowercased rendering.
- Guard against combined status flags that have no .name on Python < 3.11, and add a test for that case.
This commit is contained in:
parent
044f962f97
commit
93a0ef8e7f
|
|
@ -26,7 +26,6 @@ from logitech_receiver import receiver
|
|||
from logitech_receiver import settings_templates
|
||||
from logitech_receiver.common import LOGITECH_VENDOR_ID
|
||||
from logitech_receiver.common import BatteryLevelApproximation
|
||||
from logitech_receiver.common import BatteryStatus
|
||||
from logitech_receiver.common import NamedInt
|
||||
from logitech_receiver.common import strhex
|
||||
from logitech_receiver.device import CenturionReceiver
|
||||
|
|
@ -162,12 +161,12 @@ def _battery_json(battery):
|
|||
level_kind = "level"
|
||||
else:
|
||||
level_kind = None
|
||||
status = battery.status
|
||||
status = getattr(battery.status, "name", None)
|
||||
return {
|
||||
"level": int(level) if level is not None else None,
|
||||
"level_kind": level_kind,
|
||||
"next_level": int(battery.next_level) if battery.next_level is not None else None,
|
||||
"status": status.name.lower().replace("_", " ") if isinstance(status, BatteryStatus) else None,
|
||||
"status": status,
|
||||
"voltage": battery.voltage,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -128,7 +128,7 @@ def test_battery_json_percentage():
|
|||
"level": 55,
|
||||
"level_kind": "level",
|
||||
"next_level": None,
|
||||
"status": "discharging",
|
||||
"status": "DISCHARGING",
|
||||
"voltage": 3800,
|
||||
}
|
||||
|
||||
|
|
@ -149,10 +149,10 @@ def test_battery_json_full_approximation():
|
|||
@pytest.mark.parametrize(
|
||||
"status, expected",
|
||||
[
|
||||
(BatteryStatus.DISCHARGING, "discharging"),
|
||||
(BatteryStatus.RECHARGING, "recharging"),
|
||||
(BatteryStatus.ALMOST_FULL, "almost full"),
|
||||
(BatteryStatus.SLOW_RECHARGE, "slow recharge"),
|
||||
(BatteryStatus.DISCHARGING, "DISCHARGING"),
|
||||
(BatteryStatus.RECHARGING, "RECHARGING"),
|
||||
(BatteryStatus.ALMOST_FULL, "ALMOST_FULL"),
|
||||
(BatteryStatus.SLOW_RECHARGE, "SLOW_RECHARGE"),
|
||||
],
|
||||
)
|
||||
def test_battery_json_status_names(status, expected):
|
||||
|
|
@ -163,6 +163,12 @@ def test_battery_json_no_status():
|
|||
assert _battery_json(_battery(50, None))["status"] is None
|
||||
|
||||
|
||||
def test_battery_json_combined_status_flag():
|
||||
combined = BatteryStatus(0x07) # bits from multiple flags (no canonical name on Python < 3.11)
|
||||
status = _battery_json(_battery(50, combined))["status"]
|
||||
assert status is None or isinstance(status, str)
|
||||
|
||||
|
||||
def test_receiver_json_none():
|
||||
assert _receiver_json(None) is None
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue