diff --git a/homeassistant/helpers/storage.py b/homeassistant/helpers/storage.py index 2af21b070da..cdbea143e18 100644 --- a/homeassistant/helpers/storage.py +++ b/homeassistant/helpers/storage.py @@ -374,10 +374,6 @@ class Store(Generic[_T]): return data = self._data - - if "data_func" in data: - data["data"] = data.pop("data_func")() - self._data = None if self._read_only: @@ -395,6 +391,9 @@ class Store(Generic[_T]): """Write the data.""" os.makedirs(os.path.dirname(path), exist_ok=True) + if "data_func" in data: + data["data"] = data.pop("data_func")() + _LOGGER.debug("Writing data for %s to %s", self.key, path) json_helper.save_json( path, diff --git a/tests/common.py b/tests/common.py index 834354fa673..2e6b30d680b 100644 --- a/tests/common.py +++ b/tests/common.py @@ -1356,6 +1356,10 @@ def mock_storage( # To ensure that the data can be serialized _LOGGER.debug("Writing data to %s: %s", store.key, data_to_write) raise_contains_mocks(data_to_write) + + if "data_func" in data_to_write: + data_to_write["data"] = data_to_write.pop("data_func")() + encoder = store._encoder if encoder and encoder is not JSONEncoder: # If they pass a custom encoder that is not the @@ -1363,7 +1367,7 @@ def mock_storage( dump = ft.partial(json.dumps, cls=store._encoder) else: dump = _orjson_default_encoder - data[store.key] = json.loads(dump(data_to_write)) + data[store.key] = json_loads(dump(data_to_write)) async def mock_remove(store: storage.Store) -> None: """Remove data."""