69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
"""Binary sensors for the Whirlpool Appliances integration."""
|
|
|
|
from collections.abc import Callable
|
|
from dataclasses import dataclass
|
|
from datetime import timedelta
|
|
|
|
from whirlpool.appliance import Appliance
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
BinarySensorDeviceClass,
|
|
BinarySensorEntity,
|
|
BinarySensorEntityDescription,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|
|
|
from . import WhirlpoolConfigEntry
|
|
from .entity import WhirlpoolEntity
|
|
|
|
SCAN_INTERVAL = timedelta(minutes=5)
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
class WhirlpoolBinarySensorEntityDescription(BinarySensorEntityDescription):
|
|
"""Describes a Whirlpool binary sensor entity."""
|
|
|
|
value_fn: Callable[[Appliance], bool | None]
|
|
|
|
|
|
WASHER_DRYER_SENSORS: list[WhirlpoolBinarySensorEntityDescription] = [
|
|
WhirlpoolBinarySensorEntityDescription(
|
|
key="door",
|
|
device_class=BinarySensorDeviceClass.DOOR,
|
|
value_fn=lambda appliance: appliance.get_door_open(),
|
|
)
|
|
]
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: WhirlpoolConfigEntry,
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
|
) -> None:
|
|
"""Config flow entry for Whirlpool binary sensors."""
|
|
entities: list = []
|
|
appliances_manager = config_entry.runtime_data
|
|
for washer_dryer in appliances_manager.washer_dryers:
|
|
entities.extend(
|
|
WhirlpoolBinarySensor(washer_dryer, description)
|
|
for description in WASHER_DRYER_SENSORS
|
|
)
|
|
async_add_entities(entities)
|
|
|
|
|
|
class WhirlpoolBinarySensor(WhirlpoolEntity, BinarySensorEntity):
|
|
"""A class for the Whirlpool binary sensors."""
|
|
|
|
def __init__(
|
|
self, appliance: Appliance, description: WhirlpoolBinarySensorEntityDescription
|
|
) -> None:
|
|
"""Initialize the washer sensor."""
|
|
super().__init__(appliance, unique_id_suffix=f"-{description.key}")
|
|
self.entity_description: WhirlpoolBinarySensorEntityDescription = description
|
|
|
|
@property
|
|
def is_on(self) -> bool | None:
|
|
"""Return true if the binary sensor is on."""
|
|
return self.entity_description.value_fn(self._appliance)
|