From a79224aba8b939d6acdc705c5d1c00f06b7b6b65 Mon Sep 17 00:00:00 2001 From: "David F. Mulcahey" Date: Fri, 7 Jun 2019 11:02:35 -0400 Subject: [PATCH] Fix ZHA battery level when value is reported via signal (#24371) * fix battery signal * review comment --- homeassistant/components/zha/device_entity.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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: