diff --git a/homeassistant/components/zha/device_entity.py b/homeassistant/components/zha/device_entity.py index 94fe598b6ec..b3cb19f2c5a 100644 --- a/homeassistant/components/zha/device_entity.py +++ b/homeassistant/components/zha/device_entity.py @@ -1,6 +1,7 @@ """Device entity for Zigbee Home Automation.""" import logging +import numbers import time from homeassistant.core import callback @@ -101,6 +102,18 @@ class ZhaDeviceEntity(ZhaEntity): # only do this on add to HA because it is static await self._async_init_battery_values() + def async_update_state_attribute(self, key, value): + """Update a single device state attribute.""" + if key == 'battery_level': + if not isinstance(value, numbers.Number) or value == -1: + return + value = value / 2 + value = int(round(value)) + self._device_state_attributes.update({ + key: value + }) + self.async_schedule_update_ha_state() + async def async_update(self): """Handle polling.""" if self._zha_device.last_seen is None: