hOn/custom_components/hon/binary_sensor.py

286 lines
9.3 KiB
Python
Raw Normal View History

2023-03-05 19:19:52 +01:00
import logging
from dataclasses import dataclass
2023-04-10 19:51:16 +02:00
from homeassistant.components.binary_sensor import (
BinarySensorEntityDescription,
BinarySensorDeviceClass,
BinarySensorEntity,
)
2023-03-05 19:19:52 +01:00
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback
2023-05-25 00:52:54 +02:00
2023-03-05 19:19:52 +01:00
from .const import DOMAIN
2023-05-25 01:30:33 +02:00
from .hon import HonEntity, unique_entities
2023-03-05 19:19:52 +01:00
_LOGGER = logging.getLogger(__name__)
@dataclass
class HonBinarySensorEntityDescriptionMixin:
on_value: str = ""
@dataclass
2023-04-10 19:51:16 +02:00
class HonBinarySensorEntityDescription(
HonBinarySensorEntityDescriptionMixin, BinarySensorEntityDescription
):
2023-03-05 19:19:52 +01:00
pass
BINARY_SENSORS: dict[str, tuple[HonBinarySensorEntityDescription, ...]] = {
"WM": (
HonBinarySensorEntityDescription(
2023-03-08 23:00:55 +01:00
key="attributes.lastConnEvent.category",
2023-03-22 22:57:14 +01:00
name="Remote Control",
2023-03-05 19:19:52 +01:00
device_class=BinarySensorDeviceClass.CONNECTIVITY,
on_value="CONNECTED",
2023-04-10 19:51:16 +02:00
icon="mdi:remote",
2023-04-23 02:01:14 +02:00
translation_key="remote_control",
2023-03-05 19:19:52 +01:00
),
HonBinarySensorEntityDescription(
key="doorLockStatus",
2023-03-22 22:57:14 +01:00
name="Door Lock",
device_class=BinarySensorDeviceClass.LOCK,
on_value="0",
2023-04-23 02:01:14 +02:00
translation_key="door_lock",
2023-03-22 22:57:14 +01:00
),
HonBinarySensorEntityDescription(
key="doorStatus",
2023-03-08 23:00:55 +01:00
name="Door",
2023-03-05 19:19:52 +01:00
device_class=BinarySensorDeviceClass.DOOR,
2023-03-22 22:57:14 +01:00
on_value="1",
translation_key="door_open",
2023-03-05 19:19:52 +01:00
),
2023-04-08 04:44:47 +02:00
HonBinarySensorEntityDescription(
2023-04-24 21:38:05 +02:00
key="startProgram.prewash", name="Pre Wash", translation_key="prewash"
2023-04-08 04:44:47 +02:00
),
HonBinarySensorEntityDescription(
2023-04-23 02:01:14 +02:00
key="extraRinse1", name="Extra Rinse 1", translation_key="extra_rinse_1"
2023-04-08 04:44:47 +02:00
),
HonBinarySensorEntityDescription(
2023-04-23 02:01:14 +02:00
key="extraRinse2", name="Extra Rinse 2", translation_key="extra_rinse_2"
2023-04-08 04:44:47 +02:00
),
HonBinarySensorEntityDescription(
2023-04-23 02:01:14 +02:00
key="extraRinse3", name="Extra Rinse 3", translation_key="extra_rinse_3"
2023-04-08 04:44:47 +02:00
),
HonBinarySensorEntityDescription(
2023-04-23 02:01:14 +02:00
key="goodNight", name="Good Night Mode", translation_key="good_night"
2023-04-08 04:44:47 +02:00
),
HonBinarySensorEntityDescription(
key="acquaplus", name="Acqua Plus", translation_key="acqua_plus"
2023-04-08 04:44:47 +02:00
),
2023-05-07 16:39:45 +02:00
),
"TD": (
HonBinarySensorEntityDescription(
key="attributes.lastConnEvent.category",
name="Connection",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
on_value="CONNECTED",
translation_key="connection",
),
HonBinarySensorEntityDescription(
key="doorStatus",
name="Door",
device_class=BinarySensorDeviceClass.DOOR,
on_value="1",
translation_key="door_open",
),
2023-04-08 04:44:47 +02:00
HonBinarySensorEntityDescription(
2023-04-23 02:01:14 +02:00
key="anticrease", name="Anti-Crease", translation_key="anti_crease"
2023-04-08 04:44:47 +02:00
),
),
2023-04-07 13:52:55 +02:00
"OV": (
HonBinarySensorEntityDescription(
key="attributes.lastConnEvent.category",
2023-04-15 04:27:40 +02:00
name="Connection",
2023-04-07 13:52:55 +02:00
device_class=BinarySensorDeviceClass.CONNECTIVITY,
on_value="CONNECTED",
2023-04-10 19:51:16 +02:00
icon="mdi:wifi",
2023-04-23 02:01:14 +02:00
translation_key="connection",
2023-04-07 13:52:55 +02:00
),
2023-04-15 04:27:40 +02:00
HonBinarySensorEntityDescription(
key="attributes.parameters.onOffStatus",
2023-04-07 13:52:55 +02:00
name="On",
2023-04-15 04:27:40 +02:00
device_class=BinarySensorDeviceClass.RUNNING,
on_value="1",
icon="mdi:power-cycle",
2023-04-24 21:38:05 +02:00
translation_key="on",
2023-04-15 04:27:40 +02:00
),
),
2023-04-16 13:55:08 +02:00
"IH": (
2023-04-15 04:27:40 +02:00
HonBinarySensorEntityDescription(
key="attributes.lastConnEvent.category",
name="Connection",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
on_value="CONNECTED",
icon="mdi:wifi",
2023-04-23 02:01:14 +02:00
translation_key="connection",
2023-04-15 04:27:40 +02:00
),
2023-04-07 13:52:55 +02:00
HonBinarySensorEntityDescription(
key="attributes.parameters.onOffStatus",
name="On",
device_class=BinarySensorDeviceClass.RUNNING,
on_value="1",
2023-04-10 19:51:16 +02:00
icon="mdi:power-cycle",
2023-04-24 21:38:05 +02:00
translation_key="on",
2023-04-07 13:52:55 +02:00
),
2023-04-15 04:27:40 +02:00
HonBinarySensorEntityDescription(
key="hotStatus",
name="Hot Status",
device_class=BinarySensorDeviceClass.HEAT,
on_value="1",
2023-04-23 02:01:14 +02:00
translation_key="still_hot",
2023-04-15 04:27:40 +02:00
),
2023-04-18 23:44:23 +02:00
HonBinarySensorEntityDescription(
2023-04-23 02:01:14 +02:00
key="panStatus",
name="Pan Status",
on_value="1",
icon="mdi:pot-mix",
translation_key="pan_status",
2023-04-18 23:44:23 +02:00
),
2023-04-15 04:27:40 +02:00
HonBinarySensorEntityDescription(
key="hobLockStatus",
name="Hob Lock",
device_class=BinarySensorDeviceClass.LOCK,
on_value="0",
2023-04-24 21:38:05 +02:00
translation_key="child_lock",
2023-04-15 04:27:40 +02:00
),
2023-04-08 04:44:47 +02:00
),
2023-04-16 21:46:17 +02:00
"DW": (
HonBinarySensorEntityDescription(
key="saltStatus",
name="Salt",
device_class=BinarySensorDeviceClass.PROBLEM,
on_value="1",
icon="mdi:shaker-outline",
2023-04-23 02:01:14 +02:00
translation_key="salt_level",
2023-04-16 21:46:17 +02:00
),
HonBinarySensorEntityDescription(
key="rinseAidStatus",
name="Rinse Aid",
device_class=BinarySensorDeviceClass.PROBLEM,
on_value="1",
icon="mdi:spray-bottle",
2023-04-23 02:01:14 +02:00
translation_key="rinse_aid",
2023-04-16 21:46:17 +02:00
),
HonBinarySensorEntityDescription(
key="attributes.lastConnEvent.category",
name="Connection",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
on_value="CONNECTED",
2023-04-23 02:01:14 +02:00
translation_key="connection",
2023-04-16 21:46:17 +02:00
),
HonBinarySensorEntityDescription(
key="doorStatus",
name="Door",
device_class=BinarySensorDeviceClass.DOOR,
on_value="1",
2023-04-23 02:01:14 +02:00
translation_key="door_open",
2023-04-16 21:46:17 +02:00
),
),
2023-05-17 00:01:33 +02:00
"AC": (
HonBinarySensorEntityDescription(
key="filterChangeStatusLocal",
name="Filter Replacement",
device_class=BinarySensorDeviceClass.PROBLEM,
on_value="1",
translation_key="filter_replacement",
),
HonBinarySensorEntityDescription(
key="ch2oCleaningStatus",
name="Ch2O Cleaning",
on_value="1",
),
),
2023-05-15 00:38:41 +02:00
"REF": (
HonBinarySensorEntityDescription(
key="quickModeZ2",
name="Super Cool",
icon="mdi:snowflake",
device_class=BinarySensorDeviceClass.RUNNING,
on_value="1",
translation_key="super_cool",
),
HonBinarySensorEntityDescription(
key="quickModeZ1",
name="Super Freeze",
icon="mdi:snowflake-variant",
device_class=BinarySensorDeviceClass.RUNNING,
on_value="1",
translation_key="super_freeze",
),
HonBinarySensorEntityDescription(
key="doorStatusZ1",
name="Door Status Freezer",
device_class=BinarySensorDeviceClass.DOOR,
icon="mdi:fridge-top",
on_value="1",
translation_key="freezer_door",
),
HonBinarySensorEntityDescription(
key="door2StatusZ1",
name="Door Status Fridge",
icon="mdi:fridge-bottom",
device_class=BinarySensorDeviceClass.DOOR,
on_value="1",
translation_key="fridge_door",
),
HonBinarySensorEntityDescription(
key="intelligenceMode",
name="Auto-Set Mode",
icon="mdi:thermometer-auto",
2023-05-15 00:38:41 +02:00
device_class=BinarySensorDeviceClass.RUNNING,
on_value="1",
translation_key="auto_set",
),
HonBinarySensorEntityDescription(
key="holidayMode",
name="Holiday Mode",
icon="mdi:palm-tree",
device_class=BinarySensorDeviceClass.RUNNING,
on_value="1",
translation_key="holiday_mode",
),
),
2023-03-05 19:19:52 +01:00
}
2023-05-07 16:39:45 +02:00
BINARY_SENSORS["WD"] = unique_entities(BINARY_SENSORS["WM"], BINARY_SENSORS["TD"])
2023-03-05 19:19:52 +01:00
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
2023-05-25 01:30:33 +02:00
entities = []
for device in hass.data[DOMAIN][entry.unique_id].appliances:
for description in BINARY_SENSORS.get(device.appliance_type, []):
if not device.get(description.key):
continue
entity = HonBinarySensorEntity(hass, entry, device, description)
await entity.coordinator.async_config_entry_first_refresh()
entities.append(entity)
async_add_entities(entities)
2023-03-05 19:19:52 +01:00
class HonBinarySensorEntity(HonEntity, BinarySensorEntity):
entity_description: HonBinarySensorEntityDescription
2023-05-25 01:30:33 +02:00
def __init__(self, hass, entry, device, description) -> None:
super().__init__(hass, entry, device)
2023-03-05 19:19:52 +01:00
self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}"
@property
def is_on(self) -> bool:
2023-04-10 19:51:16 +02:00
return (
self._device.get(self.entity_description.key, "")
== self.entity_description.on_value
)
2023-03-05 19:19:52 +01:00
@callback
def _handle_coordinator_update(self):
2023-04-10 19:51:16 +02:00
self._attr_native_value = (
self._device.get(self.entity_description.key, "")
== self.entity_description.on_value
)
2023-03-05 19:19:52 +01:00
self.async_write_ha_state()