2023-03-05 18:19:52 +00:00
|
|
|
import logging
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
from pyhon import HonConnection
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntityDescription, BinarySensorDeviceClass, \
|
|
|
|
BinarySensorEntity
|
|
|
|
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
|
|
|
|
class HonBinarySensorEntityDescription(HonBinarySensorEntityDescriptionMixin, BinarySensorEntityDescription):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
BINARY_SENSORS: dict[str, tuple[HonBinarySensorEntityDescription, ...]] = {
|
|
|
|
"WM": (
|
|
|
|
HonBinarySensorEntityDescription(
|
2023-03-08 22:00:55 +00:00
|
|
|
key="attributes.lastConnEvent.category",
|
2023-03-22 21:57:14 +00:00
|
|
|
name="Remote Control",
|
2023-03-05 18:19:52 +00:00
|
|
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
|
|
|
on_value="CONNECTED",
|
2023-03-22 21:57:14 +00:00
|
|
|
icon="mdi:remote"
|
2023-03-05 18:19:52 +00:00
|
|
|
),
|
|
|
|
HonBinarySensorEntityDescription(
|
|
|
|
key="doorLockStatus",
|
2023-03-22 21:57:14 +00:00
|
|
|
name="Door Lock",
|
|
|
|
device_class=BinarySensorDeviceClass.LOCK,
|
|
|
|
on_value="0",
|
|
|
|
),
|
|
|
|
HonBinarySensorEntityDescription(
|
|
|
|
key="doorStatus",
|
2023-03-08 22:00:55 +00:00
|
|
|
name="Door",
|
2023-03-05 18:19:52 +00:00
|
|
|
device_class=BinarySensorDeviceClass.DOOR,
|
2023-03-22 21:57:14 +00:00
|
|
|
on_value="1",
|
2023-03-05 18:19:52 +00:00
|
|
|
),
|
2023-03-21 12:05:03 +00:00
|
|
|
),
|
|
|
|
"TD": (
|
|
|
|
HonBinarySensorEntityDescription(
|
|
|
|
key="attributes.lastConnEvent.category",
|
|
|
|
name="Connection",
|
|
|
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
|
|
|
on_value="CONNECTED",
|
|
|
|
),
|
|
|
|
HonBinarySensorEntityDescription(
|
|
|
|
key="doorStatus",
|
|
|
|
name="Door",
|
|
|
|
device_class=BinarySensorDeviceClass.DOOR,
|
|
|
|
on_value="1",
|
|
|
|
),
|
2023-04-07 11:52:55 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
"OV": (
|
|
|
|
HonBinarySensorEntityDescription(
|
|
|
|
key="attributes.lastConnEvent.category",
|
|
|
|
name="Online",
|
|
|
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
|
|
|
on_value="CONNECTED",
|
|
|
|
icon="mdi:wifi"
|
|
|
|
),
|
|
|
|
|
|
|
|
HonBinarySensorEntityDescription(
|
|
|
|
key="attributes.parameters.remoteCtrValid",
|
|
|
|
name="On",
|
|
|
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
|
|
|
on_value="1",
|
|
|
|
icon="mdi:remote"
|
|
|
|
),
|
|
|
|
HonBinarySensorEntityDescription(
|
|
|
|
key="attributes.parameters.onOffStatus",
|
|
|
|
name="On",
|
|
|
|
device_class=BinarySensorDeviceClass.RUNNING,
|
|
|
|
on_value="1",
|
|
|
|
icon="mdi:power-cycle"
|
|
|
|
),
|
|
|
|
|
2023-03-05 18:19:52 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
|
|
|
|
hon: HonConnection = hass.data[DOMAIN][entry.unique_id]
|
|
|
|
coordinators = hass.data[DOMAIN]["coordinators"]
|
|
|
|
appliances = []
|
|
|
|
for device in hon.devices:
|
|
|
|
if device.mac_address in coordinators:
|
|
|
|
coordinator = hass.data[DOMAIN]["coordinators"][device.mac_address]
|
|
|
|
else:
|
|
|
|
coordinator = HonCoordinator(hass, device)
|
|
|
|
hass.data[DOMAIN]["coordinators"][device.mac_address] = coordinator
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
2023-03-08 22:00:55 +00:00
|
|
|
if descriptions := BINARY_SENSORS.get(device.appliance_type):
|
2023-03-05 18:19:52 +00:00
|
|
|
for description in descriptions:
|
2023-03-08 22:00:55 +00:00
|
|
|
if not device.get(description.key):
|
2023-03-05 18:19:52 +00:00
|
|
|
_LOGGER.info("Can't setup %s", description.key)
|
|
|
|
continue
|
|
|
|
appliances.extend([
|
|
|
|
HonBinarySensorEntity(hass, coordinator, entry, device, description)]
|
|
|
|
)
|
|
|
|
|
|
|
|
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-03-08 22:00:55 +00:00
|
|
|
return self._device.get(self.entity_description.key, "") == self.entity_description.on_value
|
2023-03-05 18:19:52 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self):
|
2023-03-08 22:00:55 +00:00
|
|
|
self._attr_native_value = self._device.get(self.entity_description.key, "") == self.entity_description.on_value
|
2023-03-05 18:19:52 +00:00
|
|
|
self.async_write_ha_state()
|