2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ binary sensors."""
|
2021-11-16 19:01:10 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import ValuesView
|
|
|
|
|
2021-09-29 19:19:21 +00:00
|
|
|
from pydeconz.sensor import (
|
|
|
|
Alarm,
|
|
|
|
CarbonMonoxide,
|
2021-11-16 19:01:10 +00:00
|
|
|
DeconzBinarySensor as PydeconzBinarySensor,
|
|
|
|
DeconzSensor as PydeconzSensor,
|
2021-09-29 19:19:21 +00:00
|
|
|
Fire,
|
|
|
|
GenericFlag,
|
|
|
|
OpenClose,
|
|
|
|
Presence,
|
|
|
|
Vibration,
|
|
|
|
Water,
|
|
|
|
)
|
2020-06-02 14:17:21 +00:00
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
2020-09-25 20:49:28 +00:00
|
|
|
DOMAIN,
|
2021-12-09 12:23:01 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-06-02 14:17:21 +00:00
|
|
|
BinarySensorEntity,
|
2021-09-25 18:54:55 +00:00
|
|
|
BinarySensorEntityDescription,
|
2020-06-02 14:17:21 +00:00
|
|
|
)
|
2021-11-16 19:01:10 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-09 12:23:01 +00:00
|
|
|
from homeassistant.const import ATTR_TEMPERATURE
|
2021-11-16 19:01:10 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2018-05-05 14:11:00 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-12-09 12:23:01 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2021-11-16 19:01:10 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2021-10-07 10:48:27 +00:00
|
|
|
from .const import ATTR_DARK, ATTR_ON
|
2019-01-16 07:33:04 +00:00
|
|
|
from .deconz_device import DeconzDevice
|
2021-11-16 19:01:10 +00:00
|
|
|
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
2019-01-15 18:29:56 +00:00
|
|
|
|
2021-09-29 19:19:21 +00:00
|
|
|
DECONZ_BINARY_SENSORS = (
|
|
|
|
Alarm,
|
|
|
|
CarbonMonoxide,
|
|
|
|
Fire,
|
|
|
|
GenericFlag,
|
|
|
|
OpenClose,
|
|
|
|
Presence,
|
|
|
|
Vibration,
|
|
|
|
Water,
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2021-09-25 18:54:55 +00:00
|
|
|
ENTITY_DESCRIPTIONS = {
|
|
|
|
CarbonMonoxide: BinarySensorEntityDescription(
|
|
|
|
key="carbonmonoxide",
|
2021-12-09 12:23:01 +00:00
|
|
|
device_class=BinarySensorDeviceClass.GAS,
|
2021-09-25 18:54:55 +00:00
|
|
|
),
|
|
|
|
Fire: BinarySensorEntityDescription(
|
|
|
|
key="fire",
|
2021-12-09 12:23:01 +00:00
|
|
|
device_class=BinarySensorDeviceClass.SMOKE,
|
2021-09-25 18:54:55 +00:00
|
|
|
),
|
|
|
|
OpenClose: BinarySensorEntityDescription(
|
|
|
|
key="openclose",
|
2021-12-09 12:23:01 +00:00
|
|
|
device_class=BinarySensorDeviceClass.OPENING,
|
2021-09-25 18:54:55 +00:00
|
|
|
),
|
|
|
|
Presence: BinarySensorEntityDescription(
|
|
|
|
key="presence",
|
2021-12-09 12:23:01 +00:00
|
|
|
device_class=BinarySensorDeviceClass.MOTION,
|
2021-09-25 18:54:55 +00:00
|
|
|
),
|
|
|
|
Vibration: BinarySensorEntityDescription(
|
|
|
|
key="vibration",
|
2021-12-09 12:23:01 +00:00
|
|
|
device_class=BinarySensorDeviceClass.VIBRATION,
|
2021-09-25 18:54:55 +00:00
|
|
|
),
|
|
|
|
Water: BinarySensorEntityDescription(
|
|
|
|
key="water",
|
2021-12-09 12:23:01 +00:00
|
|
|
device_class=BinarySensorDeviceClass.MOISTURE,
|
2021-09-25 18:54:55 +00:00
|
|
|
),
|
2020-06-02 14:17:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2021-11-16 19:01:10 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
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
|
2021-11-16 19:01:10 +00:00
|
|
|
def async_add_sensor(
|
|
|
|
sensors: list[PydeconzSensor]
|
|
|
|
| ValuesView[PydeconzSensor] = gateway.api.sensors.values(),
|
|
|
|
) -> None:
|
2018-05-05 14:11:00 +00:00
|
|
|
"""Add binary sensor from deCONZ."""
|
2021-11-16 19:01:10 +00:00
|
|
|
entities: list[DeconzBinarySensor | DeconzTampering] = []
|
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
|
|
|
|
2021-09-29 19:19:21 +00:00
|
|
|
if not gateway.option_allow_clip_sensor and sensor.type.startswith("CLIP"):
|
|
|
|
continue
|
|
|
|
|
2020-02-05 00:37:01 +00:00
|
|
|
if (
|
2021-09-29 19:19:21 +00:00
|
|
|
isinstance(sensor, DECONZ_BINARY_SENSORS)
|
2021-09-18 07:05:08 +00:00
|
|
|
and sensor.unique_id not in gateway.entities[DOMAIN]
|
2020-02-05 00:37:01 +00:00
|
|
|
):
|
|
|
|
entities.append(DeconzBinarySensor(sensor, gateway))
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2021-04-28 18:16:06 +00: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 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
|
|
|
|
2021-04-20 18:20:57 +00:00
|
|
|
config_entry.async_on_unload(
|
2019-07-31 19:25:30 +00:00
|
|
|
async_dispatcher_connect(
|
2021-10-07 10:48:27 +00:00
|
|
|
hass,
|
|
|
|
gateway.signal_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
|
2021-11-16 19:01:10 +00:00
|
|
|
_device: PydeconzBinarySensor
|
2020-09-25 20:49:28 +00:00
|
|
|
|
2021-11-16 19:01:10 +00:00
|
|
|
def __init__(self, device: PydeconzBinarySensor, gateway: DeconzGateway) -> None:
|
2021-06-23 19:40:34 +00:00
|
|
|
"""Initialize deCONZ binary sensor."""
|
|
|
|
super().__init__(device, gateway)
|
2021-09-25 18:54:55 +00:00
|
|
|
|
|
|
|
if entity_description := ENTITY_DESCRIPTIONS.get(type(device)):
|
|
|
|
self.entity_description = entity_description
|
2021-06-23 19:40:34 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
@callback
|
2021-11-16 19:01:10 +00:00
|
|
|
def async_update_callback(self) -> None:
|
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"}
|
2021-10-20 12:59:36 +00:00
|
|
|
if self._device.changed_keys.intersection(keys):
|
|
|
|
super().async_update_callback()
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-16 19:01:10 +00:00
|
|
|
def is_on(self) -> bool:
|
2018-01-01 16:08:13 +00:00
|
|
|
"""Return true if sensor is on."""
|
2021-11-16 19:01:10 +00:00
|
|
|
return self._device.state # type: ignore[no-any-return]
|
2018-01-30 22:42:24 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
@property
|
2021-11-16 19:01:10 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, bool | float | int | list | None]:
|
2018-01-01 16:08:13 +00:00
|
|
|
"""Return the state attributes of the sensor."""
|
2021-11-16 19:01:10 +00:00
|
|
|
attr: dict[str, bool | float | int | list | None] = {}
|
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
|
|
|
|
|
2021-10-05 07:17:45 +00:00
|
|
|
if isinstance(self._device, Presence):
|
2020-01-29 06:40:42 +00:00
|
|
|
|
|
|
|
if self._device.dark is not None:
|
|
|
|
attr[ATTR_DARK] = self._device.dark
|
2019-05-27 04:56:00 +00:00
|
|
|
|
2021-10-05 07:17:45 +00:00
|
|
|
elif isinstance(self._device, Vibration):
|
2019-03-26 06:43:58 +00:00
|
|
|
attr[ATTR_ORIENTATION] = self._device.orientation
|
2021-09-18 07:05:08 +00:00
|
|
|
attr[ATTR_TILTANGLE] = self._device.tilt_angle
|
|
|
|
attr[ATTR_VIBRATIONSTRENGTH] = self._device.vibration_strength
|
2019-05-27 04:56:00 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
return attr
|
2021-04-28 18:16:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeconzTampering(DeconzDevice, BinarySensorEntity):
|
|
|
|
"""Representation of a deCONZ tampering sensor."""
|
|
|
|
|
|
|
|
TYPE = DOMAIN
|
2021-11-16 19:01:10 +00:00
|
|
|
_device: PydeconzSensor
|
2021-04-28 18:16:06 +00:00
|
|
|
|
2021-12-09 12:23:01 +00:00
|
|
|
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
|
|
|
_attr_device_class = BinarySensorDeviceClass.TAMPER
|
2021-05-31 08:50:11 +00:00
|
|
|
|
2021-11-16 19:01:10 +00:00
|
|
|
def __init__(self, device: PydeconzSensor, gateway: DeconzGateway) -> None:
|
2021-06-23 19:40:34 +00:00
|
|
|
"""Initialize deCONZ binary sensor."""
|
|
|
|
super().__init__(device, gateway)
|
|
|
|
|
|
|
|
self._attr_name = f"{self._device.name} Tampered"
|
|
|
|
|
2021-04-28 18:16:06 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return a unique identifier for this device."""
|
|
|
|
return f"{self.serial}-tampered"
|
|
|
|
|
|
|
|
@callback
|
2021-10-20 12:59:36 +00:00
|
|
|
def async_update_callback(self) -> None:
|
2021-04-28 18:16:06 +00:00
|
|
|
"""Update the sensor's state."""
|
|
|
|
keys = {"tampered", "reachable"}
|
2021-10-20 12:59:36 +00:00
|
|
|
if self._device.changed_keys.intersection(keys):
|
|
|
|
super().async_update_callback()
|
2021-04-28 18:16:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return the state of the sensor."""
|
2021-11-16 19:01:10 +00:00
|
|
|
return self._device.tampered # type: ignore[no-any-return]
|