forked from home-assistant/core
/
__init__.py
161 lines (136 loc) · 4.86 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""Register a custom front end panel."""
import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.loader import bind_hass
_LOGGER = logging.getLogger(__name__)
DOMAIN = "panel_custom"
CONF_COMPONENT_NAME = "name"
CONF_SIDEBAR_TITLE = "sidebar_title"
CONF_SIDEBAR_ICON = "sidebar_icon"
CONF_URL_PATH = "url_path"
CONF_CONFIG = "config"
CONF_JS_URL = "js_url"
CONF_MODULE_URL = "module_url"
CONF_EMBED_IFRAME = "embed_iframe"
CONF_TRUST_EXTERNAL_SCRIPT = "trust_external_script"
CONF_URL_EXCLUSIVE_GROUP = "url_exclusive_group"
CONF_REQUIRE_ADMIN = "require_admin"
DEFAULT_EMBED_IFRAME = False
DEFAULT_TRUST_EXTERNAL = False
DEFAULT_ICON = "mdi:bookmark"
LEGACY_URL = "/api/panel_custom/{}"
PANEL_DIR = "panels"
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.All(
cv.ensure_list,
[
vol.Schema(
{
vol.Required(CONF_COMPONENT_NAME): cv.string,
vol.Optional(CONF_SIDEBAR_TITLE): cv.string,
vol.Optional(CONF_SIDEBAR_ICON, default=DEFAULT_ICON): cv.icon,
vol.Optional(CONF_URL_PATH): cv.string,
vol.Optional(CONF_CONFIG): dict,
vol.Optional(
CONF_JS_URL,
): cv.string,
vol.Optional(
CONF_MODULE_URL,
): cv.string,
vol.Optional(
CONF_EMBED_IFRAME, default=DEFAULT_EMBED_IFRAME
): cv.boolean,
vol.Optional(
CONF_TRUST_EXTERNAL_SCRIPT,
default=DEFAULT_TRUST_EXTERNAL,
): cv.boolean,
vol.Optional(CONF_REQUIRE_ADMIN, default=False): cv.boolean,
}
),
],
)
},
extra=vol.ALLOW_EXTRA,
)
@bind_hass
async def async_register_panel(
hass,
# The url to serve the panel
frontend_url_path,
# The webcomponent name that loads your panel
webcomponent_name,
# Title/icon for sidebar
sidebar_title=None,
sidebar_icon=None,
# JS source of your panel
js_url=None,
# JS module of your panel
module_url=None,
# If your panel should be run inside an iframe
embed_iframe=DEFAULT_EMBED_IFRAME,
# Should user be asked for confirmation when loading external source
trust_external=DEFAULT_TRUST_EXTERNAL,
# Configuration to be passed to the panel
config=None,
# If your panel should only be shown to admin users
require_admin=False,
):
"""Register a new custom panel."""
if js_url is None and module_url is None:
raise ValueError("Either js_url, module_url or html_url is required.")
if config is not None and not isinstance(config, dict):
raise ValueError("Config needs to be a dictionary.")
custom_panel_config = {
"name": webcomponent_name,
"embed_iframe": embed_iframe,
"trust_external": trust_external,
}
if js_url is not None:
custom_panel_config["js_url"] = js_url
if module_url is not None:
custom_panel_config["module_url"] = module_url
if config is not None:
# Make copy because we're mutating it
config = dict(config)
else:
config = {}
config["_panel_custom"] = custom_panel_config
hass.components.frontend.async_register_built_in_panel(
component_name="custom",
sidebar_title=sidebar_title,
sidebar_icon=sidebar_icon,
frontend_url_path=frontend_url_path,
config=config,
require_admin=require_admin,
)
async def async_setup(hass, config):
"""Initialize custom panel."""
if DOMAIN not in config:
return True
for panel in config[DOMAIN]:
name = panel[CONF_COMPONENT_NAME]
kwargs = {
"webcomponent_name": panel[CONF_COMPONENT_NAME],
"frontend_url_path": panel.get(CONF_URL_PATH, name),
"sidebar_title": panel.get(CONF_SIDEBAR_TITLE),
"sidebar_icon": panel.get(CONF_SIDEBAR_ICON),
"config": panel.get(CONF_CONFIG),
"trust_external": panel[CONF_TRUST_EXTERNAL_SCRIPT],
"embed_iframe": panel[CONF_EMBED_IFRAME],
"require_admin": panel[CONF_REQUIRE_ADMIN],
}
if CONF_JS_URL in panel:
kwargs["js_url"] = panel[CONF_JS_URL]
if CONF_MODULE_URL in panel:
kwargs["module_url"] = panel[CONF_MODULE_URL]
try:
await async_register_panel(hass, **kwargs)
except ValueError as err:
_LOGGER.error(
"Unable to register panel %s: %s",
panel.get(CONF_SIDEBAR_TITLE, name),
err,
)
return True