1 """Websocket API for Lovelace."""
3 from __future__
import annotations
5 from functools
import wraps
8 import voluptuous
as vol
16 from .const
import CONF_URL_PATH, DOMAIN, ConfigNotFound
17 from .dashboard
import LovelaceStorage
21 """Handle error with WebSocket calls."""
24 async
def send_with_error_handling(
29 url_path = msg.get(CONF_URL_PATH)
30 config: LovelaceStorage |
None = hass.data[DOMAIN][
"dashboards"].
get(url_path)
33 connection.send_error(
34 msg[
"id"],
"config_not_found", f
"Unknown config specified: {url_path}"
40 result = await func(hass, connection, msg, config)
41 except ConfigNotFound:
42 error =
"config_not_found",
"No config found."
43 except HomeAssistantError
as err:
44 error =
"error",
str(err)
47 connection.send_error(msg[
"id"], *error)
50 connection.send_result(msg[
"id"], result)
52 return send_with_error_handling
55 @websocket_api.async_response
61 """Send Lovelace UI resources over WebSocket connection.
63 This function is used in YAML mode.
73 """Help send Lovelace UI resources over WebSocket connection.
75 This function is called by both Storage and YAML mode WS handlers.
77 resources = hass.data[DOMAIN][
"resources"]
79 if hass.config.safe_mode:
80 connection.send_result(msg[
"id"], [])
83 if not resources.loaded:
84 await resources.async_load()
85 resources.loaded =
True
87 connection.send_result(msg[
"id"], resources.async_items())
90 @websocket_api.websocket_command(
{
"type": "lovelace/config",
vol.Optional("force", default=False): bool,
91 vol.Optional(CONF_URL_PATH): vol.Any(
None, cv.string),
94 @websocket_api.async_response
100 config: LovelaceStorage,
102 """Send Lovelace UI config over WebSocket connection."""
103 return await config.async_json(msg[
"force"])
106 @websocket_api.require_admin
107 @websocket_api.websocket_command(
{
"type": "lovelace/config/save",
"config": vol.Any(str, dict),
108 vol.Optional(CONF_URL_PATH): vol.Any(
None, cv.string),
111 @websocket_api.async_response
117 config: LovelaceStorage,
119 """Save Lovelace UI configuration."""
120 await config.async_save(msg[
"config"])
123 @websocket_api.require_admin
124 @websocket_api.websocket_command(
{
"type": "lovelace/config/delete",
vol.Optional(CONF_URL_PATH): vol.Any(
None, cv.string),
127 @websocket_api.async_response
133 config: LovelaceStorage,
135 """Delete Lovelace UI configuration."""
136 await config.async_delete()
137
web.Response get(self, web.Request request, str config_key)
None websocket_lovelace_resources(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
None websocket_lovelace_delete_config(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg, LovelaceStorage config)
None websocket_lovelace_save_config(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg, LovelaceStorage config)
None websocket_lovelace_resources_impl(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
json_fragment websocket_lovelace_config(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg, LovelaceStorage config)