From 93a0ef8e7fe0c1da6a43d1bc3b65f9c6cbfc35f8 Mon Sep 17 00:00:00 2001 From: Itay Avraham Date: Sat, 15 Aug 2026 20:36:04 +0300 Subject: [PATCH] - 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. --- lib/solaar/cli/show.py | 5 ++--- tests/solaar/cli/test_show_json.py | 16 +++++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/solaar/cli/show.py b/lib/solaar/cli/show.py index 5aa4283e..3559193f 100644 --- a/lib/solaar/cli/show.py +++ b/lib/solaar/cli/show.py @@ -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, } diff --git a/tests/solaar/cli/test_show_json.py b/tests/solaar/cli/test_show_json.py index 8efb90f6..46b8a619 100644 --- a/tests/solaar/cli/test_show_json.py +++ b/tests/solaar/cli/test_show_json.py @@ -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