mirror of
https://github.com/mx42/home-assistant-ecocito.git
synced 2026-01-14 05:49:51 +01:00
Initial version
This commit is contained in:
113
custom_components/ecocito/coordinator.py
Normal file
113
custom_components/ecocito/coordinator.py
Normal file
@@ -0,0 +1,113 @@
|
||||
"""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
|
||||
)
|
||||
Reference in New Issue
Block a user