2019-02-13 21:21:14 +01:00
|
|
|
"""Support for deCONZ binary sensors."""
|
2024-03-08 14:15:26 +01:00
|
|
|
|
2021-11-16 20:01:10 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2022-10-10 21:18:26 +02:00
|
|
|
from collections.abc import Callable
|
|
|
|
|
from dataclasses import dataclass
|
2021-11-16 20:01:10 +01:00
|
|
|
|
2022-04-23 21:59:51 +02:00
|
|
|
from pydeconz.interfaces.sensors import SensorResources
|
2022-05-17 00:04:57 +02:00
|
|
|
from pydeconz.models.event import EventType
|
2022-08-29 08:15:10 +02:00
|
|
|
from pydeconz.models.sensor import SensorBase as PydeconzSensorBase
|
2022-04-23 21:59:51 +02:00
|
|
|
from pydeconz.models.sensor.alarm import Alarm
|
|
|
|
|
from pydeconz.models.sensor.carbon_monoxide import CarbonMonoxide
|
|
|
|
|
from pydeconz.models.sensor.fire import Fire
|
|
|
|
|
from pydeconz.models.sensor.generic_flag import GenericFlag
|
|
|
|
|
from pydeconz.models.sensor.open_close import OpenClose
|
|
|
|
|
from pydeconz.models.sensor.presence import Presence
|
|
|
|
|
from pydeconz.models.sensor.vibration import Vibration
|
|
|
|
|
from pydeconz.models.sensor.water import Water
|
2019-05-27 06:56:00 +02:00
|
|
|
|
2020-06-02 16:17:21 +02:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2020-09-25 22:49:28 +02:00
|
|
|
DOMAIN,
|
2021-12-09 13:23:01 +01:00
|
|
|
BinarySensorDeviceClass,
|
2020-06-02 16:17:21 +02:00
|
|
|
BinarySensorEntity,
|
2022-10-10 21:18:26 +02:00
|
|
|
BinarySensorEntityDescription,
|
2020-06-02 16:17:21 +02:00
|
|
|
)
|
2021-11-16 20:01:10 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-02-09 20:15:37 +01:00
|
|
|
from homeassistant.const import ATTR_TEMPERATURE, EntityCategory
|
2021-11-16 20:01:10 +01:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2024-03-15 12:05:45 +01:00
|
|
|
from .const import ATTR_DARK, ATTR_ON
|
2019-01-16 08:33:04 +01:00
|
|
|
from .deconz_device import DeconzDevice
|
2024-03-18 22:08:06 +01:00
|
|
|
from .hub import DeconzHub
|
2019-01-15 19:29:56 +01:00
|
|
|
|
2019-03-26 07:43:58 +01:00
|
|
|
ATTR_ORIENTATION = "orientation"
|
|
|
|
|
ATTR_TILTANGLE = "tiltangle"
|
|
|
|
|
ATTR_VIBRATIONSTRENGTH = "vibrationstrength"
|
|
|
|
|
|
2022-02-04 12:58:07 +01:00
|
|
|
PROVIDES_EXTRA_ATTRIBUTES = (
|
|
|
|
|
"alarm",
|
|
|
|
|
"carbon_monoxide",
|
|
|
|
|
"fire",
|
|
|
|
|
"flag",
|
|
|
|
|
"open",
|
|
|
|
|
"presence",
|
|
|
|
|
"vibration",
|
|
|
|
|
"water",
|
|
|
|
|
)
|
|
|
|
|
|
2022-10-10 21:18:26 +02:00
|
|
|
|
2023-12-19 03:28:13 +01:00
|
|
|
@dataclass(frozen=True, kw_only=True)
|
2024-05-20 12:01:49 +02:00
|
|
|
class DeconzBinarySensorDescription[
|
|
|
|
|
_T: (
|
|
|
|
|
Alarm,
|
|
|
|
|
CarbonMonoxide,
|
|
|
|
|
Fire,
|
|
|
|
|
GenericFlag,
|
|
|
|
|
OpenClose,
|
|
|
|
|
Presence,
|
|
|
|
|
Vibration,
|
|
|
|
|
Water,
|
|
|
|
|
PydeconzSensorBase,
|
|
|
|
|
)
|
|
|
|
|
](BinarySensorEntityDescription):
|
2022-10-10 21:18:26 +02:00
|
|
|
"""Class describing deCONZ binary sensor entities."""
|
|
|
|
|
|
2024-05-20 12:01:49 +02:00
|
|
|
instance_check: type[_T] | None = None
|
2022-10-10 21:18:26 +02:00
|
|
|
name_suffix: str = ""
|
|
|
|
|
old_unique_id_suffix: str = ""
|
2023-11-16 16:05:29 +01:00
|
|
|
update_key: str
|
2024-05-20 12:01:49 +02:00
|
|
|
value_fn: Callable[[_T], bool | None]
|
2022-10-10 21:18:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
ENTITY_DESCRIPTIONS: tuple[DeconzBinarySensorDescription, ...] = (
|
|
|
|
|
DeconzBinarySensorDescription[Alarm](
|
|
|
|
|
key="alarm",
|
|
|
|
|
update_key="alarm",
|
|
|
|
|
value_fn=lambda device: device.alarm,
|
|
|
|
|
instance_check=Alarm,
|
|
|
|
|
device_class=BinarySensorDeviceClass.SAFETY,
|
|
|
|
|
),
|
|
|
|
|
DeconzBinarySensorDescription[CarbonMonoxide](
|
|
|
|
|
key="carbon_monoxide",
|
|
|
|
|
update_key="carbonmonoxide",
|
|
|
|
|
value_fn=lambda device: device.carbon_monoxide,
|
|
|
|
|
instance_check=CarbonMonoxide,
|
|
|
|
|
device_class=BinarySensorDeviceClass.CO,
|
|
|
|
|
),
|
|
|
|
|
DeconzBinarySensorDescription[Fire](
|
|
|
|
|
key="fire",
|
|
|
|
|
update_key="fire",
|
|
|
|
|
value_fn=lambda device: device.fire,
|
|
|
|
|
instance_check=Fire,
|
|
|
|
|
device_class=BinarySensorDeviceClass.SMOKE,
|
|
|
|
|
),
|
|
|
|
|
DeconzBinarySensorDescription[Fire](
|
|
|
|
|
key="in_test_mode",
|
|
|
|
|
update_key="test",
|
|
|
|
|
value_fn=lambda device: device.in_test_mode,
|
|
|
|
|
instance_check=Fire,
|
|
|
|
|
name_suffix="Test Mode",
|
|
|
|
|
old_unique_id_suffix="test mode",
|
|
|
|
|
device_class=BinarySensorDeviceClass.SMOKE,
|
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
|
),
|
|
|
|
|
DeconzBinarySensorDescription[GenericFlag](
|
|
|
|
|
key="flag",
|
|
|
|
|
update_key="flag",
|
|
|
|
|
value_fn=lambda device: device.flag,
|
|
|
|
|
instance_check=GenericFlag,
|
|
|
|
|
),
|
|
|
|
|
DeconzBinarySensorDescription[OpenClose](
|
|
|
|
|
key="open",
|
|
|
|
|
update_key="open",
|
|
|
|
|
value_fn=lambda device: device.open,
|
|
|
|
|
instance_check=OpenClose,
|
|
|
|
|
device_class=BinarySensorDeviceClass.OPENING,
|
|
|
|
|
),
|
|
|
|
|
DeconzBinarySensorDescription[Presence](
|
|
|
|
|
key="presence",
|
|
|
|
|
update_key="presence",
|
|
|
|
|
value_fn=lambda device: device.presence,
|
|
|
|
|
instance_check=Presence,
|
|
|
|
|
device_class=BinarySensorDeviceClass.MOTION,
|
|
|
|
|
),
|
|
|
|
|
DeconzBinarySensorDescription[Vibration](
|
|
|
|
|
key="vibration",
|
|
|
|
|
update_key="vibration",
|
|
|
|
|
value_fn=lambda device: device.vibration,
|
|
|
|
|
instance_check=Vibration,
|
|
|
|
|
device_class=BinarySensorDeviceClass.VIBRATION,
|
|
|
|
|
),
|
|
|
|
|
DeconzBinarySensorDescription[Water](
|
|
|
|
|
key="water",
|
|
|
|
|
update_key="water",
|
|
|
|
|
value_fn=lambda device: device.water,
|
|
|
|
|
instance_check=Water,
|
|
|
|
|
device_class=BinarySensorDeviceClass.MOISTURE,
|
|
|
|
|
),
|
|
|
|
|
DeconzBinarySensorDescription[SensorResources](
|
|
|
|
|
key="tampered",
|
|
|
|
|
update_key="tampered",
|
|
|
|
|
value_fn=lambda device: device.tampered,
|
|
|
|
|
name_suffix="Tampered",
|
|
|
|
|
old_unique_id_suffix="tampered",
|
|
|
|
|
device_class=BinarySensorDeviceClass.TAMPER,
|
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
|
),
|
|
|
|
|
DeconzBinarySensorDescription[SensorResources](
|
|
|
|
|
key="low_battery",
|
|
|
|
|
update_key="lowbattery",
|
|
|
|
|
value_fn=lambda device: device.low_battery,
|
|
|
|
|
name_suffix="Low Battery",
|
|
|
|
|
old_unique_id_suffix="low battery",
|
|
|
|
|
device_class=BinarySensorDeviceClass.BATTERY,
|
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
2022-01-17 22:35:20 +01:00
|
|
|
|
2021-11-16 20:01:10 +01:00
|
|
|
async def async_setup_entry(
|
|
|
|
|
hass: HomeAssistant,
|
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
|
) -> None:
|
2018-04-23 18:00:16 +02:00
|
|
|
"""Set up the deCONZ binary sensor."""
|
2024-03-18 22:08:06 +01:00
|
|
|
hub = DeconzHub.get_hub(hass, config_entry)
|
|
|
|
|
hub.entities[DOMAIN] = set()
|
2018-11-05 16:21:44 +01:00
|
|
|
|
2018-05-05 16:11:00 +02:00
|
|
|
@callback
|
2022-05-17 00:04:57 +02:00
|
|
|
def async_add_sensor(_: EventType, sensor_id: str) -> None:
|
|
|
|
|
"""Add sensor from deCONZ."""
|
2024-03-18 22:08:06 +01:00
|
|
|
sensor = hub.api.sensors[sensor_id]
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2022-10-10 21:18:26 +02:00
|
|
|
for description in ENTITY_DESCRIPTIONS:
|
2022-05-17 00:04:57 +02:00
|
|
|
if (
|
2022-10-10 21:18:26 +02:00
|
|
|
description.instance_check
|
|
|
|
|
and not isinstance(sensor, description.instance_check)
|
|
|
|
|
) or description.value_fn(sensor) is None:
|
2021-09-29 21:19:21 +02:00
|
|
|
continue
|
2024-03-18 22:08:06 +01:00
|
|
|
async_add_entities([DeconzBinarySensor(sensor, hub, description)])
|
2022-01-17 22:35:20 +01:00
|
|
|
|
2024-03-18 22:08:06 +01:00
|
|
|
hub.register_platform_add_device_callback(
|
2022-05-25 05:48:09 +02:00
|
|
|
async_add_sensor,
|
2024-03-18 22:08:06 +01:00
|
|
|
hub.api.sensors,
|
2022-05-17 00:04:57 +02:00
|
|
|
)
|
2022-01-17 22:35:20 +01:00
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2022-10-10 21:18:26 +02:00
|
|
|
class DeconzBinarySensor(DeconzDevice[SensorResources], 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
|
2022-10-10 21:18:26 +02:00
|
|
|
entity_description: DeconzBinarySensorDescription
|
2020-09-25 22:49:28 +02:00
|
|
|
|
2022-10-10 21:18:26 +02:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
device: SensorResources,
|
2024-03-18 22:08:06 +01:00
|
|
|
hub: DeconzHub,
|
2022-10-10 21:18:26 +02:00
|
|
|
description: DeconzBinarySensorDescription,
|
|
|
|
|
) -> None:
|
2021-06-23 21:40:34 +02:00
|
|
|
"""Initialize deCONZ binary sensor."""
|
2022-10-10 21:18:26 +02:00
|
|
|
self.entity_description = description
|
|
|
|
|
self.unique_id_suffix = description.key
|
|
|
|
|
self._update_key = description.update_key
|
|
|
|
|
if description.name_suffix:
|
|
|
|
|
self._name_suffix = description.name_suffix
|
2024-03-18 22:08:06 +01:00
|
|
|
super().__init__(device, hub)
|
2021-09-25 20:54:55 +02:00
|
|
|
|
2022-08-29 08:15:10 +02:00
|
|
|
if (
|
2022-10-10 21:18:26 +02:00
|
|
|
self.entity_description.key in PROVIDES_EXTRA_ATTRIBUTES
|
2022-08-29 08:15:10 +02:00
|
|
|
and self._update_keys is not None
|
|
|
|
|
):
|
2022-02-04 12:58:07 +01:00
|
|
|
self._update_keys.update({"on", "state"})
|
|
|
|
|
|
2022-10-10 21:18:26 +02:00
|
|
|
@property
|
|
|
|
|
def is_on(self) -> bool | None:
|
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
|
return self.entity_description.value_fn(self._device)
|
|
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
@property
|
2021-11-16 20:01:10 +01:00
|
|
|
def extra_state_attributes(self) -> dict[str, bool | float | int | list | None]:
|
2018-01-01 17:08:13 +01:00
|
|
|
"""Return the state attributes of the sensor."""
|
2021-11-16 20:01:10 +01:00
|
|
|
attr: dict[str, bool | float | int | list | None] = {}
|
2019-05-27 06:56:00 +02:00
|
|
|
|
2022-10-10 21:18:26 +02:00
|
|
|
if self.entity_description.key not in PROVIDES_EXTRA_ATTRIBUTES:
|
2022-03-14 19:34:05 +01:00
|
|
|
return attr
|
|
|
|
|
|
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
|
|
|
|
2022-08-06 01:34:27 +02:00
|
|
|
if self._device.internal_temperature is not None:
|
|
|
|
|
attr[ATTR_TEMPERATURE] = self._device.internal_temperature
|
2019-05-27 06:56:00 +02:00
|
|
|
|
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
|