2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Fritzbox binary sensors."""
|
2024-03-08 13:15:26 +00:00
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-29 12:06:51 +00:00
|
|
|
from collections.abc import Callable
|
2021-08-28 21:07:06 +00:00
|
|
|
from dataclasses import dataclass
|
2021-09-29 12:06:51 +00:00
|
|
|
from typing import Final
|
2021-08-28 21:07:06 +00:00
|
|
|
|
2021-08-27 15:09:34 +00:00
|
|
|
from pyfritzhome.fritzhomedevice import FritzhomeDevice
|
|
|
|
|
2021-08-28 21:07:06 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-10 13:58:23 +00:00
|
|
|
BinarySensorDeviceClass,
|
2021-08-28 21:07:06 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
BinarySensorEntityDescription,
|
|
|
|
)
|
2021-04-25 00:40:12 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-02-09 19:15:37 +00:00
|
|
|
from homeassistant.const import EntityCategory
|
2023-11-20 16:13:52 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2022-11-18 16:37:56 +00:00
|
|
|
from . import FritzBoxDeviceEntity
|
2023-11-20 16:13:52 +00:00
|
|
|
from .common import get_coordinator
|
2021-08-28 21:07:06 +00:00
|
|
|
from .model import FritzEntityDescriptionMixinBase
|
|
|
|
|
|
|
|
|
2023-12-19 02:28:13 +00:00
|
|
|
@dataclass(frozen=True)
|
2021-08-28 21:07:06 +00:00
|
|
|
class FritzEntityDescriptionMixinBinarySensor(FritzEntityDescriptionMixinBase):
|
|
|
|
"""BinarySensor description mixin for Fritz!Smarthome entities."""
|
|
|
|
|
|
|
|
is_on: Callable[[FritzhomeDevice], bool | None]
|
|
|
|
|
|
|
|
|
2023-12-19 02:28:13 +00:00
|
|
|
@dataclass(frozen=True)
|
2021-08-28 21:07:06 +00:00
|
|
|
class FritzBinarySensorEntityDescription(
|
|
|
|
BinarySensorEntityDescription, FritzEntityDescriptionMixinBinarySensor
|
|
|
|
):
|
|
|
|
"""Description for Fritz!Smarthome binary sensor entities."""
|
|
|
|
|
|
|
|
|
|
|
|
BINARY_SENSOR_TYPES: Final[tuple[FritzBinarySensorEntityDescription, ...]] = (
|
|
|
|
FritzBinarySensorEntityDescription(
|
|
|
|
key="alarm",
|
2023-04-03 17:04:09 +00:00
|
|
|
translation_key="alarm",
|
2021-12-10 13:58:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.WINDOW,
|
2023-06-21 14:12:51 +00:00
|
|
|
suitable=lambda device: device.has_alarm,
|
|
|
|
is_on=lambda device: device.alert_state,
|
2021-08-28 21:07:06 +00:00
|
|
|
),
|
2022-01-07 13:46:17 +00:00
|
|
|
FritzBinarySensorEntityDescription(
|
|
|
|
key="lock",
|
2023-04-03 17:04:09 +00:00
|
|
|
translation_key="lock",
|
2022-01-07 13:46:17 +00:00
|
|
|
device_class=BinarySensorDeviceClass.LOCK,
|
2023-11-05 21:49:08 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2022-01-07 13:46:17 +00:00
|
|
|
suitable=lambda device: device.lock is not None,
|
|
|
|
is_on=lambda device: not device.lock,
|
|
|
|
),
|
|
|
|
FritzBinarySensorEntityDescription(
|
|
|
|
key="device_lock",
|
2023-04-03 17:04:09 +00:00
|
|
|
translation_key="device_lock",
|
2022-01-07 13:46:17 +00:00
|
|
|
device_class=BinarySensorDeviceClass.LOCK,
|
2023-11-05 21:49:08 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2022-01-07 13:46:17 +00:00
|
|
|
suitable=lambda device: device.device_lock is not None,
|
|
|
|
is_on=lambda device: not device.device_lock,
|
|
|
|
),
|
2021-08-28 21:07:06 +00:00
|
|
|
)
|
2018-10-04 10:16:27 +00:00
|
|
|
|
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-30 18:38:59 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2021-04-25 00:40:12 +00:00
|
|
|
) -> None:
|
2021-05-03 10:52:22 +00:00
|
|
|
"""Set up the FRITZ!SmartHome binary sensor from ConfigEntry."""
|
2023-11-20 16:13:52 +00:00
|
|
|
coordinator = get_coordinator(hass, entry.entry_id)
|
|
|
|
|
|
|
|
@callback
|
2023-12-13 17:07:29 +00:00
|
|
|
def _add_entities(devices: set[str] | None = None) -> None:
|
2023-11-20 16:13:52 +00:00
|
|
|
"""Add devices."""
|
2023-12-13 17:07:29 +00:00
|
|
|
if devices is None:
|
|
|
|
devices = coordinator.new_devices
|
|
|
|
if not devices:
|
2023-11-20 16:13:52 +00:00
|
|
|
return
|
|
|
|
async_add_entities(
|
2023-11-20 18:02:02 +00:00
|
|
|
FritzboxBinarySensor(coordinator, ain, description)
|
2023-12-13 17:07:29 +00:00
|
|
|
for ain in devices
|
2023-11-20 18:02:02 +00:00
|
|
|
for description in BINARY_SENSOR_TYPES
|
|
|
|
if description.suitable(coordinator.data.devices[ain])
|
2023-11-20 16:13:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
entry.async_on_unload(coordinator.async_add_listener(_add_entities))
|
|
|
|
|
2023-12-16 18:38:58 +00:00
|
|
|
_add_entities(set(coordinator.data.devices))
|
2020-04-20 13:00:07 +00:00
|
|
|
|
2018-10-04 10:16:27 +00:00
|
|
|
|
2022-11-18 16:37:56 +00:00
|
|
|
class FritzboxBinarySensor(FritzBoxDeviceEntity, BinarySensorEntity):
|
2021-05-03 10:52:22 +00:00
|
|
|
"""Representation of a binary FRITZ!SmartHome device."""
|
2018-10-04 10:16:27 +00:00
|
|
|
|
2021-08-27 15:09:34 +00:00
|
|
|
entity_description: FritzBinarySensorEntityDescription
|
|
|
|
|
2018-10-04 10:16:27 +00:00
|
|
|
@property
|
2021-08-27 15:09:34 +00:00
|
|
|
def is_on(self) -> bool | None:
|
2018-10-04 10:16:27 +00:00
|
|
|
"""Return true if sensor is on."""
|
2022-11-19 14:12:24 +00:00
|
|
|
return self.entity_description.is_on(self.data)
|