hOn/custom_components/hon/binary_sensor.py

263 lines
8.6 KiB
Python
Raw Normal View History

2023-03-05 19:19:52 +01:00
import logging
from dataclasses import dataclass
2023-04-10 07:09:54 +02:00
from pyhon import Hon
2023-03-05 19:19:52 +01:00
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
from .const import DOMAIN
from .hon import HonCoordinator, HonEntity
_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-03-21 13:05:03 +01:00
),
"TD": (
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-03-21 13:05:03 +01:00
),
HonBinarySensorEntityDescription(
key="doorStatus",
name="Door",
device_class=BinarySensorDeviceClass.DOOR,
on_value="1",
translation_key="door_open",
2023-03-21 13:05:03 +01:00
),
2023-04-07 13:52:55 +02:00
),
2023-04-08 04:44:47 +02:00
"WD": (
HonBinarySensorEntityDescription(
key="attributes.lastConnEvent.category",
name="Remote Control",
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-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
),
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
),
HonBinarySensorEntityDescription(
key="attributes.parameters.remoteCtrValid",
2023-04-15 04:27:40 +02:00
name="Remote Control",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
on_value="1",
icon="mdi:remote",
2023-04-23 02:01:14 +02:00
translation_key="remote_control",
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
),
HonBinarySensorEntityDescription(
key="attributes.parameters.remoteCtrValid",
name="Remote Control",
2023-04-07 13:52:55 +02:00
device_class=BinarySensorDeviceClass.CONNECTIVITY,
on_value="1",
2023-04-10 19:51:16 +02:00
icon="mdi:remote",
2023-04-23 02:01:14 +02:00
translation_key="remote_control",
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-03-05 19:19:52 +01:00
}
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
2023-04-10 07:09:54 +02:00
hon: Hon = hass.data[DOMAIN][entry.unique_id]
2023-03-05 19:19:52 +01:00
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
2023-04-10 07:09:54 +02:00
for device in hon.appliances:
if device.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id]
2023-03-05 19:19:52 +01:00
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator
2023-03-05 19:19:52 +01:00
await coordinator.async_config_entry_first_refresh()
2023-03-08 23:00:55 +01:00
if descriptions := BINARY_SENSORS.get(device.appliance_type):
2023-03-05 19:19:52 +01:00
for description in descriptions:
2023-03-08 23:00:55 +01:00
if not device.get(description.key):
2023-03-05 19:19:52 +01:00
continue
2023-04-10 19:51:16 +02:00
appliances.extend(
[
HonBinarySensorEntity(
hass, coordinator, entry, device, description
)
]
2023-03-05 19:19:52 +01:00
)
async_add_entities(appliances)
class HonBinarySensorEntity(HonEntity, BinarySensorEntity):
entity_description: HonBinarySensorEntityDescription
def __init__(self, hass, coordinator, entry, device, description) -> None:
super().__init__(hass, entry, coordinator, device)
self._coordinator = coordinator
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()