Home Assistant Unofficial Reference 2024.12.1
websocket.py
Go to the documentation of this file.
1 """Websocket API for Lovelace."""
2 
3 from __future__ import annotations
4 
5 from functools import wraps
6 from typing import Any
7 
8 import voluptuous as vol
9 
10 from homeassistant.components import websocket_api
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import HomeAssistantError
13 from homeassistant.helpers import config_validation as cv
14 from homeassistant.helpers.json import json_fragment
15 
16 from .const import CONF_URL_PATH, DOMAIN, ConfigNotFound
17 from .dashboard import LovelaceStorage
18 
19 
20 def _handle_errors(func):
21  """Handle error with WebSocket calls."""
22 
23  @wraps(func)
24  async def send_with_error_handling(
25  hass: HomeAssistant,
27  msg: dict[str, Any],
28  ) -> None:
29  url_path = msg.get(CONF_URL_PATH)
30  config: LovelaceStorage | None = hass.data[DOMAIN]["dashboards"].get(url_path)
31 
32  if config is None:
33  connection.send_error(
34  msg["id"], "config_not_found", f"Unknown config specified: {url_path}"
35  )
36  return
37 
38  error = None
39  try:
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)
45 
46  if error is not None:
47  connection.send_error(msg["id"], *error)
48  return
49 
50  connection.send_result(msg["id"], result)
51 
52  return send_with_error_handling
53 
54 
55 @websocket_api.async_response
57  hass: HomeAssistant,
59  msg: dict[str, Any],
60 ) -> None:
61  """Send Lovelace UI resources over WebSocket connection.
62 
63  This function is used in YAML mode.
64  """
65  await websocket_lovelace_resources_impl(hass, connection, msg)
66 
67 
69  hass: HomeAssistant,
71  msg: dict[str, Any],
72 ) -> None:
73  """Help send Lovelace UI resources over WebSocket connection.
74 
75  This function is called by both Storage and YAML mode WS handlers.
76  """
77  resources = hass.data[DOMAIN]["resources"]
78 
79  if hass.config.safe_mode:
80  connection.send_result(msg["id"], [])
81  return
82 
83  if not resources.loaded:
84  await resources.async_load()
85  resources.loaded = True
86 
87  connection.send_result(msg["id"], resources.async_items())
88 
89 
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),
92  }
93 )
94 @websocket_api.async_response
95 @_handle_errors
97  hass: HomeAssistant,
99  msg: dict[str, Any],
100  config: LovelaceStorage,
101 ) -> json_fragment:
102  """Send Lovelace UI config over WebSocket connection."""
103  return await config.async_json(msg["force"])
104 
105 
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),
109  }
110 )
111 @websocket_api.async_response
112 @_handle_errors
114  hass: HomeAssistant,
115  connection: websocket_api.ActiveConnection,
116  msg: dict[str, Any],
117  config: LovelaceStorage,
118 ) -> None:
119  """Save Lovelace UI configuration."""
120  await config.async_save(msg["config"])
121 
122 
123 @websocket_api.require_admin
124 @websocket_api.websocket_command( { "type": "lovelace/config/delete", vol.Optional(CONF_URL_PATH): vol.Any(None, cv.string),
125  }
126 )
127 @websocket_api.async_response
128 @_handle_errors
130  hass: HomeAssistant,
131  connection: websocket_api.ActiveConnection,
132  msg: dict[str, Any],
133  config: LovelaceStorage,
134 ) -> None:
135  """Delete Lovelace UI configuration."""
136  await config.async_delete()
137 
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None websocket_lovelace_resources(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
Definition: websocket.py:60
None websocket_lovelace_delete_config(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg, LovelaceStorage config)
Definition: websocket.py:143
None websocket_lovelace_save_config(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg, LovelaceStorage config)
Definition: websocket.py:124
None websocket_lovelace_resources_impl(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
Definition: websocket.py:72
json_fragment websocket_lovelace_config(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg, LovelaceStorage config)
Definition: websocket.py:104