diff --git a/lib/logitech/unifying_receiver/hidpp20.py b/lib/logitech/unifying_receiver/hidpp20.py index d59ebdc7..42da666b 100644 --- a/lib/logitech/unifying_receiver/hidpp20.py +++ b/lib/logitech/unifying_receiver/hidpp20.py @@ -59,7 +59,7 @@ FIRMWARE_KIND = _NamedInts( Hardware=0x02, Other=0x03) -BATTERY_OK = lambda status: status < 5 +BATTERY_OK = lambda status: status not in ("invalid_battery", "thermal_error") BATTERY_STATUS = _NamedInts( discharging=0x00, diff --git a/lib/logitech/unifying_receiver/status.py b/lib/logitech/unifying_receiver/status.py index 0d4cfda8..df5c5c64 100644 --- a/lib/logitech/unifying_receiver/status.py +++ b/lib/logitech/unifying_receiver/status.py @@ -125,6 +125,24 @@ class DeviceStatus(dict): return bool(self._active) __nonzero__ = __bool__ + def set_battery_info(self, level, status): + self[BATTERY_LEVEL] = level + self[BATTERY_STATUS] = status + error = None + if _hidpp20.BATTERY_OK(status): + alert = ALERT.NONE + reason = self[ERROR] = None + if _log.isEnabledFor(_DEBUG): + _log.debug("%s: battery %d%% charged, %s", self._device, level, status) + else: + alert = ALERT.ALL + error = status + _log.warn("%s: battery %d%% charged, ALERT %s", self._device, level, error) + if error is not None: + # TODO: show visual warning/notif to user + self[ERROR] = error + self._changed(alert=alert, reason=error) + def read_battery(self, timestamp=None): d = self._device if d and self._active: @@ -294,18 +312,7 @@ class DeviceStatus(dict): if n.address == 0x00: discharge = ord(n.data[:1]) battery_status = ord(n.data[1:2]) - self[BATTERY_LEVEL] = discharge - self[BATTERY_STATUS] = BATTERY_STATUS[battery_status] - if _hidpp20.BATTERY_OK(battery_status): - alert = ALERT.NONE - reason = self[ERROR] = None - if _log.isEnabledFor(_DEBUG): - _log.debug("%s: battery %d%% charged, %s", self._device, discharge, self[BATTERY_STATUS]) - else: - alert = ALERT.ALL - reason = self[ERROR] = self[BATTERY_STATUS] - _log.warn("%s: battery %d%% charged, ALERT %s", self._device, discharge, reason) - self._changed(alert=alert, reason=reason) + self.set_battery_info(discharge, _hidpp20.BATTERY_STATUS[battery_status]) else: _log.info("%s: unknown BATTERY %s", self._device, n) return True