Peter Åslund a3a1d424c6
Implement data coordinator for Adax-integration (#139514)
* Implemented coordinator (for Cloud integration)

* Optimized coordinator updates

* Finalizing

* Running ruff and ruff format

* Raise error if trying to instantiate coordinator for a AdaxLocal config

* Re-added data-handler for AdaxLocal integrations

* Added a coordinator for Local integrations

* mypy warnings

* Update homeassistant/components/adax/manifest.json

Co-authored-by: Daniel Hjelseth Høyer <mail@dahoiv.net>

* Resolve mypy issues

* PR comments

- Explicit passing of config_entry to Coordinator base type
- Avoid duplicate storing of Coordinator data. Instead use self.data
- Remove try-catch and wrapping to UpdateFailed in _async_update_data
- Custom ConfigEntry type for passing coordinator via entry.runtime_data

* When changing HVAC_MODE update data via Coordinator to optimize

* Apply already loaded data for Climate entity directly in __init__

* Moved SCAN_INTERVAL into const.py

* Removed logging statements

* Remove unnecessary get_rooms() / get_status() functions

* Resolvning mypy issues

* Adding tests for coordinators

* Resolving review comments by joostlek

* Setup of Cloud devices with device_id

* Implement Climate tests for Adax

* Implementing assertions of UNAVAILABLE state

* Removed no longer needed method

* Apply suggestions from code review

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Mock Adax class instead of individual methods

* Mock config entry via fixture

* Load config entry data from .json fixture

* Hard code config_entry_data instead of .json file

* Removed obsolete .json-files

* Fix

* Fix

---------

Co-authored-by: Daniel Hjelseth Høyer <mail@dahoiv.net>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2025-04-30 20:33:46 +02:00

72 lines
2.4 KiB
Python

"""DataUpdateCoordinator for the Adax component."""
import logging
from typing import Any, cast
from adax import Adax
from adax_local import Adax as AdaxLocal
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import ACCOUNT_ID, SCAN_INTERVAL
_LOGGER = logging.getLogger(__name__)
type AdaxConfigEntry = ConfigEntry[AdaxCloudCoordinator | AdaxLocalCoordinator]
class AdaxCloudCoordinator(DataUpdateCoordinator[dict[str, dict[str, Any]]]):
"""Coordinator for updating data to and from Adax (cloud)."""
def __init__(self, hass: HomeAssistant, entry: AdaxConfigEntry) -> None:
"""Initialize the Adax coordinator used for Cloud mode."""
super().__init__(
hass,
config_entry=entry,
logger=_LOGGER,
name="AdaxCloud",
update_interval=SCAN_INTERVAL,
)
self.adax_data_handler = Adax(
entry.data[ACCOUNT_ID],
entry.data[CONF_PASSWORD],
websession=async_get_clientsession(hass),
)
async def _async_update_data(self) -> dict[str, dict[str, Any]]:
"""Fetch data from the Adax."""
rooms = await self.adax_data_handler.get_rooms() or []
return {r["id"]: r for r in rooms}
class AdaxLocalCoordinator(DataUpdateCoordinator[dict[str, Any] | None]):
"""Coordinator for updating data to and from Adax (local)."""
def __init__(self, hass: HomeAssistant, entry: AdaxConfigEntry) -> None:
"""Initialize the Adax coordinator used for Local mode."""
super().__init__(
hass,
config_entry=entry,
logger=_LOGGER,
name="AdaxLocal",
update_interval=SCAN_INTERVAL,
)
self.adax_data_handler = AdaxLocal(
entry.data[CONF_IP_ADDRESS],
entry.data[CONF_TOKEN],
websession=async_get_clientsession(hass, verify_ssl=False),
)
async def _async_update_data(self) -> dict[str, Any]:
"""Fetch data from the Adax."""
if result := await self.adax_data_handler.get_status():
return cast(dict[str, Any], result)
raise UpdateFailed("Got invalid status from device")