2022-05-19 14:43:32 +00:00
|
|
|
"""YoLink BinarySensor."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
from yolink.device import YoLinkDevice
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
BinarySensorDeviceClass,
|
|
|
|
BinarySensorEntity,
|
|
|
|
BinarySensorEntityDescription,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2022-05-20 17:06:47 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_COORDINATOR,
|
|
|
|
ATTR_DEVICE_DOOR_SENSOR,
|
|
|
|
ATTR_DEVICE_LEAK_SENSOR,
|
|
|
|
ATTR_DEVICE_MOTION_SENSOR,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
2022-05-19 14:43:32 +00:00
|
|
|
from .coordinator import YoLinkCoordinator
|
|
|
|
from .entity import YoLinkEntity
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class YoLinkBinarySensorEntityDescription(BinarySensorEntityDescription):
|
|
|
|
"""YoLink BinarySensorEntityDescription."""
|
|
|
|
|
|
|
|
exists_fn: Callable[[YoLinkDevice], bool] = lambda _: True
|
2022-05-22 16:57:09 +00:00
|
|
|
state_key: str = "state"
|
2022-05-19 14:43:32 +00:00
|
|
|
value: Callable[[str], bool | None] = lambda _: None
|
|
|
|
|
|
|
|
|
2022-05-20 17:06:47 +00:00
|
|
|
SENSOR_DEVICE_TYPE = [
|
|
|
|
ATTR_DEVICE_DOOR_SENSOR,
|
|
|
|
ATTR_DEVICE_MOTION_SENSOR,
|
|
|
|
ATTR_DEVICE_LEAK_SENSOR,
|
|
|
|
]
|
2022-05-19 14:43:32 +00:00
|
|
|
|
|
|
|
SENSOR_TYPES: tuple[YoLinkBinarySensorEntityDescription, ...] = (
|
|
|
|
YoLinkBinarySensorEntityDescription(
|
2022-05-22 16:57:09 +00:00
|
|
|
key="door_state",
|
2022-05-19 14:43:32 +00:00
|
|
|
icon="mdi:door",
|
|
|
|
device_class=BinarySensorDeviceClass.DOOR,
|
2022-05-20 17:06:47 +00:00
|
|
|
name="State",
|
2022-05-19 14:43:32 +00:00
|
|
|
value=lambda value: value == "open",
|
|
|
|
exists_fn=lambda device: device.device_type in [ATTR_DEVICE_DOOR_SENSOR],
|
|
|
|
),
|
2022-05-20 17:06:47 +00:00
|
|
|
YoLinkBinarySensorEntityDescription(
|
2022-05-22 16:57:09 +00:00
|
|
|
key="motion_state",
|
2022-05-20 17:06:47 +00:00
|
|
|
device_class=BinarySensorDeviceClass.MOTION,
|
|
|
|
name="Motion",
|
|
|
|
value=lambda value: value == "alert",
|
|
|
|
exists_fn=lambda device: device.device_type in [ATTR_DEVICE_MOTION_SENSOR],
|
|
|
|
),
|
|
|
|
YoLinkBinarySensorEntityDescription(
|
2022-05-22 16:57:09 +00:00
|
|
|
key="leak_state",
|
2022-05-20 17:06:47 +00:00
|
|
|
name="Leak",
|
|
|
|
icon="mdi:water",
|
|
|
|
device_class=BinarySensorDeviceClass.MOISTURE,
|
|
|
|
value=lambda value: value == "alert",
|
|
|
|
exists_fn=lambda device: device.device_type in [ATTR_DEVICE_LEAK_SENSOR],
|
|
|
|
),
|
2022-05-19 14:43:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up YoLink Sensor from a config entry."""
|
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id][ATTR_COORDINATOR]
|
|
|
|
sensor_devices = [
|
|
|
|
device
|
|
|
|
for device in coordinator.yl_devices
|
|
|
|
if device.device_type in SENSOR_DEVICE_TYPE
|
|
|
|
]
|
|
|
|
entities = []
|
|
|
|
for sensor_device in sensor_devices:
|
|
|
|
for description in SENSOR_TYPES:
|
|
|
|
if description.exists_fn(sensor_device):
|
|
|
|
entities.append(
|
|
|
|
YoLinkBinarySensorEntity(coordinator, description, sensor_device)
|
|
|
|
)
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
|
|
|
class YoLinkBinarySensorEntity(YoLinkEntity, BinarySensorEntity):
|
|
|
|
"""YoLink Sensor Entity."""
|
|
|
|
|
|
|
|
entity_description: YoLinkBinarySensorEntityDescription
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: YoLinkCoordinator,
|
|
|
|
description: YoLinkBinarySensorEntityDescription,
|
|
|
|
device: YoLinkDevice,
|
|
|
|
) -> None:
|
|
|
|
"""Init YoLink Sensor."""
|
|
|
|
super().__init__(coordinator, device)
|
|
|
|
self.entity_description = description
|
|
|
|
self._attr_unique_id = f"{device.device_id} {self.entity_description.key}"
|
|
|
|
self._attr_name = f"{device.device_name} ({self.entity_description.name})"
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_entity_state(self, state: dict) -> None:
|
|
|
|
"""Update HA Entity State."""
|
|
|
|
self._attr_is_on = self.entity_description.value(
|
2022-05-22 16:57:09 +00:00
|
|
|
state[self.entity_description.state_key]
|
2022-05-19 14:43:32 +00:00
|
|
|
)
|
|
|
|
self.async_write_ha_state()
|