feat: refactor the coordinator and client to dynamically get and use the type ID from config

This commit is contained in:
Xavier Morel
2025-03-24 21:19:41 +01:00
parent 787e9738c7
commit 88d2472c64
3 changed files with 38 additions and 23 deletions

View File

@@ -9,6 +9,14 @@ from homeassistant.const import CONF_DOMAIN, CONF_PASSWORD, CONF_USERNAME, Platf
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .client import EcocitoClient from .client import EcocitoClient
from .const import (
ECOCITO_DEFAULT_REFRESH_MIN,
ECOCITO_GARBAGE_COLLECTION_TYPE,
ECOCITO_GARBAGE_TYPE,
ECOCITO_RECYCLE_TYPE,
ECOCITO_RECYCLING_COLLECTION_TYPE,
ECOCITO_REFRESH_MIN_KEY,
)
from .coordinator import ( from .coordinator import (
GarbageCollectionsDataUpdateCoordinator, GarbageCollectionsDataUpdateCoordinator,
RecyclingCollectionsDataUpdateCoordinator, RecyclingCollectionsDataUpdateCoordinator,
@@ -40,18 +48,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: EcocitoConfigEntry) -> b
entry.data[CONF_PASSWORD], entry.data[CONF_PASSWORD],
) )
await client.authenticate() await client.authenticate()
garbage_id = entry.data.get(ECOCITO_GARBAGE_TYPE, ECOCITO_GARBAGE_COLLECTION_TYPE)
recycle_id = entry.data.get(ECOCITO_RECYCLE_TYPE, ECOCITO_RECYCLING_COLLECTION_TYPE)
refresh_time = entry.data.get(ECOCITO_REFRESH_MIN_KEY, ECOCITO_DEFAULT_REFRESH_MIN)
data = EcocitoData( data = EcocitoData(
garbage_collections=GarbageCollectionsDataUpdateCoordinator(hass, client, 0), garbage_collections=GarbageCollectionsDataUpdateCoordinator(hass, client, 0, garbage_id, refresh_time),
garbage_collections_previous=GarbageCollectionsDataUpdateCoordinator( garbage_collections_previous=GarbageCollectionsDataUpdateCoordinator(
hass, client, -1 hass, client, -1, garbage_id, refresh_time
), ),
recycling_collections=RecyclingCollectionsDataUpdateCoordinator( recycling_collections=RecyclingCollectionsDataUpdateCoordinator(
hass, client, 0 hass, client, 0, recycle_id, refresh_time
), ),
recycling_collections_previous=RecyclingCollectionsDataUpdateCoordinator( recycling_collections_previous=RecyclingCollectionsDataUpdateCoordinator(
hass, client, -1 hass, client, -1, recycle_id, refresh_time
), ),
waste_depot_visits=WasteDepotVisitsDataUpdateCoordinator(hass, client, 0), waste_depot_visits=WasteDepotVisitsDataUpdateCoordinator(hass, client, 0, refresh_time),
) )
for field in fields(data): for field in fields(data):
coordinator = getattr(data, field.name) coordinator = getattr(data, field.name)

View File

@@ -17,12 +17,10 @@ from .const import (
ECOCITO_ERROR_AUTHENTICATION, ECOCITO_ERROR_AUTHENTICATION,
ECOCITO_ERROR_FETCHING, ECOCITO_ERROR_FETCHING,
ECOCITO_ERROR_UNHANDLED, ECOCITO_ERROR_UNHANDLED,
ECOCITO_GARBAGE_COLLECTION_TYPE,
ECOCITO_LOGIN_ENDPOINT, ECOCITO_LOGIN_ENDPOINT,
ECOCITO_LOGIN_PASSWORD_KEY, ECOCITO_LOGIN_PASSWORD_KEY,
ECOCITO_LOGIN_URI, ECOCITO_LOGIN_URI,
ECOCITO_LOGIN_USERNAME_KEY, ECOCITO_LOGIN_USERNAME_KEY,
ECOCITO_RECYCLING_COLLECTION_TYPE,
ECOCITO_WASTE_DEPOSIT_ENDPOINT, ECOCITO_WASTE_DEPOSIT_ENDPOINT,
LOGGER, LOGGER,
) )
@@ -83,7 +81,7 @@ class EcocitoClient:
except aiohttp.ClientError as e: except aiohttp.ClientError as e:
raise EcocitoError(ECOCITO_ERROR_AUTHENTICATION.format(exc=e)) from e raise EcocitoError(ECOCITO_ERROR_AUTHENTICATION.format(exc=e)) from e
async def get_collection_types(self) -> dict: async def get_collection_types(self) -> dict[int, str]:
"""Return the mapping of collection type ID with their label.""" """Return the mapping of collection type ID with their label."""
async with aiohttp.ClientSession(cookie_jar=self._cookies) as session: async with aiohttp.ClientSession(cookie_jar=self._cookies) as session:
try: try:
@@ -96,7 +94,7 @@ class EcocitoClient:
try: try:
select = html.find("select", {"id": "Filtres_IdMatiere"}) select = html.find("select", {"id": "Filtres_IdMatiere"})
return { return {
item.attrs["value"]: item.text int(item.attrs["value"]): item.text
for item in select.find_all("option") for item in select.find_all("option")
if "value" in item.attrs if "value" in item.attrs
} }
@@ -147,13 +145,13 @@ class EcocitoClient:
ECOCITO_ERROR_FETCHING.format(exc=e, type="collection events") ECOCITO_ERROR_FETCHING.format(exc=e, type="collection events")
) from e ) from e
async def get_garbage_collections(self, year: int) -> list[CollectionEvent]: async def get_garbage_collections(self, year: int, type_id: int) -> list[CollectionEvent]:
"""Return the list of the garbage collections for a year.""" """Return the list of the garbage collections for a year."""
return await self.get_collection_events(ECOCITO_GARBAGE_COLLECTION_TYPE, year) return await self.get_collection_events(str(type_id), year)
async def get_recycling_collections(self, year: int) -> list[CollectionEvent]: async def get_recycling_collections(self, year: int, type_id: int) -> list[CollectionEvent]:
"""Return the list of the recycling collections for a year.""" """Return the list of the recycling collections for a year."""
return await self.get_collection_events(ECOCITO_RECYCLING_COLLECTION_TYPE, year) return await self.get_collection_events(str(type_id), year)
async def get_waste_depot_visits(self, year: int) -> list[WasteDepotVisit]: async def get_waste_depot_visits(self, year: int) -> list[WasteDepotVisit]:
"""Return the list of the waste depot visits for a year.""" """Return the list of the waste depot visits for a year."""

