2017-04-24 22:24:57 -07:00
|
|
|
"""
|
|
|
|
Binary sensors on Zigbee Home Automation networks.
|
|
|
|
|
|
|
|
For more details on this platform, please refer to the documentation
|
|
|
|
at https://home-assistant.io/components/binary_sensor.zha/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import DOMAIN, BinarySensorDevice
|
2018-11-22 13:00:46 -05:00
|
|
|
from homeassistant.components.zha import helpers
|
2018-11-27 21:21:25 +01:00
|
|
|
from homeassistant.components.zha.const import (
|
2018-12-19 08:52:20 -05:00
|
|
|
DATA_ZHA, DATA_ZHA_DISPATCHERS, REPORT_CONFIG_IMMEDIATE, ZHA_DISCOVERY_NEW)
|
2018-12-04 05:38:57 -05:00
|
|
|
from homeassistant.components.zha.entities import ZhaEntity
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2017-04-24 22:24:57 -07:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEPENDENCIES = ['zha']
|
|
|
|
|
2018-10-24 18:59:52 +02:00
|
|
|
# Zigbee Cluster Library Zone Type to Home Assistant device class
|
2017-04-24 22:24:57 -07:00
|
|
|
CLASS_MAPPING = {
|
|
|
|
0x000d: 'motion',
|
|
|
|
0x0015: 'opening',
|
|
|
|
0x0028: 'smoke',
|
|
|
|
0x002a: 'moisture',
|
|
|
|
0x002b: 'gas',
|
|
|
|
0x002d: 'vibration',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities,
|
2018-03-08 22:31:52 -05:00
|
|
|
discovery_info=None):
|
2018-11-27 21:21:25 +01:00
|
|
|
"""Old way of setting up Zigbee Home Automation binary sensors."""
|
|
|
|
pass
|
2017-04-24 22:24:57 -07:00
|
|
|
|
2018-11-27 21:21:25 +01:00
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Zigbee Home Automation binary sensor from config entry."""
|
|
|
|
async def async_discover(discovery_info):
|
|
|
|
await _async_setup_entities(hass, config_entry, async_add_entities,
|
|
|
|
[discovery_info])
|
|
|
|
|
|
|
|
unsub = async_dispatcher_connect(
|
|
|
|
hass, ZHA_DISCOVERY_NEW.format(DOMAIN), async_discover)
|
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS].append(unsub)
|
|
|
|
|
|
|
|
binary_sensors = hass.data.get(DATA_ZHA, {}).get(DOMAIN)
|
|
|
|
if binary_sensors is not None:
|
|
|
|
await _async_setup_entities(hass, config_entry, async_add_entities,
|
|
|
|
binary_sensors.values())
|
|
|
|
del hass.data[DATA_ZHA][DOMAIN]
|
2017-04-24 22:24:57 -07:00
|
|
|
|
|
|
|
|
2018-11-27 21:21:25 +01:00
|
|
|
async def _async_setup_entities(hass, config_entry, async_add_entities,
|
|
|
|
discovery_infos):
|
|
|
|
"""Set up the ZHA binary sensors."""
|
|
|
|
entities = []
|
|
|
|
for discovery_info in discovery_infos:
|
|
|
|
from zigpy.zcl.clusters.general import OnOff
|
|
|
|
from zigpy.zcl.clusters.security import IasZone
|
|
|
|
if IasZone.cluster_id in discovery_info['in_clusters']:
|
|
|
|
entities.append(await _async_setup_iaszone(discovery_info))
|
|
|
|
elif OnOff.cluster_id in discovery_info['out_clusters']:
|
|
|
|
entities.append(await _async_setup_remote(discovery_info))
|
|
|
|
|
|
|
|
async_add_entities(entities, update_before_add=True)
|
|
|
|
|
|
|
|
|
|
|
|
async def _async_setup_iaszone(discovery_info):
|
2017-04-24 22:24:57 -07:00
|
|
|
device_class = None
|
2018-04-29 23:31:27 -07:00
|
|
|
from zigpy.zcl.clusters.security import IasZone
|
|
|
|
cluster = discovery_info['in_clusters'][IasZone.cluster_id]
|
2017-04-24 22:24:57 -07:00
|
|
|
if discovery_info['new_join']:
|
2018-03-08 22:31:52 -05:00
|
|
|
await cluster.bind()
|
2017-04-24 22:24:57 -07:00
|
|
|
ieee = cluster.endpoint.device.application.ieee
|
2018-03-08 22:31:52 -05:00
|
|
|
await cluster.write_attributes({'cie_addr': ieee})
|
2017-04-24 22:24:57 -07:00
|
|
|
|
|
|
|
try:
|
2018-03-08 22:31:52 -05:00
|
|
|
zone_type = await cluster['zone_type']
|
2017-04-24 22:24:57 -07:00
|
|
|
device_class = CLASS_MAPPING.get(zone_type, None)
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
# If we fail to read from the device, use a non-specific class
|
|
|
|
pass
|
|
|
|
|
2018-11-27 21:21:25 +01:00
|
|
|
return BinarySensor(device_class, **discovery_info)
|
2017-04-24 22:24:57 -07:00
|
|
|
|
2018-04-29 23:31:27 -07:00
|
|
|
|
2018-11-27 21:21:25 +01:00
|
|
|
async def _async_setup_remote(discovery_info):
|
2018-09-13 03:11:47 -04:00
|
|
|
remote = Remote(**discovery_info)
|
2018-04-29 23:31:27 -07:00
|
|
|
|
|
|
|
if discovery_info['new_join']:
|
2018-12-19 08:52:20 -05:00
|
|
|
await remote.async_configure()
|
2018-11-27 21:21:25 +01:00
|
|
|
return remote
|
2018-04-29 23:31:27 -07:00
|
|
|
|
|
|
|
|
2018-11-22 13:00:46 -05:00
|
|
|
class BinarySensor(ZhaEntity, BinarySensorDevice):
|
2018-04-29 23:31:27 -07:00
|
|
|
"""The ZHA Binary Sensor."""
|
2017-04-24 22:24:57 -07:00
|
|
|
|
|
|
|
_domain = DOMAIN
|
|
|
|
|
|
|
|
def __init__(self, device_class, **kwargs):
|
2017-04-30 07:04:49 +02:00
|
|
|
"""Initialize the ZHA binary sensor."""
|
2017-04-24 22:24:57 -07:00
|
|
|
super().__init__(**kwargs)
|
|
|
|
self._device_class = device_class
|
2018-02-05 16:05:19 -08:00
|
|
|
from zigpy.zcl.clusters.security import IasZone
|
2017-07-10 21:16:44 -07:00
|
|
|
self._ias_zone_cluster = self._in_clusters[IasZone.cluster_id]
|
2017-04-24 22:24:57 -07:00
|
|
|
|
2018-03-08 22:31:52 -05:00
|
|
|
@property
|
|
|
|
def should_poll(self) -> bool:
|
|
|
|
"""Let zha handle polling."""
|
|
|
|
return False
|
|
|
|
|
2017-04-24 22:24:57 -07:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return True if entity is on."""
|
2018-05-12 14:41:44 +02:00
|
|
|
if self._state is None:
|
2017-04-24 22:24:57 -07:00
|
|
|
return False
|
|
|
|
return bool(self._state)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this device, from component DEVICE_CLASSES."""
|
|
|
|
return self._device_class
|
|
|
|
|
2018-02-06 10:46:28 -08:00
|
|
|
def cluster_command(self, tsn, command_id, args):
|
2017-04-24 22:24:57 -07:00
|
|
|
"""Handle commands received to this cluster."""
|
|
|
|
if command_id == 0:
|
|
|
|
self._state = args[0] & 3
|
|
|
|
_LOGGER.debug("Updated alarm state: %s", self._state)
|
2018-03-08 22:31:52 -05:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-04-24 22:24:57 -07:00
|
|
|
elif command_id == 1:
|
|
|
|
_LOGGER.debug("Enroll requested")
|
2018-03-08 22:31:52 -05:00
|
|
|
res = self._ias_zone_cluster.enroll_response(0, 0)
|
|
|
|
self.hass.async_add_job(res)
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Retrieve latest state."""
|
2018-09-13 03:11:47 -04:00
|
|
|
from zigpy.types.basic import uint16_t
|
2018-03-08 22:31:52 -05:00
|
|
|
|
2018-11-22 13:00:46 -05:00
|
|
|
result = await helpers.safe_read(self._endpoint.ias_zone,
|
|
|
|
['zone_status'],
|
|
|
|
allow_cache=False,
|
|
|
|
only_cache=(not self._initialized))
|
2018-03-08 22:31:52 -05:00
|
|
|
state = result.get('zone_status', self._state)
|
|
|
|
if isinstance(state, (int, uint16_t)):
|
|
|
|
self._state = result.get('zone_status', self._state) & 3
|
2018-04-29 23:31:27 -07:00
|
|
|
|
|
|
|
|
2018-11-22 13:00:46 -05:00
|
|
|
class Remote(ZhaEntity, BinarySensorDevice):
|
2018-04-29 23:31:27 -07:00
|
|
|
"""ZHA switch/remote controller/button."""
|
|
|
|
|
|
|
|
_domain = DOMAIN
|
|
|
|
|
|
|
|
class OnOffListener:
|
2018-10-24 18:59:52 +02:00
|
|
|
"""Listener for the OnOff Zigbee cluster."""
|
2018-04-29 23:31:27 -07:00
|
|
|
|
|
|
|
def __init__(self, entity):
|
|
|
|
"""Initialize OnOffListener."""
|
|
|
|
self._entity = entity
|
|
|
|
|
|
|
|
def cluster_command(self, tsn, command_id, args):
|
|
|
|
"""Handle commands received to this cluster."""
|
|
|
|
if command_id in (0x0000, 0x0040):
|
|
|
|
self._entity.set_state(False)
|
|
|
|
elif command_id in (0x0001, 0x0041, 0x0042):
|
|
|
|
self._entity.set_state(True)
|
|
|
|
elif command_id == 0x0002:
|
|
|
|
self._entity.set_state(not self._entity.is_on)
|
|
|
|
|
|
|
|
def attribute_updated(self, attrid, value):
|
|
|
|
"""Handle attribute updates on this cluster."""
|
|
|
|
if attrid == 0:
|
|
|
|
self._entity.set_state(value)
|
|
|
|
|
|
|
|
def zdo_command(self, *args, **kwargs):
|
|
|
|
"""Handle ZDO commands on this cluster."""
|
|
|
|
pass
|
|
|
|
|
2018-12-10 07:59:50 -08:00
|
|
|
def zha_send_event(self, cluster, command, args):
|
|
|
|
"""Relay entity events to hass."""
|
|
|
|
pass # don't let entities fire events
|
|
|
|
|
2018-04-29 23:31:27 -07:00
|
|
|
class LevelListener:
|
2018-10-24 18:59:52 +02:00
|
|
|
"""Listener for the LevelControl Zigbee cluster."""
|
2018-04-29 23:31:27 -07:00
|
|
|
|
|
|
|
def __init__(self, entity):
|
|
|
|
"""Initialize LevelListener."""
|
|
|
|
self._entity = entity
|
|
|
|
|
|
|
|
def cluster_command(self, tsn, command_id, args):
|
|
|
|
"""Handle commands received to this cluster."""
|
|
|
|
if command_id in (0x0000, 0x0004): # move_to_level, -with_on_off
|
|
|
|
self._entity.set_level(args[0])
|
|
|
|
elif command_id in (0x0001, 0x0005): # move, -with_on_off
|
|
|
|
# We should dim slowly -- for now, just step once
|
|
|
|
rate = args[1]
|
|
|
|
if args[0] == 0xff:
|
|
|
|
rate = 10 # Should read default move rate
|
|
|
|
self._entity.move_level(-rate if args[0] else rate)
|
2018-06-07 16:56:07 -04:00
|
|
|
elif command_id in (0x0002, 0x0006): # step, -with_on_off
|
|
|
|
# Step (technically may change on/off)
|
2018-04-29 23:31:27 -07:00
|
|
|
self._entity.move_level(-args[1] if args[0] else args[1])
|
|
|
|
|
|
|
|
def attribute_update(self, attrid, value):
|
|
|
|
"""Handle attribute updates on this cluster."""
|
|
|
|
if attrid == 0:
|
|
|
|
self._entity.set_level(value)
|
|
|
|
|
|
|
|
def zdo_command(self, *args, **kwargs):
|
|
|
|
"""Handle ZDO commands on this cluster."""
|
|
|
|
pass
|
|
|
|
|
2018-12-10 07:59:50 -08:00
|
|
|
def zha_send_event(self, cluster, command, args):
|
|
|
|
"""Relay entity events to hass."""
|
|
|
|
pass # don't let entities fire events
|
|
|
|
|
2018-04-29 23:31:27 -07:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
"""Initialize Switch."""
|
2018-05-01 05:55:25 -07:00
|
|
|
super().__init__(**kwargs)
|
2018-05-21 01:14:18 +02:00
|
|
|
self._state = False
|
|
|
|
self._level = 0
|
2018-04-29 23:31:27 -07:00
|
|
|
from zigpy.zcl.clusters import general
|
|
|
|
self._out_listeners = {
|
|
|
|
general.OnOff.cluster_id: self.OnOffListener(self),
|
|
|
|
general.LevelControl.cluster_id: self.LevelListener(self),
|
|
|
|
}
|
2018-12-19 08:52:20 -05:00
|
|
|
out_clusters = kwargs.get('out_clusters')
|
|
|
|
self._zcl_reporting = {}
|
|
|
|
for cluster_id in [general.OnOff.cluster_id,
|
|
|
|
general.LevelControl.cluster_id]:
|
|
|
|
if cluster_id not in out_clusters:
|
|
|
|
continue
|
|
|
|
cluster = out_clusters[cluster_id]
|
|
|
|
self._zcl_reporting[cluster] = {0: REPORT_CONFIG_IMMEDIATE}
|
2018-04-29 23:31:27 -07:00
|
|
|
|
2018-05-20 19:00:51 -04:00
|
|
|
@property
|
|
|
|
def should_poll(self) -> bool:
|
|
|
|
"""Let zha handle polling."""
|
|
|
|
return False
|
|
|
|
|
2018-04-29 23:31:27 -07:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if the binary sensor is on."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device state attributes."""
|
2018-05-10 21:28:57 +02:00
|
|
|
self._device_state_attributes.update({
|
|
|
|
'level': self._state and self._level or 0
|
|
|
|
})
|
|
|
|
return self._device_state_attributes
|
2018-04-29 23:31:27 -07:00
|
|
|
|
2018-12-19 08:52:20 -05:00
|
|
|
@property
|
|
|
|
def zcl_reporting_config(self):
|
|
|
|
"""Return ZCL attribute reporting configuration."""
|
|
|
|
return self._zcl_reporting
|
|
|
|
|
2018-04-29 23:31:27 -07:00
|
|
|
def move_level(self, change):
|
|
|
|
"""Increment the level, setting state if appropriate."""
|
|
|
|
if not self._state and change > 0:
|
|
|
|
self._level = 0
|
|
|
|
self._level = min(255, max(0, self._level + change))
|
|
|
|
self._state = bool(self._level)
|
2018-05-01 05:55:25 -07:00
|
|
|
self.async_schedule_update_ha_state()
|
2018-04-29 23:31:27 -07:00
|
|
|
|
|
|
|
def set_level(self, level):
|
|
|
|
"""Set the level, setting state if appropriate."""
|
|
|
|
self._level = level
|
|
|
|
self._state = bool(self._level)
|
2018-05-01 05:55:25 -07:00
|
|
|
self.async_schedule_update_ha_state()
|
2018-04-29 23:31:27 -07:00
|
|
|
|
|
|
|
def set_state(self, state):
|
|
|
|
"""Set the state."""
|
|
|
|
self._state = state
|
|
|
|
if self._level == 0:
|
|
|
|
self._level = 255
|
2018-05-01 05:55:25 -07:00
|
|
|
self.async_schedule_update_ha_state()
|
2018-04-29 23:31:27 -07:00
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Retrieve latest state."""
|
|
|
|
from zigpy.zcl.clusters.general import OnOff
|
2018-11-22 13:00:46 -05:00
|
|
|
result = await helpers.safe_read(
|
2018-10-08 20:23:26 +02:00
|
|
|
self._endpoint.out_clusters[OnOff.cluster_id],
|
|
|
|
['on_off'],
|
|
|
|
allow_cache=False,
|
|
|
|
only_cache=(not self._initialized)
|
|
|
|
)
|
2018-04-29 23:31:27 -07:00
|
|
|
self._state = result.get('on_off', self._state)
|