Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Provides the Geocaching DataUpdateCoordinator."""
2 
3 from __future__ import annotations
4 
5 from geocachingapi.exceptions import GeocachingApiError
6 from geocachingapi.geocachingapi import GeocachingApi
7 from geocachingapi.models import GeocachingStatus
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.aiohttp_client import async_get_clientsession
12 from homeassistant.helpers.config_entry_oauth2_flow import OAuth2Session
13 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
14 
15 from .const import DOMAIN, ENVIRONMENT, LOGGER, UPDATE_INTERVAL
16 
17 
19  """Class to manage fetching Geocaching data from single endpoint."""
20 
21  def __init__(
22  self, hass: HomeAssistant, *, entry: ConfigEntry, session: OAuth2Session
23  ) -> None:
24  """Initialize global Geocaching data updater."""
25  self.sessionsession = session
26  self.entryentry = entry
27 
28  async def async_token_refresh() -> str:
29  await session.async_ensure_token_valid()
30  token = session.token["access_token"]
31  LOGGER.debug(str(token))
32  return str(token)
33 
34  client_session = async_get_clientsession(hass)
35  self.geocachinggeocaching = GeocachingApi(
36  environment=ENVIRONMENT,
37  token=session.token["access_token"],
38  session=client_session,
39  token_refresh_method=async_token_refresh,
40  )
41 
42  super().__init__(hass, LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL)
43 
44  async def _async_update_data(self) -> GeocachingStatus:
45  try:
46  return await self.geocachinggeocaching.update()
47  except GeocachingApiError as error:
48  raise UpdateFailed(f"Invalid response from API: {error}") from error
None __init__(self, HomeAssistant hass, *ConfigEntry entry, OAuth2Session session)
Definition: coordinator.py:23
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)