2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ binary sensors."""
|
2018-01-01 16:08:13 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorDevice
|
|
|
|
from homeassistant.const import ATTR_BATTERY_LEVEL
|
|
|
|
from homeassistant.core import callback
|
2018-05-05 14:11:00 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-01-15 18:29:56 +00:00
|
|
|
from .const import (
|
2019-01-16 07:33:04 +00:00
|
|
|
ATTR_DARK, ATTR_ON, CONF_ALLOW_CLIP_SENSOR, DOMAIN as DECONZ_DOMAIN)
|
|
|
|
from .deconz_device import DeconzDevice
|
2019-01-15 18:29:56 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
DEPENDENCIES = ['deconz']
|
|
|
|
|
|
|
|
|
2019-02-13 20:21:14 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass, config, async_add_entities, discovery_info=None):
|
2018-04-23 16:00:16 +00:00
|
|
|
"""Old way of setting up deCONZ binary sensors."""
|
|
|
|
pass
|
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2018-04-23 16:00:16 +00:00
|
|
|
"""Set up the deCONZ binary sensor."""
|
2018-11-05 15:21:44 +00:00
|
|
|
gateway = hass.data[DECONZ_DOMAIN]
|
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
@callback
|
|
|
|
def async_add_sensor(sensors):
|
|
|
|
"""Add binary sensor from deCONZ."""
|
|
|
|
from pydeconz.sensor import DECONZ_BINARY_SENSOR
|
|
|
|
entities = []
|
2018-05-29 14:09:53 +00:00
|
|
|
allow_clip_sensor = config_entry.data.get(CONF_ALLOW_CLIP_SENSOR, True)
|
2018-05-05 14:11:00 +00:00
|
|
|
for sensor in sensors:
|
2018-05-29 14:09:53 +00:00
|
|
|
if sensor.type in DECONZ_BINARY_SENSOR and \
|
|
|
|
not (not allow_clip_sensor and sensor.type.startswith('CLIP')):
|
2018-11-05 15:21:44 +00:00
|
|
|
entities.append(DeconzBinarySensor(sensor, gateway))
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(entities, True)
|
2018-05-29 14:09:53 +00:00
|
|
|
|
2018-11-05 15:21:44 +00:00
|
|
|
gateway.listeners.append(
|
2018-05-05 14:11:00 +00:00
|
|
|
async_dispatcher_connect(hass, 'deconz_new_sensor', async_add_sensor))
|
|
|
|
|
2018-11-05 15:21:44 +00:00
|
|
|
async_add_sensor(gateway.api.sensors.values())
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
class DeconzBinarySensor(DeconzDevice, BinarySensorDevice):
|
|
|
|
"""Representation of a deCONZ binary sensor."""
|
2018-08-29 21:18:20 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
@callback
|
|
|
|
def async_update_callback(self, reason):
|
|
|
|
"""Update the sensor's state.
|
|
|
|
|
|
|
|
If reason is that state is updated,
|
|
|
|
or reachable has changed or battery has changed.
|
|
|
|
"""
|
|
|
|
if reason['state'] or \
|
|
|
|
'reachable' in reason['attr'] or \
|
2018-07-02 21:14:38 +00:00
|
|
|
'battery' in reason['attr'] or \
|
|
|
|
'on' in reason['attr']:
|
2018-01-01 16:08:13 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if sensor is on."""
|
2019-01-16 07:33:04 +00:00
|
|
|
return self._device.is_tripped
|
2018-01-30 22:42:24 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
2018-01-19 06:36:29 +00:00
|
|
|
"""Return the class of the sensor."""
|
2019-01-16 07:33:04 +00:00
|
|
|
return self._device.sensor_class
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend."""
|
2019-01-16 07:33:04 +00:00
|
|
|
return self._device.sensor_icon
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the sensor."""
|
|
|
|
from pydeconz.sensor import PRESENCE
|
2018-03-15 03:07:37 +00:00
|
|
|
attr = {}
|
2019-01-16 07:33:04 +00:00
|
|
|
if self._device.battery:
|
|
|
|
attr[ATTR_BATTERY_LEVEL] = self._device.battery
|
|
|
|
if self._device.on is not None:
|
|
|
|
attr[ATTR_ON] = self._device.on
|
|
|
|
if self._device.type in PRESENCE and self._device.dark is not None:
|
|
|
|
attr[ATTR_DARK] = self._device.dark
|
2018-01-01 16:08:13 +00:00
|
|
|
return attr
|