Home Assistant Unofficial Reference 2024.12.1
store.py
Go to the documentation of this file.
1 """Google Calendar local storage."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from gcal_sync.store import CalendarStore
9 
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.storage import Store
12 
13 from .const import DOMAIN
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 STORAGE_KEY_FORMAT = "{domain}.{entry_id}"
18 STORAGE_VERSION = 1
19 # Buffer writes every few minutes (plus guaranteed to be written at shutdown)
20 STORAGE_SAVE_DELAY_SECONDS = 120
21 
22 
23 class LocalCalendarStore(CalendarStore):
24  """Storage for local persistence of calendar and event data."""
25 
26  def __init__(self, hass: HomeAssistant, entry_id: str) -> None:
27  """Initialize LocalCalendarStore."""
28  self._store_store = Store[dict[str, Any]](
29  hass,
30  STORAGE_VERSION,
31  STORAGE_KEY_FORMAT.format(domain=DOMAIN, entry_id=entry_id),
32  private=True,
33  )
34  self._data_data: dict[str, Any] | None = None
35 
36  async def async_load(self) -> dict[str, Any] | None:
37  """Load data."""
38  if self._data_data is None:
39  self._data_data = await self._store_store.async_load() or {}
40  return self._data_data
41 
42  async def async_save(self, data: dict[str, Any]) -> None:
43  """Save data."""
44  self._data_data = data
45 
46  def provide_data() -> dict:
47  return data
48 
49  self._store_store.async_delay_save(provide_data, STORAGE_SAVE_DELAY_SECONDS)
50 
51  async def async_remove(self) -> None:
52  """Remove data."""
53  await self._store_store.async_remove()
None __init__(self, HomeAssistant hass, str entry_id)
Definition: store.py:26
None async_save(self, dict[str, Any] data)
Definition: store.py:42
None async_delay_save(self, Callable[[], _T] data_func, float delay=0)
Definition: storage.py:444