Refactor battery info update
- `self[BATTERY_STATUS] = BATTERY_STATUS[battery_status]` should be: `self[BATTERY_STATUS] = _hidpp20.BATTERY_STATUS[battery_status]`, otherwise the battery status would be a single char from the string `battery-status`. - Make `_hidpp20.BATTERY_OK` check against strings instead of a number. - Move setting battery information to a separate function, `set_battery_info`. This prepares for notifications when a battery error/warning occurs.
This commit is contained in:
parent
d8e469a33a
commit
fbdd923d43
|
@ -59,7 +59,7 @@ FIRMWARE_KIND = _NamedInts(
|
||||||
Hardware=0x02,
|
Hardware=0x02,
|
||||||
Other=0x03)
|
Other=0x03)
|
||||||
|
|
||||||
BATTERY_OK = lambda status: status < 5
|
BATTERY_OK = lambda status: status not in ("invalid_battery", "thermal_error")
|
||||||
|
|
||||||
BATTERY_STATUS = _NamedInts(
|
BATTERY_STATUS = _NamedInts(
|
||||||
discharging=0x00,
|
discharging=0x00,
|
||||||
|
|
|
@ -125,6 +125,24 @@ class DeviceStatus(dict):
|
||||||
return bool(self._active)
|
return bool(self._active)
|
||||||
__nonzero__ = __bool__
|
__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):
|
def read_battery(self, timestamp=None):
|
||||||
d = self._device
|
d = self._device
|
||||||
if d and self._active:
|
if d and self._active:
|
||||||
|
@ -294,18 +312,7 @@ class DeviceStatus(dict):
|
||||||
if n.address == 0x00:
|
if n.address == 0x00:
|
||||||
discharge = ord(n.data[:1])
|
discharge = ord(n.data[:1])
|
||||||
battery_status = ord(n.data[1:2])
|
battery_status = ord(n.data[1:2])
|
||||||
self[BATTERY_LEVEL] = discharge
|
self.set_battery_info(discharge, _hidpp20.BATTERY_STATUS[battery_status])
|
||||||
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)
|
|
||||||
else:
|
else:
|
||||||
_log.info("%s: unknown BATTERY %s", self._device, n)
|
_log.info("%s: unknown BATTERY %s", self._device, n)
|
||||||
return True
|
return True
|
||||||
|
|
Loading…
Reference in New Issue