Files
home-assistant-ecocito/custom_components/ecocito/coordinator.py
Thomas Bétrancourt df600eb18e Initial version
2024-11-03 20:59:08 +00:00

114 lines
3.6 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, 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,
) -> None:
"""Initialize the coordinator."""
super().__init__(
hass=hass,
logger=LOGGER,
name=DOMAIN,
update_interval=timedelta(minutes=5),
)
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(
"Credentials are no longer valid. Please reauthenticate"
) from ex
@abstractmethod
async def _fetch_data(self) -> T:
"""Fetch the actual data."""
raise NotImplementedError
class GarbageCollectionsDataUpdateCoordinator(
EcocitoDataUpdateCoordinator[list[CollectionEvent]]
):
"""Garbage collections list update from Ecocito."""
def __init__(
self, hass: HomeAssistant, client: EcocitoClient, year_offset: int
) -> None:
"""Initialize the coordinator."""
super().__init__(hass, client)
self._year_offset = year_offset
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
)
class RecyclingCollectionsDataUpdateCoordinator(
EcocitoDataUpdateCoordinator[list[CollectionEvent]]
):
"""Recycling collections list update from Ecocito."""
def __init__(
self, hass: HomeAssistant, client: EcocitoClient, year_offset: int
) -> None:
"""Initialize the coordinator."""
super().__init__(hass, client)
self._year_offset = year_offset
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
)
class WasteDepotVisitsDataUpdateCoordinator(
EcocitoDataUpdateCoordinator[list[WasteDepotVisit]]
):
"""Waste depot visits list update from Ecocito."""
def __init__(
self, hass: HomeAssistant, client: EcocitoClient, year_offset: int
) -> None:
"""Initialize the coordinator."""
super().__init__(hass, client)
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
)