hOn/custom_components/hon/hon.py

123 lines
3.6 KiB
Python
Raw Normal View History

2023-06-29 22:19:29 +02:00
import json
2023-03-03 18:23:30 +01:00
import logging
2023-06-13 00:14:51 +02:00
from contextlib import suppress
2023-03-03 18:23:30 +01:00
from datetime import timedelta
2023-06-29 22:19:29 +02:00
from pathlib import Path
2023-03-03 18:23:30 +01:00
2023-06-29 22:19:29 +02:00
import pkg_resources
2023-06-08 20:01:55 +02:00
from homeassistant.core import callback
2023-02-19 02:58:21 +01:00
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
2023-03-03 18:23:30 +01:00
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
2023-05-25 00:52:54 +02:00
from pyhon.appliance import HonAppliance
2023-06-08 20:01:55 +02:00
from .const import DOMAIN, UPDATE_INTERVAL
2023-03-03 18:23:30 +01:00
_LOGGER = logging.getLogger(__name__)
2023-02-19 02:58:21 +01:00
class HonEntity(CoordinatorEntity):
_attr_has_entity_name = True
2023-05-28 00:30:08 +02:00
def __init__(self, hass, entry, device: HonAppliance, description=None) -> None:
2023-05-25 01:30:33 +02:00
coordinator = get_coordinator(hass, device)
2023-02-19 02:58:21 +01:00
super().__init__(coordinator)
self._hon = hass.data[DOMAIN][entry.unique_id]
self._hass = hass
2023-05-19 01:27:44 +02:00
self._coordinator = coordinator
2023-06-08 20:01:55 +02:00
self._device: HonAppliance = device
2023-02-19 02:58:21 +01:00
2023-05-28 00:30:08 +02:00
if description is not None:
self.entity_description = description
self._attr_unique_id = f"{self._device.unique_id}{description.key}"
else:
self._attr_unique_id = self._device.unique_id
2023-06-08 20:01:55 +02:00
self._handle_coordinator_update(update=False)
2023-02-19 02:58:21 +01:00
@property
def device_info(self):
return DeviceInfo(
identifiers={(DOMAIN, self._device.unique_id)},
2023-03-08 23:00:55 +01:00
manufacturer=self._device.get("brand", ""),
2023-06-29 22:19:29 +02:00
name=self._device.nick_name,
2023-02-19 02:58:21 +01:00
model=self._device.model_name,
2023-03-08 23:00:55 +01:00
sw_version=self._device.get("fwVersion", ""),
2023-02-19 02:58:21 +01:00
)
2023-03-03 18:23:30 +01:00
2023-06-08 20:01:55 +02:00
@callback
def _handle_coordinator_update(self, update: bool = True) -> None:
if update:
self.async_write_ha_state()
2023-03-03 18:23:30 +01:00
2023-06-29 22:19:29 +02:00
class HonInfo:
def __init__(self):
self._manifest = self._get_manifest()
self._hon_version = self._manifest.get("version", "")
self._pyhon_version = pkg_resources.get_distribution("pyhon").version
@staticmethod
def _get_manifest():
manifest = Path(__file__).parent / "manifest.json"
with open(manifest, "r", encoding="utf-8") as file:
return json.loads(file.read())
@property
def manifest(self):
return self._manifest
@property
def hon_version(self):
return self._hon_version
@property
def pyhon_version(self):
return self._pyhon_version
2023-03-03 18:23:30 +01:00
class HonCoordinator(DataUpdateCoordinator):
2023-04-10 07:09:54 +02:00
def __init__(self, hass, device: HonAppliance):
2023-03-03 18:23:30 +01:00
"""Initialize my coordinator."""
2023-04-10 19:51:16 +02:00
super().__init__(
hass,
_LOGGER,
name=device.unique_id,
2023-06-08 20:01:55 +02:00
update_interval=timedelta(seconds=UPDATE_INTERVAL),
2023-04-10 19:51:16 +02:00
)
2023-03-03 18:23:30 +01:00
self._device = device
2023-06-29 22:19:29 +02:00
self._info = HonInfo()
2023-03-03 18:23:30 +01:00
async def _async_update_data(self):
await self._device.update()
2023-05-07 16:39:45 +02:00
2023-06-29 22:19:29 +02:00
@property
def info(self) -> HonInfo:
return self._info
2023-05-07 16:39:45 +02: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)
2023-05-25 00:52:54 +02:00
def get_coordinator(hass, appliance):
coordinators = hass.data[DOMAIN]["coordinators"]
if appliance.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][appliance.unique_id]
else:
coordinator = HonCoordinator(hass, appliance)
hass.data[DOMAIN]["coordinators"][appliance.unique_id] = coordinator
return coordinator
2023-06-13 00:14:51 +02:00
def get_readable(description, value):
2023-06-22 13:18:45 +02:00
if description.option_list is not None:
with suppress(ValueError):
return description.option_list.get(int(value), value)
return value