ZHA entity ZCL reporting configuration (#19177)

* Implement async_configure() method for ZHA entities.

Allow attribute reporting configuration to be stored as dict of zha
entity.

* Update ZHA platform to use new attribute reporting configuration.

* Use const declaration instead of magic numbers.

* Add support for manufacturer_id in ZCL attribute reporting configuration.

* Refactor async_configure() method.

Rename attribute reporting dict to zcl_reporting_config.
This commit is contained in:
Alexei Chetroi 2018-12-19 08:52:20 -05:00 committed by Paulus Schoutsen
parent 23a579421d
commit 4692605974
8 changed files with 214 additions and 42 deletions

View file

@ -9,7 +9,8 @@ import logging
from homeassistant.components.sensor import DOMAIN
from homeassistant.components.zha import helpers
from homeassistant.components.zha.const import (
DATA_ZHA, DATA_ZHA_DISPATCHERS, ZHA_DISCOVERY_NEW)
DATA_ZHA, DATA_ZHA_DISPATCHERS, REPORT_CONFIG_MAX_INT,
REPORT_CONFIG_MIN_INT, REPORT_CONFIG_RPT_CHANGE, ZHA_DISCOVERY_NEW)
from homeassistant.components.zha.entities import ZhaEntity
from homeassistant.const import TEMP_CELSIUS
from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -81,11 +82,7 @@ async def make_sensor(discovery_info):
sensor = Sensor(**discovery_info)
if discovery_info['new_join']:
cluster = list(in_clusters.values())[0]
await helpers.configure_reporting(
sensor.entity_id, cluster, sensor.value_attribute,
reportable_change=sensor.min_reportable_change
)
await sensor.async_configure()
return sensor
@ -95,7 +92,28 @@ class Sensor(ZhaEntity):
_domain = DOMAIN
value_attribute = 0
min_reportable_change = 1
min_report_interval = REPORT_CONFIG_MIN_INT
max_report_interval = REPORT_CONFIG_MAX_INT
min_reportable_change = REPORT_CONFIG_RPT_CHANGE
report_config = (min_report_interval, max_report_interval,
min_reportable_change)
def __init__(self, **kwargs):
"""Init ZHA Sensor instance."""
super().__init__(**kwargs)
self._cluster = list(kwargs['in_clusters'].values())[0]
@property
def zcl_reporting_config(self) -> dict:
"""Return a dict of attribute reporting configuration."""
return {
self.cluster: {self.value_attribute: self.report_config}
}
@property
def cluster(self):
"""Return Sensor's cluster."""
return self._cluster
@property
def should_poll(self) -> bool:
@ -119,7 +137,7 @@ class Sensor(ZhaEntity):
async def async_update(self):
"""Retrieve latest state."""
result = await helpers.safe_read(
list(self._in_clusters.values())[0],
self.cluster,
[self.value_attribute],
allow_cache=False,
only_cache=(not self._initialized)
@ -251,6 +269,6 @@ class ElectricalMeasurementSensor(Sensor):
_LOGGER.debug("%s async_update", self.entity_id)
result = await helpers.safe_read(
self._endpoint.electrical_measurement, ['active_power'],
self.cluster, ['active_power'],
allow_cache=False, only_cache=(not self._initialized))
self._state = result.get('active_power', self._state)