Allow storing other items than untyped dict in StorageCollection (#90932)

Allow storing other items than untyped dict in StorageCollection
This commit is contained in:
Erik Montnemery 2023-04-06 16:57:00 +02:00 committed by GitHub
parent 8025fbf398
commit 59a02cd08c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 134 additions and 91 deletions

View file

@ -6,7 +6,6 @@ import logging
import os
from pathlib import Path
import time
from typing import cast
import voluptuous as vol
@ -218,7 +217,7 @@ def _config_info(mode, config):
}
class DashboardsCollection(collection.StorageCollection):
class DashboardsCollection(collection.DictStorageCollection):
"""Collection of dashboards."""
CREATE_SCHEMA = vol.Schema(STORAGE_DASHBOARD_CREATE_FIELDS)
@ -230,10 +229,10 @@ class DashboardsCollection(collection.StorageCollection):
storage.Store(hass, DASHBOARDS_STORAGE_VERSION, DASHBOARDS_STORAGE_KEY),
)
async def _async_load_data(self) -> dict | None:
async def _async_load_data(self) -> collection.SerializedStorageCollection | None:
"""Load the data."""
if (data := await self.store.async_load()) is None:
return cast(dict | None, data)
return data
updated = False
@ -245,7 +244,7 @@ class DashboardsCollection(collection.StorageCollection):
if updated:
await self.store.async_save(data)
return cast(dict | None, data)
return data
async def _process_create_data(self, data: dict) -> dict:
"""Validate the config is valid."""
@ -262,10 +261,10 @@ class DashboardsCollection(collection.StorageCollection):
"""Suggest an ID based on the config."""
return info[CONF_URL_PATH]
async def _update_data(self, data: dict, update_data: dict) -> dict:
async def _update_data(self, item: dict, update_data: dict) -> dict:
"""Return a new updated data object."""
update_data = self.UPDATE_SCHEMA(update_data)
updated = {**data, **update_data}
updated = {**item, **update_data}
if CONF_ICON in updated and updated[CONF_ICON] is None:
updated.pop(CONF_ICON)