2019-04-03 15:40:03 +00:00
|
|
|
"""Binary sensors on Zigbee Home Automation networks."""
|
2017-04-25 05:24:57 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import DOMAIN, BinarySensorDevice
|
2019-03-10 04:09:09 +00:00
|
|
|
from homeassistant.const import STATE_ON
|
|
|
|
from homeassistant.core import callback
|
2018-12-04 10:38:57 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2019-01-26 13:54:49 +00:00
|
|
|
from .core.const import (
|
2019-02-19 17:58:22 +00:00
|
|
|
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,
|
2019-03-04 23:56:05 +00:00
|
|
|
ATTR_LEVEL, SENSOR_TYPE, ACCELERATION)
|
2019-01-26 13:54:49 +00:00
|
|
|
from .entity import ZhaEntity
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-10-24 16:59:52 +00:00
|
|
|
# Zigbee Cluster Library Zone Type to Home Assistant device class
|
2017-04-25 05:24:57 +00:00
|
|
|
CLASS_MAPPING = {
|
|
|
|
0x000d: 'motion',
|
|
|
|
0x0015: 'opening',
|
|
|
|
0x0028: 'smoke',
|
|
|
|
0x002a: 'moisture',
|
|
|
|
0x002b: 'gas',
|
|
|
|
0x002d: 'vibration',
|
|
|
|
}
|
2019-02-06 18:33:21 +00:00
|
|
|
|
|
|
|
|
2019-02-19 17:58:22 +00:00
|
|
|
async def get_ias_device_class(channel):
|
|
|
|
"""Get the HA device class from the channel."""
|
|
|
|
zone_type = await channel.get_attribute_value('zone_type')
|
2019-02-06 18:33:21 +00:00
|
|
|
return CLASS_MAPPING.get(zone_type)
|
|
|
|
|
|
|
|
|
|
|
|
DEVICE_CLASS_REGISTRY = {
|
|
|
|
UNKNOWN: None,
|
|
|
|
OPENING: OPENING,
|
|
|
|
ZONE: get_ias_device_class,
|
|
|
|
OCCUPANCY: OCCUPANCY,
|
2019-03-04 23:56:05 +00:00
|
|
|
ACCELERATION: 'moving',
|
2019-02-06 18:33:21 +00:00
|
|
|
}
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities,
|
2018-03-09 03:31:52 +00:00
|
|
|
discovery_info=None):
|
2018-11-27 20:21:25 +00:00
|
|
|
"""Old way of setting up Zigbee Home Automation binary sensors."""
|
|
|
|
pass
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-11-27 20:21:25 +00: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-25 05:24:57 +00:00
|
|
|
|
|
|
|
|
2018-11-27 20:21:25 +00: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:
|
2019-02-06 18:33:21 +00:00
|
|
|
entities.append(BinarySensor(**discovery_info))
|
2018-11-27 20:21:25 +00:00
|
|
|
|
|
|
|
async_add_entities(entities, update_before_add=True)
|
|
|
|
|
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
class BinarySensor(ZhaEntity, BinarySensorDevice):
|
|
|
|
"""ZHA BinarySensor."""
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
_domain = DOMAIN
|
2019-02-06 18:33:21 +00:00
|
|
|
_device_class = None
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
def __init__(self, **kwargs):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Initialize the ZHA binary sensor."""
|
2017-04-25 05:24:57 +00:00
|
|
|
super().__init__(**kwargs)
|
2019-02-06 18:33:21 +00:00
|
|
|
self._device_state_attributes = {}
|
2019-02-19 17:58:22 +00:00
|
|
|
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)
|
2019-02-06 18:33:21 +00:00
|
|
|
self._zha_sensor_type = kwargs[SENSOR_TYPE]
|
|
|
|
self._level = None
|
|
|
|
|
|
|
|
async def _determine_device_class(self):
|
|
|
|
"""Determine the device class for this binary sensor."""
|
|
|
|
device_class_supplier = DEVICE_CLASS_REGISTRY.get(
|
|
|
|
self._zha_sensor_type)
|
|
|
|
if callable(device_class_supplier):
|
2019-02-19 17:58:22 +00:00
|
|
|
channel = self.cluster_channels.get(self._zha_sensor_type)
|
|
|
|
if channel is None:
|
2019-02-06 18:33:21 +00:00
|
|
|
return None
|
2019-02-19 17:58:22 +00:00
|
|
|
return await device_class_supplier(channel)
|
2019-02-06 18:33:21 +00:00
|
|
|
return device_class_supplier
|
2018-03-09 03:31:52 +00:00
|
|
|
|
2018-12-22 08:34:47 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Run when about to be added to hass."""
|
2019-02-06 18:33:21 +00:00
|
|
|
self._device_class = await self._determine_device_class()
|
2018-12-22 08:34:47 +00:00
|
|
|
await super().async_added_to_hass()
|
2019-02-19 17:58:22 +00:00
|
|
|
if self._level_channel:
|
2019-02-06 18:33:21 +00:00
|
|
|
await self.async_accept_signal(
|
2019-02-19 17:58:22 +00:00
|
|
|
self._level_channel, SIGNAL_SET_LEVEL, self.set_level)
|
2019-02-06 18:33:21 +00:00
|
|
|
await self.async_accept_signal(
|
2019-02-19 17:58:22 +00:00
|
|
|
self._level_channel, SIGNAL_MOVE_LEVEL, self.move_level)
|
|
|
|
if self._on_off_channel:
|
2019-02-06 18:33:21 +00:00
|
|
|
await self.async_accept_signal(
|
2019-02-19 17:58:22 +00:00
|
|
|
self._on_off_channel, SIGNAL_ATTR_UPDATED,
|
2019-02-06 18:33:21 +00:00
|
|
|
self.async_set_state)
|
2019-02-19 17:58:22 +00:00
|
|
|
if self._zone_channel:
|
2019-02-06 18:33:21 +00:00
|
|
|
await self.async_accept_signal(
|
2019-02-19 17:58:22 +00:00
|
|
|
self._zone_channel, SIGNAL_ATTR_UPDATED, self.async_set_state)
|
|
|
|
if self._attr_channel:
|
2019-02-06 18:33:21 +00:00
|
|
|
await self.async_accept_signal(
|
2019-02-19 17:58:22 +00:00
|
|
|
self._attr_channel, SIGNAL_ATTR_UPDATED, self.async_set_state)
|
2018-12-23 15:16:21 +00:00
|
|
|
|
2019-03-10 04:09:09 +00:00
|
|
|
@callback
|
|
|
|
def async_restore_last_state(self, last_state):
|
|
|
|
"""Restore previous state."""
|
|
|
|
super().async_restore_last_state(last_state)
|
|
|
|
self._state = last_state.state == STATE_ON
|
|
|
|
if 'level' in last_state.attributes:
|
|
|
|
self._level = last_state.attributes['level']
|
|
|
|
|
2018-04-30 06:31:27 +00:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
2019-02-06 18:33:21 +00:00
|
|
|
"""Return if the switch is on based on the statemachine."""
|
|
|
|
if self._state is None:
|
|
|
|
return False
|
2018-04-30 06:31:27 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
2019-02-06 18:33:21 +00:00
|
|
|
def device_class(self) -> str:
|
|
|
|
"""Return device class from component DEVICE_CLASSES."""
|
|
|
|
return self._device_class
|
2018-04-30 06:31:27 +00:00
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
def async_set_state(self, state):
|
|
|
|
"""Set the state."""
|
|
|
|
self._state = bool(state)
|
|
|
|
self.async_schedule_update_ha_state()
|
2018-12-19 13:52:20 +00:00
|
|
|
|
2018-04-30 06:31:27 +00:00
|
|
|
def move_level(self, change):
|
|
|
|
"""Increment the level, setting state if appropriate."""
|
2019-02-06 18:33:21 +00:00
|
|
|
level = self._level or 0
|
2018-04-30 06:31:27 +00:00
|
|
|
if not self._state and change > 0:
|
2019-02-06 18:33:21 +00:00
|
|
|
level = 0
|
|
|
|
self._level = min(254, max(0, level + change))
|
2018-04-30 06:31:27 +00:00
|
|
|
self._state = bool(self._level)
|
2018-05-01 12:55:25 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2018-04-30 06:31:27 +00:00
|
|
|
|
|
|
|
def set_level(self, level):
|
|
|
|
"""Set the level, setting state if appropriate."""
|
|
|
|
self._level = level
|
2019-02-06 18:33:21 +00:00
|
|
|
self._state = bool(level)
|
2018-05-01 12:55:25 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2018-04-30 06:31:27 +00:00
|
|
|
|
2018-12-22 19:53:15 +00:00
|
|
|
@property
|
2019-02-06 18:33:21 +00:00
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device state attributes."""
|
2019-02-19 17:58:22 +00:00
|
|
|
if self._level_channel is not None:
|
2019-02-06 18:33:21 +00:00
|
|
|
self._device_state_attributes.update({
|
|
|
|
ATTR_LEVEL: self._state and self._level or 0
|
|
|
|
})
|
|
|
|
return self._device_state_attributes
|
2019-03-10 04:09:09 +00:00
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Attempt to retrieve on off state from the binary sensor."""
|
|
|
|
await super().async_update()
|
|
|
|
if self._level_channel:
|
|
|
|
self._level = await self._level_channel.get_attribute_value(
|
|
|
|
'current_level')
|
|
|
|
if self._on_off_channel:
|
|
|
|
self._state = await self._on_off_channel.get_attribute_value(
|
|
|
|
'on_off')
|
|
|
|
if self._zone_channel:
|
|
|
|
value = await self._zone_channel.get_attribute_value(
|
|
|
|
'zone_status')
|
|
|
|
if value is not None:
|
|
|
|
self._state = value & 3
|
|
|
|
if self._attr_channel:
|
|
|
|
self._state = await self._attr_channel.get_attribute_value(
|
|
|
|
self._attr_channel.value_attribute)
|