store.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Storage handers."""
  2. from homeassistant.helpers.json import JSONEncoder
  3. from homeassistant.helpers.storage import Store
  4. from homeassistant.util import json as json_util
  5. from ..const import VERSION_STORAGE
  6. from ..exceptions import HacsException
  7. from .logger import LOGGER
  8. _LOGGER = LOGGER
  9. class HACSStore(Store):
  10. """A subclass of Store that allows multiple loads in the executor."""
  11. def load(self):
  12. """Load the data from disk if version matches."""
  13. try:
  14. data = json_util.load_json(self.path)
  15. except BaseException as exception: # lgtm [py/catch-base-exception] pylint: disable=broad-except
  16. _LOGGER.critical(
  17. "Could not load '%s', restore it from a backup or delete the file: %s",
  18. self.path,
  19. exception,
  20. )
  21. raise HacsException(exception) from exception
  22. if data == {} or data["version"] != self.version:
  23. return None
  24. return data["data"]
  25. def get_store_key(key):
  26. """Return the key to use with homeassistant.helpers.storage.Storage."""
  27. return key if "/" in key else f"hacs.{key}"
  28. def _get_store_for_key(hass, key, encoder):
  29. """Create a Store object for the key."""
  30. return HACSStore(hass, VERSION_STORAGE, get_store_key(key), encoder=encoder, atomic_writes=True)
  31. def get_store_for_key(hass, key):
  32. """Create a Store object for the key."""
  33. return _get_store_for_key(hass, key, JSONEncoder)
  34. async def async_load_from_store(hass, key):
  35. """Load the retained data from store and return de-serialized data."""
  36. return await get_store_for_key(hass, key).async_load() or {}
  37. async def async_save_to_store(hass, key, data):
  38. """Generate dynamic data to store and save it to the filesystem.
  39. The data is only written if the content on the disk has changed
  40. by reading the existing content and comparing it.
  41. If the data has changed this will generate two executor jobs
  42. If the data has not changed this will generate one executor job
  43. """
  44. current = await async_load_from_store(hass, key)
  45. if current is None or current != data:
  46. await get_store_for_key(hass, key).async_save(data)
  47. return
  48. _LOGGER.debug(
  49. "<HACSStore async_save_to_store> Did not store data for '%s'. Content did not change",
  50. get_store_key(key),
  51. )
  52. async def async_remove_store(hass, key):
  53. """Remove a store element that should no longer be used."""
  54. if "/" not in key:
  55. return
  56. await get_store_for_key(hass, key).async_remove()