Home Assistant Unofficial Reference 2024.12.1
panel.py
Go to the documentation of this file.
1 """Dynalite API interface for the frontend."""
2 
3 from dynalite_panel import get_build_id, locate_dir
4 import voluptuous as vol
5 
6 from homeassistant.components import panel_custom, websocket_api
7 from homeassistant.components.cover import DEVICE_CLASSES
8 from homeassistant.components.http import StaticPathConfig
9 from homeassistant.const import CONF_DEFAULT, CONF_HOST, CONF_NAME, CONF_PORT
10 from homeassistant.core import HomeAssistant, callback
11 
12 from .const import (
13  CONF_ACTIVE,
14  CONF_AREA,
15  CONF_AUTO_DISCOVER,
16  CONF_PRESET,
17  CONF_TEMPLATE,
18  DEFAULT_NAME,
19  DEFAULT_PORT,
20  DOMAIN,
21  LOGGER,
22 )
23 from .schema import BRIDGE_SCHEMA
24 
25 URL_BASE = "/dynalite_static"
26 
27 RELEVANT_CONFS = [
28  CONF_NAME,
29  CONF_HOST,
30  CONF_PORT,
31  CONF_AUTO_DISCOVER,
32  CONF_AREA,
33  CONF_DEFAULT,
34  CONF_ACTIVE,
35  CONF_PRESET,
36  CONF_TEMPLATE,
37 ]
38 
39 
40 @websocket_api.websocket_command( { vol.Required("type"): "dynalite/get-config",
41  }
42 )
43 @websocket_api.require_admin
44 @callback
46  hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict
47 ) -> None:
48  """Retrieve the Dynalite config for the frontend."""
49  entries = hass.config_entries.async_entries(DOMAIN)
50  relevant_config = {
51  entry.entry_id: {
52  conf: entry.data[conf] for conf in RELEVANT_CONFS if conf in entry.data
53  }
54  for entry in entries
55  }
56  dynalite_defaults = {
57  "DEFAULT_NAME": DEFAULT_NAME,
58  "DEVICE_CLASSES": DEVICE_CLASSES,
59  "DEFAULT_PORT": DEFAULT_PORT,
60  }
61  connection.send_result(
62  msg["id"], {"config": relevant_config, "default": dynalite_defaults}
63  )
64 
65 
66 @websocket_api.websocket_command( { vol.Required("type"): "dynalite/save-config",
67  vol.Required("entry_id"): str,
68  vol.Required("config"): BRIDGE_SCHEMA,
69  }
70 )
71 @websocket_api.require_admin
72 @callback
74  hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict
75 ) -> None:
76  """Retrieve the Dynalite config for the frontend."""
77  entry_id = msg["entry_id"]
78  entry = hass.config_entries.async_get_entry(entry_id)
79  if not entry:
80  LOGGER.error(
81  "Dynalite - received updated config for invalid entry - %s", entry_id
82  )
83  connection.send_result(msg["id"], {"error": True})
84  return
85  message_conf = msg["config"]
86  message_data = {
87  conf: message_conf[conf] for conf in RELEVANT_CONFS if conf in message_conf
88  }
89  LOGGER.debug("Updating Dynalite config entry")
90  hass.config_entries.async_update_entry(entry, data=message_data)
91  connection.send_result(msg["id"], {})
92 
93 
94 async def async_register_dynalite_frontend(hass: HomeAssistant):
95  """Register the Dynalite frontend configuration panel."""
96  websocket_api.async_register_command(hass, get_dynalite_config)
97  websocket_api.async_register_command(hass, save_dynalite_config)
98  path = locate_dir()
99  build_id = get_build_id()
100  await hass.http.async_register_static_paths(
101  [StaticPathConfig(URL_BASE, path, cache_headers=(build_id != "dev"))]
102  )
103 
104  await panel_custom.async_register_panel(
105  hass=hass,
106  frontend_url_path=DOMAIN,
107  config_panel_domain=DOMAIN,
108  webcomponent_name="dynalite-panel",
109  module_url=f"{URL_BASE}/entrypoint-{build_id}.js",
110  embed_iframe=True,
111  require_admin=True,
112  )
113 
None save_dynalite_config(HomeAssistant hass, websocket_api.ActiveConnection connection, dict msg)
Definition: panel.py:77
None get_dynalite_config(HomeAssistant hass, websocket_api.ActiveConnection connection, dict msg)
Definition: panel.py:47
def async_register_dynalite_frontend(HomeAssistant hass)
Definition: panel.py:96