2023-03-03 17:23:30 +00:00
|
|
|
import logging
|
|
|
|
from datetime import timedelta
|
|
|
|
|
2023-05-16 22:01:33 +00:00
|
|
|
from pyhon.appliance import HonAppliance
|
|
|
|
|
2023-02-19 01:58:21 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2023-03-03 17:23:30 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2023-02-19 01:58:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HonEntity(CoordinatorEntity):
|
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
2023-04-10 05:09:54 +00:00
|
|
|
def __init__(self, hass, entry, coordinator, device: HonAppliance) -> None:
|
2023-02-19 01:58:21 +00:00
|
|
|
super().__init__(coordinator)
|
|
|
|
|
|
|
|
self._hon = hass.data[DOMAIN][entry.unique_id]
|
|
|
|
self._hass = hass
|
2023-05-18 23:27:44 +00:00
|
|
|
self._coordinator = coordinator
|
2023-02-19 01:58:21 +00:00
|
|
|
self._device = device
|
|
|
|
|
2023-04-15 20:03:50 +00:00
|
|
|
self._attr_unique_id = self._device.unique_id
|
2023-02-19 01:58:21 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
return DeviceInfo(
|
2023-04-15 20:03:50 +00:00
|
|
|
identifiers={(DOMAIN, self._device.unique_id)},
|
2023-03-08 22:00:55 +00:00
|
|
|
manufacturer=self._device.get("brand", ""),
|
2023-04-10 17:51:16 +00:00
|
|
|
name=self._device.nick_name
|
|
|
|
if self._device.nick_name
|
|
|
|
else self._device.model_name,
|
2023-02-19 01:58:21 +00:00
|
|
|
model=self._device.model_name,
|
2023-03-08 22:00:55 +00:00
|
|
|
sw_version=self._device.get("fwVersion", ""),
|
2023-02-19 01:58:21 +00:00
|
|
|
)
|
2023-03-03 17:23:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HonCoordinator(DataUpdateCoordinator):
|
2023-04-10 05:09:54 +00:00
|
|
|
def __init__(self, hass, device: HonAppliance):
|
2023-03-03 17:23:30 +00:00
|
|
|
"""Initialize my coordinator."""
|
2023-04-10 17:51:16 +00:00
|
|
|
super().__init__(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
2023-04-15 20:03:50 +00:00
|
|
|
name=device.unique_id,
|
2023-04-10 17:51:16 +00:00
|
|
|
update_interval=timedelta(seconds=30),
|
|
|
|
)
|
2023-03-03 17:23:30 +00:00
|
|
|
self._device = device
|
|
|
|
|
|
|
|
async def _async_update_data(self):
|
|
|
|
await self._device.update()
|
2023-05-07 14:39:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def unique_entities(base_entities, new_entities):
|
|
|
|
result = list(base_entities)
|
|
|
|
existing_entities = [entity.key for entity in base_entities]
|
|
|
|
for entity in new_entities:
|
|
|
|
if entity.key not in existing_entities:
|
|
|
|
result.append(entity)
|
|
|
|
return tuple(result)
|