hOn/custom_components/hon/hon.py

143 lines
4.4 KiB
Python
Raw Normal View History

2023-06-29 20:19:29 +00:00
import json
2023-03-03 17:23:30 +00:00
import logging
2023-06-12 22:14:51 +00:00
from contextlib import suppress
2023-03-03 17:23:30 +00:00
from datetime import timedelta
2023-06-29 20:19:29 +00:00
from pathlib import Path
2023-07-23 23:56:15 +00:00
from typing import Optional, Any
2023-03-03 17:23:30 +00:00
2023-07-23 23:56:15 +00:00
import pkg_resources # type: ignore[import, unused-ignore]
from homeassistant.config_entries import ConfigEntry
2023-06-08 18:01:55 +00:00
from homeassistant.core import callback
2023-02-19 01:58:21 +00:00
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.typing import HomeAssistantType
2023-02-19 01:58:21 +00:00
from homeassistant.helpers.update_coordinator import CoordinatorEntity
2023-03-03 17:23:30 +00:00
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
2023-05-24 22:52:54 +00:00
from pyhon.appliance import HonAppliance
2024-03-25 01:26:20 +00:00
from .const import DOMAIN
from .typedefs import HonEntityDescription, HonOptionEntityDescription, T
2023-03-03 17:23:30 +00:00
_LOGGER = logging.getLogger(__name__)
2023-02-19 01:58:21 +00:00
2023-06-29 20:19:29 +00:00
class HonInfo:
def __init__(self) -> None:
self._manifest: dict[str, Any] = self._get_manifest()
self._hon_version: str = self._manifest.get("version", "")
self._pyhon_version: str = pkg_resources.get_distribution("pyhon").version
2023-06-29 20:19:29 +00:00
@staticmethod
def _get_manifest() -> dict[str, Any]:
2023-06-29 20:19:29 +00:00
manifest = Path(__file__).parent / "manifest.json"
with open(manifest, "r", encoding="utf-8") as file:
result: dict[str, Any] = json.loads(file.read())
return result
2023-06-29 20:19:29 +00:00
@property
def manifest(self) -> dict[str, Any]:
2023-06-29 20:19:29 +00:00
return self._manifest
@property
def hon_version(self) -> str:
2023-06-29 20:19:29 +00:00
return self._hon_version
@property
def pyhon_version(self) -> str:
2023-06-29 20:19:29 +00:00
return self._pyhon_version
class HonCoordinator(DataUpdateCoordinator[None]):
def __init__(self, hass: HomeAssistantType, 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,
name=device.unique_id,
2023-04-10 17:51:16 +00:00
)
2023-03-03 17:23:30 +00:00
self._device = device
2023-06-29 20:19:29 +00:00
self._info = HonInfo()
2024-03-25 01:26:20 +00:00
self._device.subscribe(self.async_set_updated_data)
2023-03-03 17:23:30 +00:00
async def _async_update_data(self) -> None:
2024-03-25 01:26:20 +00:00
return
2023-05-07 14:39:45 +00:00
2023-06-29 20:19:29 +00:00
@property
def info(self) -> HonInfo:
return self._info
2023-05-07 14:39:45 +00:00
class HonEntity(CoordinatorEntity[HonCoordinator]):
_attr_has_entity_name = True
2024-03-25 01:26:20 +00:00
_attr_should_poll = False
def __init__(
self,
hass: HomeAssistantType,
entry: ConfigEntry,
device: HonAppliance,
description: Optional[HonEntityDescription] = None,
) -> None:
coordinator = get_coordinator(hass, device)
super().__init__(coordinator)
self._hon = hass.data[DOMAIN][entry.unique_id]
self._hass = hass
self._coordinator = coordinator
self._device: HonAppliance = device
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
self._handle_coordinator_update(update=False)
@property
def device_info(self) -> DeviceInfo:
return DeviceInfo(
identifiers={(DOMAIN, self._device.unique_id)},
manufacturer=self._device.get("brand", ""),
name=self._device.nick_name,
model=self._device.model_name,
sw_version=self._device.get("fwVersion", ""),
)
@callback
def _handle_coordinator_update(self, update: bool = True) -> None:
if update:
self.async_write_ha_state()
def unique_entities(
base_entities: tuple[T, ...],
new_entities: tuple[T, ...],
) -> tuple[T, ...]:
2023-05-07 14:39:45 +00:00
result = list(base_entities)
existing_entities = [entity.key for entity in base_entities]
entity: HonEntityDescription
2023-05-07 14:39:45 +00:00
for entity in new_entities:
if entity.key not in existing_entities:
result.append(entity)
return tuple(result)
2023-05-24 22:52:54 +00:00
def get_coordinator(hass: HomeAssistantType, appliance: HonAppliance) -> HonCoordinator:
2023-05-24 22:52:54 +00:00
coordinators = hass.data[DOMAIN]["coordinators"]
if appliance.unique_id in coordinators:
coordinator: HonCoordinator = hass.data[DOMAIN]["coordinators"][
appliance.unique_id
]
2023-05-24 22:52:54 +00:00
else:
coordinator = HonCoordinator(hass, appliance)
hass.data[DOMAIN]["coordinators"][appliance.unique_id] = coordinator
return coordinator
2023-06-12 22:14:51 +00:00
def get_readable(
description: HonOptionEntityDescription, value: float | str
) -> float | str:
2023-06-22 11:18:45 +00:00
if description.option_list is not None:
with suppress(ValueError):
return description.option_list.get(int(value), value)
return value