1 """Define a Ridwell coordinator."""
3 from __future__
import annotations
6 from datetime
import timedelta
7 from typing
import cast
9 from aioridwell.client
import async_get_client
10 from aioridwell.errors
import InvalidCredentialsError, RidwellError
11 from aioridwell.model
import RidwellAccount, RidwellPickupEvent
20 from .const
import LOGGER
26 DataUpdateCoordinator[dict[str, list[RidwellPickupEvent]]]
28 """Class to manage fetching data from single endpoint."""
30 config_entry: ConfigEntry
32 def __init__(self, hass: HomeAssistant, *, name: str) ->
None:
36 self.
accountsaccounts: dict[str, RidwellAccount] = {}
40 super().
__init__(hass, LOGGER, name=name, update_interval=UPDATE_INTERVAL)
43 """Fetch the latest data from the source."""
46 async
def async_get_pickups(account: RidwellAccount) ->
None:
47 """Get the latest pickups for an account."""
48 data[account.account_id] = await account.async_get_pickup_events()
50 tasks = [async_get_pickups(account)
for account
in self.
accountsaccounts.values()]
51 results = await asyncio.gather(*tasks, return_exceptions=
True)
52 for result
in results:
53 if isinstance(result, InvalidCredentialsError):
55 if isinstance(result, RidwellError):
61 """Initialize the coordinator."""
62 session = aiohttp_client.async_get_clientsession(self.
hasshass)
65 client = await async_get_client(
70 except InvalidCredentialsError
as err:
72 except RidwellError
as err:
75 self.
accountsaccounts = await client.async_get_accounts()
79 self.
user_iduser_id = cast(str, client.user_id)
dict[str, list[RidwellPickupEvent]] _async_update_data(self)
None __init__(self, HomeAssistant hass, *str name)
None async_initialize(self)
None async_config_entry_first_refresh(self)