add ZHA channel name property (#22218)

* Make channel name a property.

* Cleanup Zigbee channels.

Use zcl.Cluster.ep_attribute as default channel name.
This commit is contained in:
Alexei Chetroi
2019-03-21 10:40:12 -04:00
committed by Paulus Schoutsen
parent 2b02c0d0fc
commit 03855c18fc
7 changed files with 30 additions and 46 deletions

View File

@@ -83,9 +83,14 @@ class ChannelStatus(Enum):
class ZigbeeChannel:
"""Base channel for a Zigbee cluster."""
CHANNEL_NAME = None
def __init__(self, cluster, device):
"""Initialize ZigbeeChannel."""
self.name = 'channel_{}'.format(cluster.cluster_id)
self._channel_name = cluster.ep_attribute
if self.CHANNEL_NAME:
self._channel_name = self.CHANNEL_NAME
self._generic_id = 'channel_0x{:04x}'.format(cluster.cluster_id)
self._cluster = cluster
self._zha_device = device
self._unique_id = construct_unique_id(cluster)
@@ -96,6 +101,11 @@ class ZigbeeChannel:
self._status = ChannelStatus.CREATED
self._cluster.add_listener(self)
@property
def generic_id(self):
"""Return the generic id for this channel."""
return self._generic_id
@property
def unique_id(self):
"""Return the unique id for this channel."""
@@ -111,6 +121,11 @@ class ZigbeeChannel:
"""Return the device this channel is linked to."""
return self._zha_device
@property
def name(self) -> str:
"""Return friendly name."""
return self._channel_name
@property
def status(self):
"""Return the status of the channel."""
@@ -215,10 +230,11 @@ class ZigbeeChannel:
class AttributeListeningChannel(ZigbeeChannel):
"""Channel for attribute reports from the cluster."""
CHANNEL_NAME = ATTRIBUTE_CHANNEL
def __init__(self, cluster, device):
"""Initialize AttributeListeningChannel."""
super().__init__(cluster, device)
self.name = ATTRIBUTE_CHANNEL
attr = self._report_config[0].get('attr')
if isinstance(attr, str):
self.value_attribute = get_attr_id_by_name(self.cluster, attr)
@@ -340,10 +356,7 @@ class ZDOChannel:
class EventRelayChannel(ZigbeeChannel):
"""Event relay that can be attached to zigbee clusters."""
def __init__(self, cluster, device):
"""Initialize EventRelayChannel."""
super().__init__(cluster, device)
self.name = EVENT_RELAY_CHANNEL
CHANNEL_NAME = EVENT_RELAY_CHANNEL
@callback
def attribute_updated(self, attrid, value):

View File

@@ -11,8 +11,7 @@ from . import ZigbeeChannel, parse_and_log_command
from ..helpers import get_attr_id_by_name
from ..const import (
SIGNAL_ATTR_UPDATED, SIGNAL_MOVE_LEVEL, SIGNAL_SET_LEVEL,
SIGNAL_STATE_ATTR, BASIC_CHANNEL, ON_OFF_CHANNEL, LEVEL_CHANNEL,
POWER_CONFIGURATION_CHANNEL
SIGNAL_STATE_ATTR
)
_LOGGER = logging.getLogger(__name__)
@@ -26,7 +25,6 @@ class OnOffChannel(ZigbeeChannel):
def __init__(self, cluster, device):
"""Initialize OnOffChannel."""
super().__init__(cluster, device)
self.name = ON_OFF_CHANNEL
self._state = None
@callback
@@ -77,11 +75,6 @@ class LevelControlChannel(ZigbeeChannel):
CURRENT_LEVEL = 0
def __init__(self, cluster, device):
"""Initialize LevelControlChannel."""
super().__init__(cluster, device)
self.name = LEVEL_CHANNEL
@callback
def cluster_command(self, tsn, command_id, args):
"""Handle commands received to this cluster."""
@@ -149,7 +142,6 @@ class BasicChannel(ZigbeeChannel):
def __init__(self, cluster, device):
"""Initialize BasicChannel."""
super().__init__(cluster, device)
self.name = BASIC_CHANNEL
self._power_source = None
async def async_configure(self):
@@ -171,11 +163,6 @@ class BasicChannel(ZigbeeChannel):
class PowerConfigurationChannel(ZigbeeChannel):
"""Channel for the zigbee power configuration cluster."""
def __init__(self, cluster, device):
"""Initialize PowerConfigurationChannel."""
super().__init__(cluster, device)
self.name = POWER_CONFIGURATION_CHANNEL
@callback
def attribute_updated(self, attrid, value):
"""Handle attribute updates on this cluster."""

View File

@@ -15,18 +15,15 @@ _LOGGER = logging.getLogger(__name__)
class ElectricalMeasurementChannel(AttributeListeningChannel):
"""Channel that polls active power level."""
def __init__(self, cluster, device):
"""Initialize ElectricalMeasurementChannel."""
super().__init__(cluster, device)
self.name = ELECTRICAL_MEASUREMENT_CHANNEL
CHANNEL_NAME = ELECTRICAL_MEASUREMENT_CHANNEL
async def async_update(self):
"""Retrieve latest state."""
_LOGGER.debug("%s async_update", self.unique_id)
# This is a polling channel. Don't allow cache.
result = await self.get_attribute_value(
ELECTRICAL_MEASUREMENT_CHANNEL, from_cache=False)
result = await self.get_attribute_value('active_power',
from_cache=False)
async_dispatcher_send(
self._zha_device.hass,
"{}_{}".format(self.unique_id, SIGNAL_ATTR_UPDATED),
@@ -35,6 +32,5 @@ class ElectricalMeasurementChannel(AttributeListeningChannel):
async def async_initialize(self, from_cache):
"""Initialize channel."""
await self.get_attribute_value(
ELECTRICAL_MEASUREMENT_CHANNEL, from_cache=from_cache)
await self.get_attribute_value('active_power', from_cache=from_cache)
await super().async_initialize(from_cache)

View File

@@ -8,7 +8,7 @@ import logging
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_send
from . import ZigbeeChannel
from ..const import FAN_CHANNEL, SIGNAL_ATTR_UPDATED
from ..const import SIGNAL_ATTR_UPDATED
_LOGGER = logging.getLogger(__name__)
@@ -18,11 +18,6 @@ class FanChannel(ZigbeeChannel):
_value_attribute = 0
def __init__(self, cluster, device):
"""Initialize FanChannel."""
super().__init__(cluster, device)
self.name = FAN_CHANNEL
async def async_set_speed(self, value) -> None:
"""Set the speed of the fan."""
from zigpy.exceptions import DeliveryError

View File

@@ -6,7 +6,6 @@ https://home-assistant.io/components/zha/
"""
import logging
from . import ZigbeeChannel
from ..const import COLOR_CHANNEL
_LOGGER = logging.getLogger(__name__)
@@ -21,7 +20,6 @@ class ColorChannel(ZigbeeChannel):
def __init__(self, cluster, device):
"""Initialize ColorChannel."""
super().__init__(cluster, device)
self.name = COLOR_CHANNEL
self._color_capabilities = None
def get_color_capabilities(self):

View File

@@ -9,7 +9,7 @@ from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_send
from . import ZigbeeChannel
from ..helpers import bind_cluster
from ..const import SIGNAL_ATTR_UPDATED, ZONE_CHANNEL
from ..const import SIGNAL_ATTR_UPDATED
_LOGGER = logging.getLogger(__name__)
@@ -17,11 +17,6 @@ _LOGGER = logging.getLogger(__name__)
class IASZoneChannel(ZigbeeChannel):
"""Channel for the IASZone Zigbee cluster."""
def __init__(self, cluster, device):
"""Initialize IASZoneChannel."""
super().__init__(cluster, device)
self.name = ZONE_CHANNEL
@callback
def cluster_command(self, tsn, command_id, args):
"""Handle commands received to this cluster."""

View File

@@ -87,12 +87,12 @@ ZDO_CHANNEL = 'zdo'
ON_OFF_CHANNEL = 'on_off'
ATTRIBUTE_CHANNEL = 'attribute'
BASIC_CHANNEL = 'basic'
COLOR_CHANNEL = 'color'
COLOR_CHANNEL = 'light_color'
FAN_CHANNEL = 'fan'
LEVEL_CHANNEL = ATTR_LEVEL
ZONE_CHANNEL = 'zone'
ELECTRICAL_MEASUREMENT_CHANNEL = 'active_power'
POWER_CONFIGURATION_CHANNEL = 'battery'
ZONE_CHANNEL = 'ias_zone'
ELECTRICAL_MEASUREMENT_CHANNEL = 'electrical_measurement'
POWER_CONFIGURATION_CHANNEL = 'power'
EVENT_RELAY_CHANNEL = 'event_relay'
SIGNAL_ATTR_UPDATED = 'attribute_updated'