mirror of
https://github.com/mx42/home-assistant-ecocito.git
synced 2026-01-14 05:49:51 +01:00
117 lines
4.1 KiB
Python
117 lines
4.1 KiB
Python
"""Data update coordinator for the Lidarr integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from abc import ABC, abstractmethod
|
|
from datetime import datetime, timedelta
|
|
from typing import Generic, TypeVar
|
|
from zoneinfo import ZoneInfo
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
from .client import CollectionEvent, EcocitoClient, WasteDepotVisit
|
|
from .const import DOMAIN, ECOCITO_MESSAGE_REAUTHENTICATE, LOGGER
|
|
from .errors import CannotConnectError, InvalidAuthenticationError
|
|
|
|
T = TypeVar("T", bound=list[CollectionEvent] | list[WasteDepotVisit])
|
|
|
|
|
|
class EcocitoDataUpdateCoordinator(DataUpdateCoordinator[T], Generic[T], ABC):
|
|
"""Data update coordinator for the Ecocito integration."""
|
|
|
|
config_entry: ConfigEntry
|
|
|
|
def __init__(
|
|
self,
|
|
hass: HomeAssistant,
|
|
client: EcocitoClient,
|
|
refresh_interval: int = 60
|
|
) -> None:
|
|
"""Initialize the coordinator."""
|
|
super().__init__(
|
|
hass=hass,
|
|
logger=LOGGER,
|
|
name=DOMAIN,
|
|
update_interval=timedelta(minutes=refresh_interval),
|
|
)
|
|
self.client = client
|
|
self._time_zone = ZoneInfo(hass.config.time_zone)
|
|
|
|
async def _async_update_data(self) -> T:
|
|
"""Get the latest data from Ecocito."""
|
|
try:
|
|
return await self._fetch_data()
|
|
except CannotConnectError as ex:
|
|
raise UpdateFailed(ex) from ex
|
|
except InvalidAuthenticationError as ex:
|
|
raise ConfigEntryAuthFailed(ECOCITO_MESSAGE_REAUTHENTICATE) from ex
|
|
|
|
@abstractmethod
|
|
async def _fetch_data(self) -> T:
|
|
"""Fetch the actual data."""
|
|
raise NotImplementedError
|
|
|
|
# TODO Fuse both coordinators? Since there's no hardcoded ID anymore, the duplicate may
|
|
# not really be needed anymore.
|
|
# Also later we could build any number of sensors, not just 2 (possibly only 1 also).
|
|
class GarbageCollectionsDataUpdateCoordinator(
|
|
EcocitoDataUpdateCoordinator[list[CollectionEvent]]
|
|
):
|
|
"""Garbage collections list update from Ecocito."""
|
|
|
|
def __init__(
|
|
self, hass: HomeAssistant, client: EcocitoClient, year_offset: int, garbage_id: int, refresh_time: int
|
|
) -> None:
|
|
"""Initialize the coordinator."""
|
|
super().__init__(hass, client, refresh_time)
|
|
self._year_offset = year_offset
|
|
self._garbage_id = garbage_id
|
|
|
|
async def _fetch_data(self) -> list[CollectionEvent]:
|
|
"""Fetch the data."""
|
|
return await self.client.get_garbage_collections(
|
|
datetime.now(tz=self._time_zone).year + self._year_offset, self._garbage_id
|
|
)
|
|
|
|
|
|
class RecyclingCollectionsDataUpdateCoordinator(
|
|
EcocitoDataUpdateCoordinator[list[CollectionEvent]]
|
|
):
|
|
"""Recycling collections list update from Ecocito."""
|
|
|
|
def __init__(
|
|
self, hass: HomeAssistant, client: EcocitoClient, year_offset: int, recycle_id: int, refresh_time: int
|
|
) -> None:
|
|
"""Initialize the coordinator."""
|
|
super().__init__(hass, client, refresh_time)
|
|
self._year_offset = year_offset
|
|
self._recycle_id = recycle_id
|
|
|
|
async def _fetch_data(self) -> list[CollectionEvent]:
|
|
"""Fetch the data."""
|
|
return await self.client.get_recycling_collections(
|
|
datetime.now(tz=self._time_zone).year + self._year_offset, self._recycle_id
|
|
)
|
|
|
|
|
|
class WasteDepotVisitsDataUpdateCoordinator(
|
|
EcocitoDataUpdateCoordinator[list[WasteDepotVisit]]
|
|
):
|
|
"""Waste depot visits list update from Ecocito."""
|
|
|
|
def __init__(
|
|
self, hass: HomeAssistant, client: EcocitoClient, year_offset: int, refresh_time: int
|
|
) -> None:
|
|
"""Initialize the coordinator."""
|
|
super().__init__(hass, client, refresh_time)
|
|
self._year_offset = year_offset
|
|
|
|
async def _fetch_data(self) -> list[CollectionEvent]:
|
|
"""Fetch the data."""
|
|
return await self.client.get_waste_depot_visits(
|
|
datetime.now(tz=self._time_zone).year + self._year_offset
|
|
)
|