2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ binary sensors."""
|
2020-06-02 14:17:21 +00:00
|
|
|
from pydeconz.sensor import CarbonMonoxide, Fire, OpenClose, Presence, Vibration, Water
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
DEVICE_CLASS_GAS,
|
|
|
|
DEVICE_CLASS_MOISTURE,
|
|
|
|
DEVICE_CLASS_MOTION,
|
|
|
|
DEVICE_CLASS_OPENING,
|
|
|
|
DEVICE_CLASS_SMOKE,
|
|
|
|
DEVICE_CLASS_VIBRATION,
|
2020-09-25 20:49:28 +00:00
|
|
|
DOMAIN,
|
2020-06-02 14:17:21 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2019-09-14 17:15:18 +00:00
|
|
|
from homeassistant.const import ATTR_TEMPERATURE
|
2018-01-01 16:08:13 +00:00
|
|
|
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-04-05 00:48:24 +00:00
|
|
|
from .const import ATTR_DARK, ATTR_ON, NEW_SENSOR
|
2019-01-16 07:33:04 +00:00
|
|
|
from .deconz_device import DeconzDevice
|
2020-02-05 00:37:01 +00:00
|
|
|
from .gateway import get_gateway_from_config_entry
|
2019-01-15 18:29:56 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ORIENTATION = "orientation"
|
|
|
|
ATTR_TILTANGLE = "tiltangle"
|
|
|
|
ATTR_VIBRATIONSTRENGTH = "vibrationstrength"
|
2019-03-26 06:43:58 +00:00
|
|
|
|
2020-06-02 14:17:21 +00:00
|
|
|
DEVICE_CLASS = {
|
|
|
|
CarbonMonoxide: DEVICE_CLASS_GAS,
|
|
|
|
Fire: DEVICE_CLASS_SMOKE,
|
|
|
|
OpenClose: DEVICE_CLASS_OPENING,
|
|
|
|
Presence: DEVICE_CLASS_MOTION,
|
|
|
|
Vibration: DEVICE_CLASS_VIBRATION,
|
|
|
|
Water: DEVICE_CLASS_MOISTURE,
|
|
|
|
}
|
|
|
|
|
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."""
|
2019-04-05 00:48:24 +00:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2020-09-25 20:49:28 +00:00
|
|
|
gateway.entities[DOMAIN] = set()
|
2018-11-05 15:21:44 +00:00
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
@callback
|
2020-12-02 15:21:27 +00:00
|
|
|
def async_add_sensor(sensors=gateway.api.sensors.values()):
|
2018-05-05 14:11:00 +00:00
|
|
|
"""Add binary sensor from deCONZ."""
|
|
|
|
entities = []
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
for sensor in sensors:
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2020-02-05 00:37:01 +00:00
|
|
|
if (
|
2020-09-25 20:49:28 +00:00
|
|
|
sensor.BINARY
|
|
|
|
and sensor.uniqueid not in gateway.entities[DOMAIN]
|
2020-02-05 00:37:01 +00:00
|
|
|
and (
|
|
|
|
gateway.option_allow_clip_sensor
|
|
|
|
or not sensor.type.startswith("CLIP")
|
|
|
|
)
|
|
|
|
):
|
|
|
|
entities.append(DeconzBinarySensor(sensor, gateway))
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2020-10-02 09:20:33 +00:00
|
|
|
if entities:
|
2020-10-16 15:14:26 +00:00
|
|
|
async_add_entities(entities)
|
2018-05-29 14:09:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
gateway.listeners.append(
|
|
|
|
async_dispatcher_connect(
|
2019-09-05 23:38:00 +00:00
|
|
|
hass, gateway.async_signal_new_device(NEW_SENSOR), async_add_sensor
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2018-05-05 14:11:00 +00:00
|
|
|
|
2020-01-08 08:30:02 +00:00
|
|
|
async_add_sensor(
|
|
|
|
[gateway.api.sensors[key] for key in sorted(gateway.api.sensors, key=int)]
|
|
|
|
)
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class DeconzBinarySensor(DeconzDevice, BinarySensorEntity):
|
2019-01-16 07:33:04 +00:00
|
|
|
"""Representation of a deCONZ binary sensor."""
|
2018-08-29 21:18:20 +00:00
|
|
|
|
2020-09-25 20:49:28 +00:00
|
|
|
TYPE = DOMAIN
|
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
@callback
|
2020-09-30 15:24:30 +00:00
|
|
|
def async_update_callback(self, force_update=False):
|
2019-05-27 04:56:00 +00:00
|
|
|
"""Update the sensor's state."""
|
2019-09-14 17:15:18 +00:00
|
|
|
keys = {"on", "reachable", "state"}
|
2020-01-17 23:33:46 +00:00
|
|
|
if force_update or self._device.changed_keys.intersection(keys):
|
2020-09-30 15:24:30 +00:00
|
|
|
super().async_update_callback(force_update=force_update)
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if sensor is on."""
|
2020-11-23 10:37:11 +00:00
|
|
|
return self._device.state
|
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."""
|
2020-06-02 14:17:21 +00:00
|
|
|
return DEVICE_CLASS.get(type(self._device))
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the sensor."""
|
2018-03-15 03:07:37 +00:00
|
|
|
attr = {}
|
2019-05-27 04:56:00 +00:00
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
if self._device.on is not None:
|
|
|
|
attr[ATTR_ON] = self._device.on
|
2019-05-27 04:56:00 +00:00
|
|
|
|
|
|
|
if self._device.secondary_temperature is not None:
|
|
|
|
attr[ATTR_TEMPERATURE] = self._device.secondary_temperature
|
|
|
|
|
2020-01-29 06:40:42 +00:00
|
|
|
if self._device.type in Presence.ZHATYPE:
|
|
|
|
|
|
|
|
if self._device.dark is not None:
|
|
|
|
attr[ATTR_DARK] = self._device.dark
|
2019-05-27 04:56:00 +00:00
|
|
|
|
|
|
|
elif self._device.type in Vibration.ZHATYPE:
|
2019-03-26 06:43:58 +00:00
|
|
|
attr[ATTR_ORIENTATION] = self._device.orientation
|
|
|
|
attr[ATTR_TILTANGLE] = self._device.tiltangle
|
|
|
|
attr[ATTR_VIBRATIONSTRENGTH] = self._device.vibrationstrength
|
2019-05-27 04:56:00 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
return attr
|