2019-02-13 21:21:14 +01:00
|
|
|
"""Support for deCONZ binary sensors."""
|
2021-09-29 21:19:21 +02:00
|
|
|
from pydeconz.sensor import (
|
|
|
|
Alarm,
|
|
|
|
CarbonMonoxide,
|
|
|
|
Fire,
|
|
|
|
GenericFlag,
|
|
|
|
OpenClose,
|
|
|
|
Presence,
|
|
|
|
Vibration,
|
|
|
|
Water,
|
|
|
|
)
|
2020-06-02 16:17:21 +02:00
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
DEVICE_CLASS_GAS,
|
|
|
|
DEVICE_CLASS_MOISTURE,
|
|
|
|
DEVICE_CLASS_MOTION,
|
|
|
|
DEVICE_CLASS_OPENING,
|
|
|
|
DEVICE_CLASS_SMOKE,
|
2021-10-17 17:45:32 +02:00
|
|
|
DEVICE_CLASS_TAMPER,
|
2020-06-02 16:17:21 +02:00
|
|
|
DEVICE_CLASS_VIBRATION,
|
2020-09-25 22:49:28 +02:00
|
|
|
DOMAIN,
|
2020-06-02 16:17:21 +02:00
|
|
|
BinarySensorEntity,
|
2021-09-25 20:54:55 +02:00
|
|
|
BinarySensorEntityDescription,
|
2020-06-02 16:17:21 +02:00
|
|
|
)
|
2021-10-20 20:24:11 +02:00
|
|
|
from homeassistant.const import ATTR_TEMPERATURE, ENTITY_CATEGORY_DIAGNOSTIC
|
2018-01-01 17:08:13 +01:00
|
|
|
from homeassistant.core import callback
|
2018-05-05 16:11:00 +02:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2021-10-07 12:48:27 +02:00
|
|
|
from .const import ATTR_DARK, ATTR_ON
|
2019-01-16 08:33:04 +01:00
|
|
|
from .deconz_device import DeconzDevice
|
2020-02-05 01:37:01 +01:00
|
|
|
from .gateway import get_gateway_from_config_entry
|
2019-01-15 19:29:56 +01:00
|
|
|
|
2021-09-29 21:19:21 +02:00
|
|
|
DECONZ_BINARY_SENSORS = (
|
|
|
|
Alarm,
|
|
|
|
CarbonMonoxide,
|
|
|
|
Fire,
|
|
|
|
GenericFlag,
|
|
|
|
OpenClose,
|
|
|
|
Presence,
|
|
|
|
Vibration,
|
|
|
|
Water,
|
|
|
|
)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_ORIENTATION = "orientation"
|
|
|
|
ATTR_TILTANGLE = "tiltangle"
|
|
|
|
ATTR_VIBRATIONSTRENGTH = "vibrationstrength"
|
2019-03-26 07:43:58 +01:00
|
|
|
|
2021-09-25 20:54:55 +02:00
|
|
|
ENTITY_DESCRIPTIONS = {
|
|
|
|
CarbonMonoxide: BinarySensorEntityDescription(
|
|
|
|
key="carbonmonoxide",
|
|
|
|
device_class=DEVICE_CLASS_GAS,
|
|
|
|
),
|
|
|
|
Fire: BinarySensorEntityDescription(
|
|
|
|
key="fire",
|
|
|
|
device_class=DEVICE_CLASS_SMOKE,
|
|
|
|
),
|
|
|
|
OpenClose: BinarySensorEntityDescription(
|
|
|
|
key="openclose",
|
|
|
|
device_class=DEVICE_CLASS_OPENING,
|
|
|
|
),
|
|
|
|
Presence: BinarySensorEntityDescription(
|
|
|
|
key="presence",
|
|
|
|
device_class=DEVICE_CLASS_MOTION,
|
|
|
|
),
|
|
|
|
Vibration: BinarySensorEntityDescription(
|
|
|
|
key="vibration",
|
|
|
|
device_class=DEVICE_CLASS_VIBRATION,
|
|
|
|
),
|
|
|
|
Water: BinarySensorEntityDescription(
|
|
|
|
key="water",
|
|
|
|
device_class=DEVICE_CLASS_MOISTURE,
|
|
|
|
),
|
2020-06-02 16:17:21 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2018-04-23 18:00:16 +02:00
|
|
|
"""Set up the deCONZ binary sensor."""
|
2019-04-05 02:48:24 +02:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2020-09-25 22:49:28 +02:00
|
|
|
gateway.entities[DOMAIN] = set()
|
2018-11-05 16:21:44 +01:00
|
|
|
|
2018-05-05 16:11:00 +02:00
|
|
|
@callback
|
2020-12-02 16:21:27 +01:00
|
|
|
def async_add_sensor(sensors=gateway.api.sensors.values()):
|
2018-05-05 16:11:00 +02:00
|
|
|
"""Add binary sensor from deCONZ."""
|
|
|
|
entities = []
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2018-05-05 16:11:00 +02:00
|
|
|
for sensor in sensors:
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2021-09-29 21:19:21 +02:00
|
|
|
if not gateway.option_allow_clip_sensor and sensor.type.startswith("CLIP"):
|
|
|
|
continue
|
|
|
|
|
2020-02-05 01:37:01 +01:00
|
|
|
if (
|
2021-09-29 21:19:21 +02:00
|
|
|
isinstance(sensor, DECONZ_BINARY_SENSORS)
|
2021-09-18 09:05:08 +02:00
|
|
|
and sensor.unique_id not in gateway.entities[DOMAIN]
|
2020-02-05 01:37:01 +01:00
|
|
|
):
|
|
|
|
entities.append(DeconzBinarySensor(sensor, gateway))
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2021-04-28 20:16:06 +02:00
|
|
|
if sensor.tampered is not None:
|
|
|
|
known_tampering_sensors = set(gateway.entities[DOMAIN])
|
|
|
|
new_tampering_sensor = DeconzTampering(sensor, gateway)
|
|
|
|
if new_tampering_sensor.unique_id not in known_tampering_sensors:
|
|
|
|
entities.append(new_tampering_sensor)
|
|
|
|
|
2020-10-02 11:20:33 +02:00
|
|
|
if entities:
|
2020-10-16 17:14:26 +02:00
|
|
|
async_add_entities(entities)
|
2018-05-29 16:09:53 +02:00
|
|
|
|
2021-04-20 20:20:57 +02:00
|
|
|
config_entry.async_on_unload(
|
2019-07-31 12:25:30 -07:00
|
|
|
async_dispatcher_connect(
|
2021-10-07 12:48:27 +02:00
|
|
|
hass,
|
|
|
|
gateway.signal_new_sensor,
|
|
|
|
async_add_sensor,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
|
|
|
)
|
2018-05-05 16:11:00 +02:00
|
|
|
|
2020-01-08 09:30:02 +01:00
|
|
|
async_add_sensor(
|
|
|
|
[gateway.api.sensors[key] for key in sorted(gateway.api.sensors, key=int)]
|
|
|
|
)
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
|
2020-04-23 21:57:07 +02:00
|
|
|
class DeconzBinarySensor(DeconzDevice, BinarySensorEntity):
|
2019-01-16 08:33:04 +01:00
|
|
|
"""Representation of a deCONZ binary sensor."""
|
2018-08-29 23:18:20 +02:00
|
|
|
|
2020-09-25 22:49:28 +02:00
|
|
|
TYPE = DOMAIN
|
|
|
|
|
2021-06-23 21:40:34 +02:00
|
|
|
def __init__(self, device, gateway):
|
|
|
|
"""Initialize deCONZ binary sensor."""
|
|
|
|
super().__init__(device, gateway)
|
2021-09-25 20:54:55 +02:00
|
|
|
|
|
|
|
if entity_description := ENTITY_DESCRIPTIONS.get(type(device)):
|
|
|
|
self.entity_description = entity_description
|
2021-06-23 21:40:34 +02:00
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
@callback
|
2021-10-20 14:59:36 +02:00
|
|
|
def async_update_callback(self):
|
2019-05-27 06:56:00 +02:00
|
|
|
"""Update the sensor's state."""
|
2019-09-14 19:15:18 +02:00
|
|
|
keys = {"on", "reachable", "state"}
|
2021-10-20 14:59:36 +02:00
|
|
|
if self._device.changed_keys.intersection(keys):
|
|
|
|
super().async_update_callback()
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if sensor is on."""
|
2020-11-23 11:37:11 +01:00
|
|
|
return self._device.state
|
2018-01-30 23:42:24 +01:00
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
@property
|
2021-03-11 16:51:03 +01:00
|
|
|
def extra_state_attributes(self):
|
2018-01-01 17:08:13 +01:00
|
|
|
"""Return the state attributes of the sensor."""
|
2018-03-15 04:07:37 +01:00
|
|
|
attr = {}
|
2019-05-27 06:56:00 +02:00
|
|
|
|
2019-01-16 08:33:04 +01:00
|
|
|
if self._device.on is not None:
|
|
|
|
attr[ATTR_ON] = self._device.on
|
2019-05-27 06:56:00 +02:00
|
|
|
|
|
|
|
if self._device.secondary_temperature is not None:
|
|
|
|
attr[ATTR_TEMPERATURE] = self._device.secondary_temperature
|
|
|
|
|
2021-10-05 09:17:45 +02:00
|
|
|
if isinstance(self._device, Presence):
|
2020-01-29 07:40:42 +01:00
|
|
|
|
|
|
|
if self._device.dark is not None:
|
|
|
|
attr[ATTR_DARK] = self._device.dark
|
2019-05-27 06:56:00 +02:00
|
|
|
|
2021-10-05 09:17:45 +02:00
|
|
|
elif isinstance(self._device, Vibration):
|
2019-03-26 07:43:58 +01:00
|
|
|
attr[ATTR_ORIENTATION] = self._device.orientation
|
2021-09-18 09:05:08 +02:00
|
|
|
attr[ATTR_TILTANGLE] = self._device.tilt_angle
|
|
|
|
attr[ATTR_VIBRATIONSTRENGTH] = self._device.vibration_strength
|
2019-05-27 06:56:00 +02:00
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
return attr
|
2021-04-28 20:16:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DeconzTampering(DeconzDevice, BinarySensorEntity):
|
|
|
|
"""Representation of a deCONZ tampering sensor."""
|
|
|
|
|
|
|
|
TYPE = DOMAIN
|
|
|
|
|
2021-10-20 20:24:11 +02:00
|
|
|
_attr_entity_category = ENTITY_CATEGORY_DIAGNOSTIC
|
2021-10-17 17:45:32 +02:00
|
|
|
_attr_device_class = DEVICE_CLASS_TAMPER
|
2021-05-31 10:50:11 +02:00
|
|
|
|
2021-06-23 21:40:34 +02:00
|
|
|
def __init__(self, device, gateway):
|
|
|
|
"""Initialize deCONZ binary sensor."""
|
|
|
|
super().__init__(device, gateway)
|
|
|
|
|
|
|
|
self._attr_name = f"{self._device.name} Tampered"
|
|
|
|
|
2021-04-28 20:16:06 +02:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return a unique identifier for this device."""
|
|
|
|
return f"{self.serial}-tampered"
|
|
|
|
|
|
|
|
@callback
|
2021-10-20 14:59:36 +02:00
|
|
|
def async_update_callback(self) -> None:
|
2021-04-28 20:16:06 +02:00
|
|
|
"""Update the sensor's state."""
|
|
|
|
keys = {"tampered", "reachable"}
|
2021-10-20 14:59:36 +02:00
|
|
|
if self._device.changed_keys.intersection(keys):
|
|
|
|
super().async_update_callback()
|
2021-04-28 20:16:06 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._device.tampered
|