2024-01-22 12:36:26 +00:00
|
|
|
"""Binary sensor module."""
|
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Generic
|
|
|
|
|
|
|
|
from deebot_client.capabilities import CapabilityEvent
|
|
|
|
from deebot_client.events.water_info import WaterInfoEvent
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
BinarySensorEntity,
|
|
|
|
BinarySensorEntityDescription,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import EntityCategory
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .controller import EcovacsController
|
2024-01-23 20:17:18 +00:00
|
|
|
from .entity import EcovacsCapabilityEntityDescription, EcovacsDescriptionEntity, EventT
|
|
|
|
from .util import get_supported_entitites
|
2024-01-22 12:36:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass(kw_only=True, frozen=True)
|
|
|
|
class EcovacsBinarySensorEntityDescription(
|
|
|
|
BinarySensorEntityDescription,
|
2024-01-23 20:17:18 +00:00
|
|
|
EcovacsCapabilityEntityDescription,
|
2024-01-22 12:36:26 +00:00
|
|
|
Generic[EventT],
|
|
|
|
):
|
|
|
|
"""Class describing Deebot binary sensor entity."""
|
|
|
|
|
|
|
|
value_fn: Callable[[EventT], bool | None]
|
|
|
|
|
|
|
|
|
|
|
|
ENTITY_DESCRIPTIONS: tuple[EcovacsBinarySensorEntityDescription, ...] = (
|
|
|
|
EcovacsBinarySensorEntityDescription[WaterInfoEvent](
|
|
|
|
capability_fn=lambda caps: caps.water,
|
|
|
|
value_fn=lambda e: e.mop_attached,
|
|
|
|
key="mop_attached",
|
|
|
|
translation_key="mop_attached",
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Add entities for passed config_entry in HA."""
|
|
|
|
controller: EcovacsController = hass.data[DOMAIN][config_entry.entry_id]
|
2024-01-23 20:17:18 +00:00
|
|
|
async_add_entities(
|
|
|
|
get_supported_entitites(controller, EcovacsBinarySensor, ENTITY_DESCRIPTIONS)
|
2024-01-22 12:36:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class EcovacsBinarySensor(
|
2024-01-23 20:17:18 +00:00
|
|
|
EcovacsDescriptionEntity[CapabilityEvent[EventT]],
|
2024-01-22 12:36:26 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
):
|
|
|
|
"""Ecovacs binary sensor."""
|
|
|
|
|
|
|
|
entity_description: EcovacsBinarySensorEntityDescription
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Set up the event listeners now that hass is ready."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
|
|
|
async def on_event(event: EventT) -> None:
|
|
|
|
self._attr_is_on = self.entity_description.value_fn(event)
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
self._subscribe(self._capability.event, on_event)
|