Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Handle the frontend for Home Assistant."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Iterator
6 from functools import lru_cache, partial
7 import logging
8 import os
9 import pathlib
10 from typing import Any, TypedDict
11 
12 from aiohttp import hdrs, web, web_urldispatcher
13 import jinja2
14 from propcache import cached_property
15 import voluptuous as vol
16 from yarl import URL
17 
18 from homeassistant.components import onboarding, websocket_api
19 from homeassistant.components.http import KEY_HASS, HomeAssistantView, StaticPathConfig
20 from homeassistant.components.websocket_api import ActiveConnection
21 from homeassistant.config import async_hass_config_yaml
22 from homeassistant.const import (
23  CONF_MODE,
24  CONF_NAME,
25  EVENT_PANELS_UPDATED,
26  EVENT_THEMES_UPDATED,
27 )
28 from homeassistant.core import HomeAssistant, ServiceCall, callback
29 from homeassistant.helpers import service
31 from homeassistant.helpers.icon import async_get_icons
32 from homeassistant.helpers.json import json_dumps_sorted
33 from homeassistant.helpers.storage import Store
34 from homeassistant.helpers.translation import async_get_translations
35 from homeassistant.helpers.typing import ConfigType
36 from homeassistant.loader import async_get_integration, bind_hass
37 from homeassistant.util.hass_dict import HassKey
38 
39 from .storage import async_setup_frontend_storage
40 
41 DOMAIN = "frontend"
42 CONF_THEMES = "themes"
43 CONF_THEMES_MODES = "modes"
44 CONF_THEMES_LIGHT = "light"
45 CONF_THEMES_DARK = "dark"
46 CONF_EXTRA_HTML_URL = "extra_html_url"
47 CONF_EXTRA_HTML_URL_ES5 = "extra_html_url_es5"
48 CONF_EXTRA_MODULE_URL = "extra_module_url"
49 CONF_EXTRA_JS_URL_ES5 = "extra_js_url_es5"
50 CONF_FRONTEND_REPO = "development_repo"
51 CONF_JS_VERSION = "javascript_version"
52 
53 DEFAULT_THEME_COLOR = "#03A9F4"
54 
55 
56 DATA_PANELS = "frontend_panels"
57 DATA_JS_VERSION = "frontend_js_version"
58 DATA_EXTRA_MODULE_URL = "frontend_extra_module_url"
59 DATA_EXTRA_JS_URL_ES5 = "frontend_extra_js_url_es5"
60 
61 DATA_WS_SUBSCRIBERS: HassKey[set[tuple[websocket_api.ActiveConnection, int]]] = HassKey(
62  "frontend_ws_subscribers"
63 )
64 
65 THEMES_STORAGE_KEY = f"{DOMAIN}_theme"
66 THEMES_STORAGE_VERSION = 1
67 THEMES_SAVE_DELAY = 60
68 DATA_THEMES_STORE = "frontend_themes_store"
69 DATA_THEMES = "frontend_themes"
70 DATA_DEFAULT_THEME = "frontend_default_theme"
71 DATA_DEFAULT_DARK_THEME = "frontend_default_dark_theme"
72 DEFAULT_THEME = "default"
73 VALUE_NO_THEME = "none"
74 
75 PRIMARY_COLOR = "primary-color"
76 
77 _LOGGER = logging.getLogger(__name__)
78 
79 EXTENDED_THEME_SCHEMA = vol.Schema(
80  {
81  # Theme variables that apply to all modes
82  cv.string: cv.string,
83  # Mode specific theme variables
84  vol.Optional(CONF_THEMES_MODES): vol.Schema(
85  {
86  vol.Optional(CONF_THEMES_LIGHT): vol.Schema({cv.string: cv.string}),
87  vol.Optional(CONF_THEMES_DARK): vol.Schema({cv.string: cv.string}),
88  }
89  ),
90  }
91 )
92 
93 THEME_SCHEMA = vol.Schema(
94  {
95  cv.string: (
96  vol.Any(
97  # Legacy theme scheme
98  {cv.string: cv.string},
99  # New extended schema with mode support
100  EXTENDED_THEME_SCHEMA,
101  )
102  )
103  }
104 )
105 
106 CONFIG_SCHEMA = vol.Schema(
107  {
108  DOMAIN: vol.Schema(
109  {
110  vol.Optional(CONF_FRONTEND_REPO): cv.isdir,
111  vol.Optional(CONF_THEMES): THEME_SCHEMA,
112  vol.Optional(CONF_EXTRA_MODULE_URL): vol.All(
113  cv.ensure_list, [cv.string]
114  ),
115  vol.Optional(CONF_EXTRA_JS_URL_ES5): vol.All(
116  cv.ensure_list, [cv.string]
117  ),
118  # We no longer use these options.
119  vol.Optional(CONF_EXTRA_HTML_URL): cv.match_all,
120  vol.Optional(CONF_EXTRA_HTML_URL_ES5): cv.match_all,
121  vol.Optional(CONF_JS_VERSION): cv.match_all,
122  },
123  )
124  },
125  extra=vol.ALLOW_EXTRA,
126 )
127 
128 SERVICE_SET_THEME = "set_theme"
129 SERVICE_RELOAD_THEMES = "reload_themes"
130 
131 
132 class Manifest:
133  """Manage the manifest.json contents."""
134 
135  def __init__(self, data: dict) -> None:
136  """Init the manifest manager."""
137  self.manifestmanifest = data
138  self._serialize_serialize()
139 
140  def __getitem__(self, key: str) -> Any:
141  """Return an item in the manifest."""
142  return self.manifestmanifest[key]
143 
144  @property
145  def json(self) -> str:
146  """Return the serialized manifest."""
147  return self._serialized_serialized
148 
149  def _serialize(self) -> None:
150  self._serialized_serialized = json_dumps_sorted(self.manifestmanifest)
151 
152  def update_key(self, key: str, val: str) -> None:
153  """Add a keyval to the manifest.json."""
154  self.manifestmanifest[key] = val
155  self._serialize_serialize()
156 
157 
158 MANIFEST_JSON = Manifest(
159  {
160  "background_color": "#FFFFFF",
161  "description": (
162  "Home automation platform that puts local control and privacy first."
163  ),
164  "dir": "ltr",
165  "display": "standalone",
166  "icons": [
167  {
168  "src": f"/static/icons/favicon-{size}x{size}.png",
169  "sizes": f"{size}x{size}",
170  "type": "image/png",
171  "purpose": "any",
172  }
173  for size in (192, 384, 512, 1024)
174  ]
175  + [
176  {
177  "src": f"/static/icons/maskable_icon-{size}x{size}.png",
178  "sizes": f"{size}x{size}",
179  "type": "image/png",
180  "purpose": "maskable",
181  }
182  for size in (48, 72, 96, 128, 192, 384, 512)
183  ],
184  "screenshots": [
185  {
186  "src": "/static/images/screenshots/screenshot-1.png",
187  "sizes": "413x792",
188  "type": "image/png",
189  }
190  ],
191  "lang": "en-US",
192  "name": "Home Assistant",
193  "short_name": "Home Assistant",
194  "start_url": "/?homescreen=1",
195  "id": "/?homescreen=1",
196  "theme_color": DEFAULT_THEME_COLOR,
197  "prefer_related_applications": True,
198  "related_applications": [
199  {"platform": "play", "id": "io.homeassistant.companion.android"}
200  ],
201  }
202 )
203 
204 
206  """Manage urls to be used on the frontend.
207 
208  This is abstracted into a class because
209  some integrations add a remove these directly
210  on hass.data
211  """
212 
213  def __init__(
214  self,
215  on_change: Callable[[str, str], None],
216  urls: list[str],
217  ) -> None:
218  """Init the url manager."""
219  self._on_change_on_change = on_change
220  self.urlsurls = frozenset(urls)
221 
222  def add(self, url: str) -> None:
223  """Add a url to the set."""
224  self.urlsurls = frozenset([*self.urlsurls, url])
225  self._on_change_on_change("added", url)
226 
227  def remove(self, url: str) -> None:
228  """Remove a url from the set."""
229  self.urlsurls = self.urlsurls - {url}
230  self._on_change_on_change("removed", url)
231 
232 
233 class Panel:
234  """Abstract class for panels."""
235 
236  # Name of the webcomponent
237  component_name: str
238 
239  # Icon to show in the sidebar
240  sidebar_icon: str | None = None
241 
242  # Title to show in the sidebar
243  sidebar_title: str | None = None
244 
245  # Url to show the panel in the frontend
246  frontend_url_path: str | None = None
247 
248  # Config to pass to the webcomponent
249  config: dict[str, Any] | None = None
250 
251  # If the panel should only be visible to admins
252  require_admin = False
253 
254  # If the panel is a configuration panel for a integration
255  config_panel_domain: str | None = None
256 
257  def __init__(
258  self,
259  component_name: str,
260  sidebar_title: str | None,
261  sidebar_icon: str | None,
262  frontend_url_path: str | None,
263  config: dict[str, Any] | None,
264  require_admin: bool,
265  config_panel_domain: str | None,
266  ) -> None:
267  """Initialize a built-in panel."""
268  self.component_namecomponent_name = component_name
269  self.sidebar_titlesidebar_title = sidebar_title
270  self.sidebar_iconsidebar_icon = sidebar_icon
271  self.frontend_url_pathfrontend_url_path = frontend_url_path or component_name
272  self.configconfig = config
273  self.require_adminrequire_adminrequire_admin = require_admin
274  self.config_panel_domainconfig_panel_domain = config_panel_domain
275 
276  @callback
277  def to_response(self) -> PanelRespons:
278  """Panel as dictionary."""
279  return {
280  "component_name": self.component_namecomponent_name,
281  "icon": self.sidebar_iconsidebar_icon,
282  "title": self.sidebar_titlesidebar_title,
283  "config": self.configconfig,
284  "url_path": self.frontend_url_pathfrontend_url_path,
285  "require_admin": self.require_adminrequire_adminrequire_admin,
286  "config_panel_domain": self.config_panel_domainconfig_panel_domain,
287  }
288 
289 
290 @bind_hass
291 @callback
293  hass: HomeAssistant,
294  component_name: str,
295  sidebar_title: str | None = None,
296  sidebar_icon: str | None = None,
297  frontend_url_path: str | None = None,
298  config: dict[str, Any] | None = None,
299  require_admin: bool = False,
300  *,
301  update: bool = False,
302  config_panel_domain: str | None = None,
303 ) -> None:
304  """Register a built-in panel."""
305  panel = Panel(
306  component_name,
307  sidebar_title,
308  sidebar_icon,
309  frontend_url_path,
310  config,
311  require_admin,
312  config_panel_domain,
313  )
314 
315  panels = hass.data.setdefault(DATA_PANELS, {})
316 
317  if not update and panel.frontend_url_path in panels:
318  raise ValueError(f"Overwriting panel {panel.frontend_url_path}")
319 
320  panels[panel.frontend_url_path] = panel
321 
322  hass.bus.async_fire(EVENT_PANELS_UPDATED)
323 
324 
325 @bind_hass
326 @callback
328  hass: HomeAssistant, frontend_url_path: str, *, warn_if_unknown: bool = True
329 ) -> None:
330  """Remove a built-in panel."""
331  panel = hass.data.get(DATA_PANELS, {}).pop(frontend_url_path, None)
332 
333  if panel is None:
334  if warn_if_unknown:
335  _LOGGER.warning("Removing unknown panel %s", frontend_url_path)
336  return
337 
338  hass.bus.async_fire(EVENT_PANELS_UPDATED)
339 
340 
341 def add_extra_js_url(hass: HomeAssistant, url: str, es5: bool = False) -> None:
342  """Register extra js or module url to load.
343 
344  This function allows custom integrations to register extra js or module.
345  """
346  key = DATA_EXTRA_JS_URL_ES5 if es5 else DATA_EXTRA_MODULE_URL
347  hass.data[key].add(url)
348 
349 
350 def remove_extra_js_url(hass: HomeAssistant, url: str, es5: bool = False) -> None:
351  """Remove extra js or module url to load.
352 
353  This function allows custom integrations to remove extra js or module.
354  """
355  key = DATA_EXTRA_JS_URL_ES5 if es5 else DATA_EXTRA_MODULE_URL
356  hass.data[key].remove(url)
357 
358 
359 def add_manifest_json_key(key: str, val: Any) -> None:
360  """Add a keyval to the manifest.json."""
361  MANIFEST_JSON.update_key(key, val)
362 
363 
364 def _frontend_root(dev_repo_path: str | None) -> pathlib.Path:
365  """Return root path to the frontend files."""
366  if dev_repo_path is not None:
367  return pathlib.Path(dev_repo_path) / "hass_frontend"
368  # Keep import here so that we can import frontend without installing reqs
369  # pylint: disable-next=import-outside-toplevel
370  import hass_frontend
371 
372  return hass_frontend.where()
373 
374 
375 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
376  """Set up the serving of the frontend."""
377  await async_setup_frontend_storage(hass)
378  websocket_api.async_register_command(hass, websocket_get_icons)
379  websocket_api.async_register_command(hass, websocket_get_panels)
380  websocket_api.async_register_command(hass, websocket_get_themes)
381  websocket_api.async_register_command(hass, websocket_get_translations)
382  websocket_api.async_register_command(hass, websocket_get_version)
383  websocket_api.async_register_command(hass, websocket_subscribe_extra_js)
384  hass.http.register_view(ManifestJSONView())
385 
386  conf = config.get(DOMAIN, {})
387 
388  for key in (CONF_EXTRA_HTML_URL, CONF_EXTRA_HTML_URL_ES5, CONF_JS_VERSION):
389  if key in conf:
390  _LOGGER.error(
391  "Please remove %s from your frontend config. It is no longer supported",
392  key,
393  )
394 
395  repo_path = conf.get(CONF_FRONTEND_REPO)
396  is_dev = repo_path is not None
397  root_path = _frontend_root(repo_path)
398 
399  static_paths_configs: list[StaticPathConfig] = []
400 
401  for path, should_cache in (
402  ("service_worker.js", False),
403  ("sw-modern.js", False),
404  ("sw-modern.js.map", False),
405  ("sw-legacy.js", False),
406  ("sw-legacy.js.map", False),
407  ("robots.txt", False),
408  ("onboarding.html", not is_dev),
409  ("static", not is_dev),
410  ("frontend_latest", not is_dev),
411  ("frontend_es5", not is_dev),
412  ):
413  static_paths_configs.append(
414  StaticPathConfig(f"/{path}", str(root_path / path), should_cache)
415  )
416 
417  static_paths_configs.append(
418  StaticPathConfig("/auth/authorize", str(root_path / "authorize.html"), False)
419  )
420  # https://wicg.github.io/change-password-url/
421  hass.http.register_redirect(
422  "/.well-known/change-password", "/profile", redirect_exc=web.HTTPFound
423  )
424 
425  local = hass.config.path("www")
426  if await hass.async_add_executor_job(os.path.isdir, local):
427  static_paths_configs.append(StaticPathConfig("/local", local, not is_dev))
428 
429  await hass.http.async_register_static_paths(static_paths_configs)
430  # Shopping list panel was replaced by todo panel in 2023.11
431  hass.http.register_redirect("/shopping-list", "/todo")
432 
433  hass.http.app.router.register_resource(IndexView(repo_path, hass))
434 
435  async_register_built_in_panel(hass, "profile")
436 
438  hass,
439  "developer-tools",
440  require_admin=True,
441  sidebar_title="developer_tools",
442  sidebar_icon="hass:hammer",
443  )
444 
445  @callback
446  def async_change_listener(
447  resource_type: str,
448  change_type: str,
449  url: str,
450  ) -> None:
451  subscribers = hass.data[DATA_WS_SUBSCRIBERS]
452  json_msg = {
453  "change_type": change_type,
454  "item": {"type": resource_type, "url": url},
455  }
456  for connection, msg_id in subscribers:
457  connection.send_message(websocket_api.event_message(msg_id, json_msg))
458 
459  hass.data[DATA_EXTRA_MODULE_URL] = UrlManager(
460  partial(async_change_listener, "module"), conf.get(CONF_EXTRA_MODULE_URL, [])
461  )
462  hass.data[DATA_EXTRA_JS_URL_ES5] = UrlManager(
463  partial(async_change_listener, "es5"), conf.get(CONF_EXTRA_JS_URL_ES5, [])
464  )
465  hass.data[DATA_WS_SUBSCRIBERS] = set()
466 
467  await _async_setup_themes(hass, conf.get(CONF_THEMES))
468 
469  return True
470 
471 
473  hass: HomeAssistant, themes: dict[str, Any] | None
474 ) -> None:
475  """Set up themes data and services."""
476  hass.data[DATA_THEMES] = themes or {}
477 
478  store = hass.data[DATA_THEMES_STORE] = Store(
479  hass, THEMES_STORAGE_VERSION, THEMES_STORAGE_KEY
480  )
481 
482  if not (theme_data := await store.async_load()) or not isinstance(theme_data, dict):
483  theme_data = {}
484  theme_name = theme_data.get(DATA_DEFAULT_THEME, DEFAULT_THEME)
485  dark_theme_name = theme_data.get(DATA_DEFAULT_DARK_THEME)
486 
487  if theme_name == DEFAULT_THEME or theme_name in hass.data[DATA_THEMES]:
488  hass.data[DATA_DEFAULT_THEME] = theme_name
489  else:
490  hass.data[DATA_DEFAULT_THEME] = DEFAULT_THEME
491 
492  if dark_theme_name == DEFAULT_THEME or dark_theme_name in hass.data[DATA_THEMES]:
493  hass.data[DATA_DEFAULT_DARK_THEME] = dark_theme_name
494 
495  @callback
496  def update_theme_and_fire_event() -> None:
497  """Update theme_color in manifest."""
498  name = hass.data[DATA_DEFAULT_THEME]
499  themes = hass.data[DATA_THEMES]
500  if name != DEFAULT_THEME:
501  MANIFEST_JSON.update_key(
502  "theme_color",
503  themes[name].get(
504  "app-header-background-color",
505  themes[name].get(PRIMARY_COLOR, DEFAULT_THEME_COLOR),
506  ),
507  )
508  else:
509  MANIFEST_JSON.update_key("theme_color", DEFAULT_THEME_COLOR)
510  hass.bus.async_fire(EVENT_THEMES_UPDATED)
511 
512  @callback
513  def set_theme(call: ServiceCall) -> None:
514  """Set backend-preferred theme."""
515  name = call.data[CONF_NAME]
516  mode = call.data.get("mode", "light")
517 
518  if (
519  name not in (DEFAULT_THEME, VALUE_NO_THEME)
520  and name not in hass.data[DATA_THEMES]
521  ):
522  _LOGGER.warning("Theme %s not found", name)
523  return
524 
525  light_mode = mode == "light"
526 
527  theme_key = DATA_DEFAULT_THEME if light_mode else DATA_DEFAULT_DARK_THEME
528 
529  if name == VALUE_NO_THEME:
530  to_set = DEFAULT_THEME if light_mode else None
531  else:
532  _LOGGER.info("Theme %s set as default %s theme", name, mode)
533  to_set = name
534 
535  hass.data[theme_key] = to_set
536  store.async_delay_save(
537  lambda: {
538  DATA_DEFAULT_THEME: hass.data[DATA_DEFAULT_THEME],
539  DATA_DEFAULT_DARK_THEME: hass.data.get(DATA_DEFAULT_DARK_THEME),
540  },
541  THEMES_SAVE_DELAY,
542  )
543  update_theme_and_fire_event()
544 
545  async def reload_themes(_: ServiceCall) -> None:
546  """Reload themes."""
547  config = await async_hass_config_yaml(hass)
548  new_themes = config.get(DOMAIN, {}).get(CONF_THEMES, {})
549  hass.data[DATA_THEMES] = new_themes
550  if hass.data[DATA_DEFAULT_THEME] not in new_themes:
551  hass.data[DATA_DEFAULT_THEME] = DEFAULT_THEME
552  if (
553  hass.data.get(DATA_DEFAULT_DARK_THEME)
554  and hass.data.get(DATA_DEFAULT_DARK_THEME) not in new_themes
555  ):
556  hass.data[DATA_DEFAULT_DARK_THEME] = None
557  update_theme_and_fire_event()
558 
559  service.async_register_admin_service(
560  hass,
561  DOMAIN,
562  SERVICE_SET_THEME,
563  set_theme,
564  vol.Schema(
565  {
566  vol.Required(CONF_NAME): cv.string,
567  vol.Optional(CONF_MODE): vol.Any("dark", "light"),
568  }
569  ),
570  )
571 
572  service.async_register_admin_service(
573  hass, DOMAIN, SERVICE_RELOAD_THEMES, reload_themes
574  )
575 
576 
577 @callback
578 @lru_cache(maxsize=1)
579 def _async_render_index_cached(template: jinja2.Template, **kwargs: Any) -> str:
580  return template.render(**kwargs)
581 
582 
583 class IndexView(web_urldispatcher.AbstractResource):
584  """Serve the frontend."""
585 
586  def __init__(self, repo_path: str | None, hass: HomeAssistant) -> None:
587  """Initialize the frontend view."""
588  super().__init__(name="frontend:index")
589  self.repo_pathrepo_path = repo_path
590  self.hasshass = hass
591  self._template_cache_template_cache: jinja2.Template | None = None
592 
593  @cached_property
594  def canonical(self) -> str:
595  """Return resource's canonical path."""
596  return "/"
597 
598  @cached_property
599  def _route(self) -> web_urldispatcher.ResourceRoute:
600  """Return the index route."""
601  return web_urldispatcher.ResourceRoute("GET", self.getget, self)
602 
603  def url_for(self, **kwargs: str) -> URL:
604  """Construct url for resource with additional params."""
605  return URL("/")
606 
607  async def resolve(
608  self, request: web.Request
609  ) -> tuple[web_urldispatcher.UrlMappingMatchInfo | None, set[str]]:
610  """Resolve resource.
611 
612  Return (UrlMappingMatchInfo, allowed_methods) pair.
613  """
614  if (
615  request.path != "/"
616  and (parts := request.rel_url.parts)
617  and len(parts) > 1
618  and parts[1] not in self.hasshass.data[DATA_PANELS]
619  ):
620  return None, set()
621 
622  if request.method != hdrs.METH_GET:
623  return None, {"GET"}
624 
625  return web_urldispatcher.UrlMappingMatchInfo({}, self._route_route), {"GET"}
626 
627  def add_prefix(self, prefix: str) -> None:
628  """Add a prefix to processed URLs.
629 
630  Required for subapplications support.
631  """
632 
633  def get_info(self) -> dict[str, list[str]]: # type: ignore[override]
634  """Return a dict with additional info useful for introspection."""
635  return {"panels": list(self.hasshass.data[DATA_PANELS])}
636 
637  def raw_match(self, path: str) -> bool:
638  """Perform a raw match against path."""
639  return False
640 
641  def get_template(self) -> jinja2.Template:
642  """Get template."""
643  if (tpl := self._template_cache_template_cache) is None:
644  with (_frontend_root(self.repo_pathrepo_path) / "index.html").open(
645  encoding="utf8"
646  ) as file:
647  tpl = jinja2.Template(file.read())
648 
649  # Cache template if not running from repository
650  if self.repo_pathrepo_path is None:
651  self._template_cache_template_cache = tpl
652 
653  return tpl
654 
655  async def get(self, request: web.Request) -> web.Response:
656  """Serve the index page for panel pages."""
657  hass = request.app[KEY_HASS]
658 
659  if not onboarding.async_is_onboarded(hass):
660  return web.Response(status=302, headers={"location": "/onboarding.html"})
661 
662  template = self._template_cache_template_cache or await hass.async_add_executor_job(
663  self.get_templateget_template
664  )
665 
666  extra_modules: frozenset[str]
667  extra_js_es5: frozenset[str]
668  if hass.config.safe_mode:
669  extra_modules = frozenset()
670  extra_js_es5 = frozenset()
671  else:
672  extra_modules = hass.data[DATA_EXTRA_MODULE_URL].urls
673  extra_js_es5 = hass.data[DATA_EXTRA_JS_URL_ES5].urls
674 
675  response = web.Response(
677  template,
678  theme_color=MANIFEST_JSON["theme_color"],
679  extra_modules=extra_modules,
680  extra_js_es5=extra_js_es5,
681  ),
682  content_type="text/html",
683  )
684  response.enable_compression()
685  return response
686 
687  def __len__(self) -> int:
688  """Return length of resource."""
689  return 1
690 
691  def __iter__(self) -> Iterator[web_urldispatcher.ResourceRoute]:
692  """Iterate over routes."""
693  return iter([self._route_route])
694 
695 
696 class ManifestJSONView(HomeAssistantView):
697  """View to return a manifest.json."""
698 
699  requires_auth = False
700  url = "/manifest.json"
701  name = "manifestjson"
702 
703  @callback
704  def get(self, request: web.Request) -> web.Response:
705  """Return the manifest.json."""
706  response = web.Response(
707  text=MANIFEST_JSON.json, content_type="application/manifest+json"
708  )
709  response.enable_compression()
710  return response
711 
712 
713 @websocket_api.websocket_command( { "type": "frontend/get_icons", vol.Required("category"): vol.In({"entity", "entity_component", "services"}),
714  vol.Optional("integration"): vol.All(cv.ensure_list, [str]),
715  }
716 )
717 @websocket_api.async_response
718 async def websocket_get_icons(
719  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
720 ) -> None:
721  """Handle get icons command."""
722  resources = await async_get_icons(
723  hass,
724  msg["category"],
725  msg.get("integration"),
726  )
727  connection.send_message(
728  websocket_api.result_message(msg["id"], {"resources": resources})
729  )
730 
731 
732 @callback
733 @websocket_api.websocket_command({"type": "get_panels"})
735  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
736 ) -> None:
737  """Handle get panels command."""
738  user_is_admin = connection.user.is_admin
739  panels = {
740  panel_key: panel.to_response()
741  for panel_key, panel in connection.hass.data[DATA_PANELS].items()
742  if user_is_admin or not panel.require_admin
743  }
744 
745  connection.send_message(websocket_api.result_message(msg["id"], panels))
746 
747 
748 @callback
749 @websocket_api.websocket_command({"type": "frontend/get_themes"})
751  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
752 ) -> None:
753  """Handle get themes command."""
754  if hass.config.recovery_mode or hass.config.safe_mode:
755  connection.send_message(
756  websocket_api.result_message(
757  msg["id"],
758  {
759  "themes": {},
760  "default_theme": "default",
761  },
762  )
763  )
764  return
765 
766  connection.send_message(
767  websocket_api.result_message(
768  msg["id"],
769  {
770  "themes": hass.data[DATA_THEMES],
771  "default_theme": hass.data[DATA_DEFAULT_THEME],
772  "default_dark_theme": hass.data.get(DATA_DEFAULT_DARK_THEME),
773  },
774  )
775  )
776 
777 
778 @websocket_api.websocket_command( { "type": "frontend/get_translations", vol.Required("language"): str,
779  vol.Required("category"): str,
780  vol.Optional("integration"): vol.All(cv.ensure_list, [str]),
781  vol.Optional("config_flow"): bool,
782  }
783 )
784 @websocket_api.async_response
786  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
787 ) -> None:
788  """Handle get translations command."""
789  resources = await async_get_translations(
790  hass,
791  msg["language"],
792  msg["category"],
793  msg.get("integration"),
794  msg.get("config_flow"),
795  )
796  connection.send_message(
797  websocket_api.result_message(msg["id"], {"resources": resources})
798  )
799 
800 
801 @websocket_api.websocket_command({"type": "frontend/get_version"})
802 @websocket_api.async_response
803 async def websocket_get_version(
804  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
805 ) -> None:
806  """Handle get version command."""
807  integration = await async_get_integration(hass, "frontend")
808 
809  frontend = None
810 
811  for req in integration.requirements:
812  if req.startswith("home-assistant-frontend=="):
813  frontend = req.removeprefix("home-assistant-frontend==")
814 
815  if frontend is None:
816  connection.send_error(msg["id"], "unknown_version", "Version not found")
817  else:
818  connection.send_result(msg["id"], {"version": frontend})
819 
820 
821 @callback
822 @websocket_api.websocket_command({"type": "frontend/subscribe_extra_js"})
824  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
825 ) -> None:
826  """Subscribe to URL manager updates."""
827 
828  subscribers = hass.data[DATA_WS_SUBSCRIBERS]
829  subscribers.add((connection, msg["id"]))
830 
831  @callback
832  def cancel_subscription() -> None:
833  subscribers.remove((connection, msg["id"]))
834 
835  connection.subscriptions[msg["id"]] = cancel_subscription
836  connection.send_message(websocket_api.result_message(msg["id"]))
837 
838 
839 class PanelRespons(TypedDict):
840  """Represent the panel response type."""
841 
842  component_name: str
843  icon: str | None
844  title: str | None
845  config: dict[str, Any] | None
846  url_path: str | None
847  require_admin: bool
848  config_panel_domain: str | None
849 
web_urldispatcher.ResourceRoute _route(self)
Definition: __init__.py:599
None __init__(self, str|None repo_path, HomeAssistant hass)
Definition: __init__.py:586
web.Response get(self, web.Request request)
Definition: __init__.py:655
dict[str, list[str]] get_info(self)
Definition: __init__.py:633
tuple[web_urldispatcher.UrlMappingMatchInfo|None, set[str]] resolve(self, web.Request request)
Definition: __init__.py:609
Iterator[web_urldispatcher.ResourceRoute] __iter__(self)
Definition: __init__.py:691
web.Response get(self, web.Request request)
Definition: __init__.py:704
None update_key(self, str key, str val)
Definition: __init__.py:152
None __init__(self, str component_name, str|None sidebar_title, str|None sidebar_icon, str|None frontend_url_path, dict[str, Any]|None config, bool require_admin, str|None config_panel_domain)
Definition: __init__.py:266
None __init__(self, Callable[[str, str], None] on_change, list[str] urls)
Definition: __init__.py:217
bool add(self, _T matcher)
Definition: match.py:185
bool remove(self, _T matcher)
Definition: match.py:214
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_frontend_storage(HomeAssistant hass)
Definition: storage.py:28
None add_manifest_json_key(str key, Any val)
Definition: __init__.py:359
None websocket_get_version(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
Definition: __init__.py:811
None websocket_get_panels(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
Definition: __init__.py:739
None async_remove_panel(HomeAssistant hass, str frontend_url_path, *bool warn_if_unknown=True)
Definition: __init__.py:329
None websocket_get_themes(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
Definition: __init__.py:755
None add_extra_js_url(HomeAssistant hass, str url, bool es5=False)
Definition: __init__.py:341
None async_register_built_in_panel(HomeAssistant hass, str component_name, str|None sidebar_title=None, str|None sidebar_icon=None, str|None frontend_url_path=None, dict[str, Any]|None config=None, bool require_admin=False, *bool update=False, str|None config_panel_domain=None)
Definition: __init__.py:303
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:375
None websocket_subscribe_extra_js(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
Definition: __init__.py:831
str _async_render_index_cached(jinja2.Template template, **Any kwargs)
Definition: __init__.py:579
None websocket_get_icons(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
Definition: __init__.py:723
None _async_setup_themes(HomeAssistant hass, dict[str, Any]|None themes)
Definition: __init__.py:474
None remove_extra_js_url(HomeAssistant hass, str url, bool es5=False)
Definition: __init__.py:350
None websocket_get_translations(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
Definition: __init__.py:793
pathlib.Path _frontend_root(str|None dev_repo_path)
Definition: __init__.py:364
None open(self, **Any kwargs)
Definition: lock.py:86
dict async_hass_config_yaml(HomeAssistant hass)
Definition: config.py:209
dict[str, Any] async_get_icons(HomeAssistant hass, str category, Iterable[str]|None integrations=None)
Definition: icon.py:147
str json_dumps_sorted(Any data)
Definition: json.py:173
dict[str, str] async_get_translations(HomeAssistant hass, str language, str category, Iterable[str]|None integrations=None, bool|None config_flow=None)
Definition: translation.py:342
Integration async_get_integration(HomeAssistant hass, str domain)
Definition: loader.py:1354