Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Define an update coordinator for OpenUV."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable
6 from typing import Any, cast
7 
8 from pyopenuv.errors import InvalidApiKeyError, OpenUvError
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryAuthFailed
13 from homeassistant.helpers.debounce import Debouncer
14 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
15 
16 from .const import LOGGER
17 
18 DEFAULT_DEBOUNCER_COOLDOWN_SECONDS = 15 * 60
19 
20 
22  """Define an OpenUV data coordinator."""
23 
24  config_entry: ConfigEntry
25  update_method: Callable[[], Awaitable[dict[str, Any]]]
26 
27  def __init__(
28  self,
29  hass: HomeAssistant,
30  *,
31  entry: ConfigEntry,
32  name: str,
33  latitude: str,
34  longitude: str,
35  update_method: Callable[[], Awaitable[dict[str, Any]]],
36  ) -> None:
37  """Initialize."""
38  super().__init__(
39  hass,
40  LOGGER,
41  name=name,
42  update_method=update_method,
43  request_refresh_debouncer=Debouncer(
44  hass,
45  LOGGER,
46  cooldown=DEFAULT_DEBOUNCER_COOLDOWN_SECONDS,
47  immediate=True,
48  ),
49  )
50 
51  self._entry_entry = entry
52  self.latitudelatitude = latitude
53  self.longitudelongitude = longitude
54 
55  async def _async_update_data(self) -> dict[str, Any]:
56  """Fetch data from OpenUV."""
57  try:
58  data = await self.update_methodupdate_method()
59  except InvalidApiKeyError as err:
60  raise ConfigEntryAuthFailed("Invalid API key") from err
61  except OpenUvError as err:
62  raise UpdateFailed(str(err)) from err
63 
64  return cast(dict[str, Any], data["result"])
None __init__(self, HomeAssistant hass, *ConfigEntry entry, str name, str latitude, str longitude, Callable[[], Awaitable[dict[str, Any]]] update_method)
Definition: coordinator.py:36