handle 0x07 battery notifications with indicating charging, but with no battery level

This commit is contained in:
Daniel Pavel 2013-06-29 20:26:29 +02:00
parent 1d305db29e
commit 1d438f098f
2 changed files with 23 additions and 6 deletions

View File

@ -163,10 +163,21 @@ def parse_battery_reply_07(level, battery_status):
else BATTERY_APPOX.low if level == 3 # low
else BATTERY_APPOX.critical if level == 1 # critical
else BATTERY_APPOX.empty ) # wtf?
status = ('charging' if battery_status == 0x21 or battery_status == 0x25
else 'fully charged' if battery_status == 0x22
else 'discharging' if battery_status == 0x00
else None)
if battery_status == 0x00:
status = 'discharging'
elif battery_status & 0x21 == 0x21:
status = 'charging'
elif battery_status & 0x22 == 0x22:
status = 'fully charged'
else:
_log.warn("could not parse 0x07 battery status: %02X (level %02X)", battery_status, level)
status = None
if battery_status & 0x03 and level == 0:
# some 'charging' notifications may come with no battery level information
charge = None
return charge, status

View File

@ -169,15 +169,21 @@ class DeviceStatus(dict):
__nonzero__ = __bool__
def set_battery_info(self, level, status, timestamp=None):
assert isinstance(level, int)
if _log.isEnabledFor(_DEBUG):
_log.debug("%s: battery %s, %s", self._device, level, status)
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[KEYS.BATTERY_LEVEL]
else:
assert isinstance(level, int)
# TODO: this is also executed when pressing Fn+F7 on K800.
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 ('charging', 'recharging', 'slow recharge')
charging = status in ('charging', 'fully charged', 'recharging', '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