From 69d51c67cf2076e4d66ef45c19c7537924bc0e1a Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 9 Jul 2022 22:33:32 +0200 Subject: [PATCH] Store is now a Generic class (#1386) Co-authored-by: Martin Hjelmare --- blog/2022-07-08-generic-store.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 blog/2022-07-08-generic-store.md diff --git a/blog/2022-07-08-generic-store.md b/blog/2022-07-08-generic-store.md new file mode 100644 index 00000000..729247d1 --- /dev/null +++ b/blog/2022-07-08-generic-store.md @@ -0,0 +1,13 @@ +--- +author: epenet +authorURL: https://github.com/epenet +title: "Store is now a Generic class" +--- + +As of Home Assistant Core 2022.8, a Store (from `homeassistant/helpers/storage.py`) is defined as a Generic `Store(Generic[_T])`. It is recommended that the type of data being stored be defined in the Store definition. It should be JSON-serialisable (dict or list), for example: + - Standard definition using a dict: `self._store = Store[dict[str, int]](hass, STORAGE_VERSION, STORAGE_KEY)` + - Using a TypedDict: `self._store = Store[EnergyPreferences](hass, STORAGE_VERSION, STORAGE_KEY)` + - Accessing an existing Store: `store: Store[dict[str, Any]] = hass.data[DOMAIN][DATA_STORE]` + - Inherited Store: `class MyCustomStorage(Store[list[int]]):` + +For more information about generics, see [PEP 483](https://peps.python.org/pep-0483/#generic-types)