Add battery voltage state attribute for ZHA devices (#30901)

* Add battery voltage state attribute for ZHA devices.

* Pylint.
This commit is contained in:
Alexei Chetroi 2020-01-17 19:43:02 -05:00 committed by David F. Mulcahey
parent 4d7c8e254b
commit 58520aa733
2 changed files with 16 additions and 1 deletions

View File

@ -22,6 +22,7 @@ from ..const import (
SIGNAL_ATTR_UPDATED,
SIGNAL_MOVE_LEVEL,
SIGNAL_SET_LEVEL,
SIGNAL_STATE_ATTR,
)
from ..helpers import get_attr_id_by_name
@ -355,6 +356,14 @@ class PowerConfigurationChannel(ZigbeeChannel):
async_dispatcher_send(
self._zha_device.hass, f"{self.unique_id}_{SIGNAL_ATTR_UPDATED}", value
)
return
attr_name = self.cluster.attributes.get(attrid, [attrid])[0]
async_dispatcher_send(
self._zha_device.hass,
f"{self.unique_id}_{SIGNAL_STATE_ATTR}",
attr_name,
value,
)
async def async_initialize(self, from_cache):
"""Initialize channel."""

View File

@ -128,7 +128,7 @@ class Sensor(ZhaEntity):
async def async_added_to_hass(self):
"""Run when about to be added to hass."""
await super().async_added_to_hass()
self._device_state_attributes = await self.async_state_attr_provider()
self._device_state_attributes.update(await self.async_state_attr_provider())
await self.async_accept_signal(
self._channel, SIGNAL_ATTR_UPDATED, self.async_set_state
@ -209,6 +209,12 @@ class Battery(Sensor):
state_attrs["battery_quantity"] = battery_quantity
return state_attrs
def async_update_state_attribute(self, key, value):
"""Update a single device state attribute."""
if key == "battery_voltage":
self._device_state_attributes["voltage"] = f"{round(value/10, 1)}V"
self.async_schedule_update_ha_state()
@STRICT_MATCH(channel_names=CHANNEL_ELECTRICAL_MEASUREMENT)
class ElectricalMeasurement(Sensor):