1 """Lovelace resources support."""
3 from __future__
import annotations
9 import voluptuous
as vol
18 CONF_RESOURCE_TYPE_WS,
20 RESOURCE_CREATE_FIELDS,
22 RESOURCE_UPDATE_FIELDS,
24 from .dashboard
import LovelaceConfig
25 from .websocket
import websocket_lovelace_resources_impl
27 RESOURCE_STORAGE_KEY = f
"{DOMAIN}_resources"
28 RESOURCES_STORAGE_VERSION = 1
29 _LOGGER = logging.getLogger(__name__)
33 """Collection representing static YAML."""
38 """Initialize a resource YAML collection."""
42 """Return the resources info for YAML mode."""
43 return {
"resources": len(self.
async_itemsasync_items()
or [])}
47 """Return list of items in collection."""
52 """Collection to store resources."""
55 CREATE_SCHEMA = vol.Schema(RESOURCE_CREATE_FIELDS)
56 UPDATE_SCHEMA = vol.Schema(RESOURCE_UPDATE_FIELDS)
58 def __init__(self, hass: HomeAssistant, ll_config: LovelaceConfig) ->
None:
59 """Initialize the storage collection."""
61 storage.Store(hass, RESOURCES_STORAGE_VERSION, RESOURCE_STORAGE_KEY),
66 """Return the resources info for YAML mode."""
68 await self.async_load()
71 return {
"resources": len(self.async_items()
or [])}
75 if (store_data := await self.store.
async_load())
is not None:
81 except HomeAssistantError:
84 if CONF_RESOURCES
not in conf:
88 resources: list[dict[str, Any]] = conf[CONF_RESOURCES]
91 vol.Schema([RESOURCE_SCHEMA])(resources)
92 except vol.Invalid
as err:
93 _LOGGER.warning(
"Resource import failed. Data invalid: %s", err)
96 conf.pop(CONF_RESOURCES)
98 for item
in resources:
99 item[CONF_ID] = uuid.uuid4().hex
101 data: collection.SerializedStorageCollection = {
"items": resources}
109 """Validate the config is valid."""
111 data[CONF_TYPE] = data.pop(CONF_RESOURCE_TYPE_WS)
116 """Return unique ID."""
117 return uuid.uuid4().hex
120 """Return a new updated data object."""
122 await self.async_load()
126 if CONF_RESOURCE_TYPE_WS
in update_data:
127 update_data[CONF_TYPE] = update_data.pop(CONF_RESOURCE_TYPE_WS)
129 return {**item, **update_data}
133 """Class to expose storage collection management over websocket."""
137 """Set up the websocket commands."""
142 websocket_api.async_register_command(
146 websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
147 {vol.Required(
"type"): f
"{self.api_prefix}"}
152 @websocket_api.async_response
158 """Send Lovelace UI resources over WebSocket connection."""
None ws_list_item(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
None async_setup(self, HomeAssistant hass)
str _get_suggested_id(self, dict info)
None __init__(self, HomeAssistant hass, LovelaceConfig ll_config)
dict _process_create_data(self, dict data)
dict _update_data(self, dict item, dict update_data)
collection.SerializedStorageCollection|None _async_load_data(self)
list[dict] async_items(self)
None websocket_lovelace_resources_impl(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
None async_load(HomeAssistant hass)
None async_save(self, _T data)