Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """The OurGroceries coordinator."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from datetime import timedelta
7 import logging
8 
9 from ourgroceries import OurGroceries
10 
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
13 
14 from .const import DOMAIN
15 
16 SCAN_INTERVAL = 60
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 
22  """Class to manage fetching OurGroceries data."""
23 
24  def __init__(self, hass: HomeAssistant, og: OurGroceries) -> None:
25  """Initialize global OurGroceries data updater."""
26  self.ogog = og
27  self.listslists: list[dict] = []
28  self._cache: dict[str, dict] = {}
29  interval = timedelta(seconds=SCAN_INTERVAL)
30  super().__init__(
31  hass,
32  _LOGGER,
33  name=DOMAIN,
34  update_interval=interval,
35  )
36 
37  async def _update_list(self, list_id: str, version_id: str) -> None:
38  old_version = self._cache.get(list_id, {}).get("list", {}).get("versionId", "")
39  if old_version == version_id:
40  return
41  self._cache[list_id] = await self.ogog.get_list_items(list_id=list_id)
42 
43  async def _async_update_data(self) -> dict[str, dict]:
44  """Fetch data from OurGroceries."""
45  self.listslists = (await self.ogog.get_my_lists())["shoppingLists"]
46  await asyncio.gather(
47  *[self._update_list_update_list(sl["id"], sl["versionId"]) for sl in self.listslists]
48  )
49  return self._cache
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88