Refactor ZHA listeners into channels (#21196)

* refactor listeners to channels

* update coveragerc
This commit is contained in:
David F. Mulcahey authored and Paulus Schoutsen committed 2019-02-19 09:58:22 -08:00
1 parent fe4a2b5b31
commit 3be8178035
28 files changed
+1037 -899

No files matched your search

+23 -23
View File
@@ -9,9 +9,9 @@ import logging
from homeassistant.components.binary_sensor import DOMAIN, BinarySensorDevice
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .core.const import (
DATA_ZHA, DATA_ZHA_DISPATCHERS, ZHA_DISCOVERY_NEW, LISTENER_ON_OFF,
LISTENER_LEVEL, LISTENER_ZONE, SIGNAL_ATTR_UPDATED, SIGNAL_MOVE_LEVEL,
SIGNAL_SET_LEVEL, LISTENER_ATTRIBUTE, UNKNOWN, OPENING, ZONE, OCCUPANCY,
DATA_ZHA, DATA_ZHA_DISPATCHERS, ZHA_DISCOVERY_NEW, ON_OFF_CHANNEL,
LEVEL_CHANNEL, ZONE_CHANNEL, SIGNAL_ATTR_UPDATED, SIGNAL_MOVE_LEVEL,
SIGNAL_SET_LEVEL, ATTRIBUTE_CHANNEL, UNKNOWN, OPENING, ZONE, OCCUPANCY,
ATTR_LEVEL, SENSOR_TYPE)
from .entity import ZhaEntity
@@ -30,9 +30,9 @@ CLASS_MAPPING = {
}
async def get_ias_device_class(listener):
"""Get the HA device class from the listener."""
zone_type = await listener.get_attribute_value('zone_type')
async def get_ias_device_class(channel):
"""Get the HA device class from the channel."""
zone_type = await channel.get_attribute_value('zone_type')
return CLASS_MAPPING.get(zone_type)
@@ -87,10 +87,10 @@ class BinarySensor(ZhaEntity, BinarySensorDevice):
"""Initialize the ZHA binary sensor."""
super().__init__(**kwargs)
self._device_state_attributes = {}
self._zone_listener = self.cluster_listeners.get(LISTENER_ZONE)
self._on_off_listener = self.cluster_listeners.get(LISTENER_ON_OFF)
self._level_listener = self.cluster_listeners.get(LISTENER_LEVEL)
self._attr_listener = self.cluster_listeners.get(LISTENER_ATTRIBUTE)
self._zone_channel = self.cluster_channels.get(ZONE_CHANNEL)
self._on_off_channel = self.cluster_channels.get(ON_OFF_CHANNEL)
self._level_channel = self.cluster_channels.get(LEVEL_CHANNEL)
self._attr_channel = self.cluster_channels.get(ATTRIBUTE_CHANNEL)
self._zha_sensor_type = kwargs[SENSOR_TYPE]
self._level = None
@@ -99,31 +99,31 @@ class BinarySensor(ZhaEntity, BinarySensorDevice):
device_class_supplier = DEVICE_CLASS_REGISTRY.get(
self._zha_sensor_type)
if callable(device_class_supplier):
listener = self.cluster_listeners.get(self._zha_sensor_type)
if listener is None:
channel = self.cluster_channels.get(self._zha_sensor_type)
if channel is None:
return None
return await device_class_supplier(listener)
return await device_class_supplier(channel)
return device_class_supplier
async def async_added_to_hass(self):
"""Run when about to be added to hass."""
self._device_class = await self._determine_device_class()
await super().async_added_to_hass()
if self._level_listener:
if self._level_channel:
await self.async_accept_signal(
self._level_listener, SIGNAL_SET_LEVEL, self.set_level)
self._level_channel, SIGNAL_SET_LEVEL, self.set_level)
await self.async_accept_signal(
self._level_listener, SIGNAL_MOVE_LEVEL, self.move_level)
if self._on_off_listener:
self._level_channel, SIGNAL_MOVE_LEVEL, self.move_level)
if self._on_off_channel:
await self.async_accept_signal(
self._on_off_listener, SIGNAL_ATTR_UPDATED,
self._on_off_channel, SIGNAL_ATTR_UPDATED,
self.async_set_state)
if self._zone_listener:
if self._zone_channel:
await self.async_accept_signal(
self._zone_listener, SIGNAL_ATTR_UPDATED, self.async_set_state)
if self._attr_listener:
self._zone_channel, SIGNAL_ATTR_UPDATED, self.async_set_state)
if self._attr_channel:
await self.async_accept_signal(
self._attr_listener, SIGNAL_ATTR_UPDATED, self.async_set_state)
self._attr_channel, SIGNAL_ATTR_UPDATED, self.async_set_state)
@property
def is_on(self) -> bool:
@@ -160,7 +160,7 @@ class BinarySensor(ZhaEntity, BinarySensorDevice):
@property
def device_state_attributes(self):
"""Return the device state attributes."""
if self._level_listener is not None:
if self._level_channel is not None:
self._device_state_attributes.update({
ATTR_LEVEL: self._state and self._level or 0
})