From 4d23d3abacb1ed73bc39812e31fe1fb3f5e951ca Mon Sep 17 00:00:00 2001 From: "Peter F. Patel-Schneider" Date: Mon, 27 Apr 2020 14:36:01 -0400 Subject: [PATCH] device: heuristic inference of battery level when charging and no discharging information available --- lib/logitech_receiver/status.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/logitech_receiver/status.py b/lib/logitech_receiver/status.py index 4fe3b6ee..06e85ad3 100644 --- a/lib/logitech_receiver/status.py +++ b/lib/logitech_receiver/status.py @@ -176,8 +176,16 @@ class DeviceStatus(dict): if level is None: # Some notifications may come with no battery level info, just - # charging state info, so assume the level is unchanged. - level = self.get(KEYS.BATTERY_LEVEL) + # charging state info, so do our best to infer a level (even if it is just the last level) + # It is not always possible to do this well + if status == _hidpp20.BATTERY_STATUS.full: + level = _hidpp10.BATTERY_APPOX.full + elif status in (_hidpp20.BATTERY_STATUS.almost_full, _hidpp20.BATTERY_STATUS.recharging): + level = _hidpp10.BATTERY_APPOX.good + elif status == _hidpp20.BATTERY_STATUS.slow_recharge: + level = _hidpp10.BATTERY_APPOX.low + else: + level = self.get(KEYS.BATTERY_LEVEL) else: assert isinstance(level, int) @@ -185,7 +193,8 @@ class DeviceStatus(dict): old_level, self[KEYS.BATTERY_LEVEL] = self.get(KEYS.BATTERY_LEVEL), level old_status, self[KEYS.BATTERY_STATUS] = self.get(KEYS.BATTERY_STATUS), status - charging = status in (_hidpp20.BATTERY_STATUS.recharging, _hidpp20.BATTERY_STATUS.slow_recharge) + charging = status in (_hidpp20.BATTERY_STATUS.recharging, _hidpp20.BATTERY_STATUS.almost_full, + _hidpp20.BATTERY_STATUS.full, _hidpp20.BATTERY_STATUS.slow_recharge) old_charging, self[KEYS.BATTERY_CHARGING] = self.get(KEYS.BATTERY_CHARGING), charging changed = old_level != level or old_status != status or old_charging != charging