View File

@@ -28,13 +28,14 @@ class EcocitoDataUpdateCoordinator(DataUpdateCoordinator[T], Generic[T], ABC):
self, self,
hass: HomeAssistant, hass: HomeAssistant,
client: EcocitoClient, client: EcocitoClient,
refresh_interval: int = 60
) -> None: ) -> None:
"""Initialize the coordinator.""" """Initialize the coordinator."""
super().__init__( super().__init__(
hass=hass, hass=hass,
logger=LOGGER, logger=LOGGER,
name=DOMAIN, name=DOMAIN,
update_interval=timedelta(minutes=5), update_interval=timedelta(minutes=refresh_interval),
) )
self.client = client self.client = client
self._time_zone = ZoneInfo(hass.config.time_zone) self._time_zone = ZoneInfo(hass.config.time_zone)
@@ -53,23 +54,26 @@ class EcocitoDataUpdateCoordinator(DataUpdateCoordinator[T], Generic[T], ABC):
"""Fetch the actual data.""" """Fetch the actual data."""
raise NotImplementedError 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( class GarbageCollectionsDataUpdateCoordinator(
EcocitoDataUpdateCoordinator[list[CollectionEvent]] EcocitoDataUpdateCoordinator[list[CollectionEvent]]
): ):
"""Garbage collections list update from Ecocito.""" """Garbage collections list update from Ecocito."""
def __init__( def __init__(
self, hass: HomeAssistant, client: EcocitoClient, year_offset: int self, hass: HomeAssistant, client: EcocitoClient, year_offset: int, garbage_id: int, refresh_time: int
) -> None: ) -> None:
"""Initialize the coordinator.""" """Initialize the coordinator."""
super().__init__(hass, client) super().__init__(hass, client, refresh_time)
self._year_offset = year_offset self._year_offset = year_offset
self._garbage_id = garbage_id
async def _fetch_data(self) -> list[CollectionEvent]: async def _fetch_data(self) -> list[CollectionEvent]:
"""Fetch the data.""" """Fetch the data."""
return await self.client.get_garbage_collections( return await self.client.get_garbage_collections(
datetime.now(tz=self._time_zone).year + self._year_offset datetime.now(tz=self._time_zone).year + self._year_offset, self._garbage_id
) )
@@ -79,16 +83,17 @@ class RecyclingCollectionsDataUpdateCoordinator(
"""Recycling collections list update from Ecocito.""" """Recycling collections list update from Ecocito."""
def __init__( def __init__(
self, hass: HomeAssistant, client: EcocitoClient, year_offset: int self, hass: HomeAssistant, client: EcocitoClient, year_offset: int, recycle_id: int, refresh_time: int
) -> None: ) -> None:
"""Initialize the coordinator.""" """Initialize the coordinator."""
super().__init__(hass, client) super().__init__(hass, client, refresh_time)
self._year_offset = year_offset self._year_offset = year_offset
self._recycle_id = recycle_id
async def _fetch_data(self) -> list[CollectionEvent]: async def _fetch_data(self) -> list[CollectionEvent]:
"""Fetch the data.""" """Fetch the data."""
return await self.client.get_recycling_collections( return await self.client.get_recycling_collections(
datetime.now(tz=self._time_zone).year + self._year_offset datetime.now(tz=self._time_zone).year + self._year_offset, self._recycle_id
) )
@@ -98,10 +103,10 @@ class WasteDepotVisitsDataUpdateCoordinator(
"""Waste depot visits list update from Ecocito.""" """Waste depot visits list update from Ecocito."""
def __init__( def __init__(
self, hass: HomeAssistant, client: EcocitoClient, year_offset: int self, hass: HomeAssistant, client: EcocitoClient, year_offset: int, refresh_time: int
) -> None: ) -> None:
"""Initialize the coordinator.""" """Initialize the coordinator."""
super().__init__(hass, client) super().__init__(hass, client, refresh_time)
self._year_offset = year_offset self._year_offset = year_offset
async def _fetch_data(self) -> list[CollectionEvent]: async def _fetch_data(self) -> list[CollectionEvent]: