frontend.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """"Starting setup task: Frontend"."""
  2. from __future__ import annotations
  3. from typing import TYPE_CHECKING
  4. from aiohttp import web
  5. from homeassistant.components.http import HomeAssistantView
  6. from homeassistant.core import HomeAssistant, callback
  7. from .const import DOMAIN
  8. from .hacs_frontend import locate_dir
  9. from .hacs_frontend.version import VERSION as FE_VERSION
  10. URL_BASE = "/hacsfiles"
  11. if TYPE_CHECKING:
  12. from .base import HacsBase
  13. @callback
  14. def async_register_frontend(hass: HomeAssistant, hacs: HacsBase) -> None:
  15. """Register the frontend."""
  16. # Register themes
  17. hass.http.register_static_path(f"{URL_BASE}/themes", hass.config.path("themes"))
  18. # Register frontend
  19. if hacs.configuration.frontend_repo_url:
  20. hacs.log.warning(
  21. "<HacsFrontend> Frontend development mode enabled. Do not run in production!"
  22. )
  23. hass.http.register_view(HacsFrontendDev())
  24. else:
  25. #
  26. hass.http.register_static_path(f"{URL_BASE}/frontend", locate_dir(), cache_headers=False)
  27. # Custom iconset
  28. hass.http.register_static_path(
  29. f"{URL_BASE}/iconset.js", str(hacs.integration_dir / "iconset.js")
  30. )
  31. if "frontend_extra_module_url" not in hass.data:
  32. hass.data["frontend_extra_module_url"] = set()
  33. hass.data["frontend_extra_module_url"].add(f"{URL_BASE}/iconset.js")
  34. # Register www/community for all other files
  35. use_cache = hacs.core.lovelace_mode == "storage"
  36. hacs.log.info(
  37. "<HacsFrontend> %s mode, cache for /hacsfiles/: %s",
  38. hacs.core.lovelace_mode,
  39. use_cache,
  40. )
  41. hass.http.register_static_path(
  42. URL_BASE,
  43. hass.config.path("www/community"),
  44. cache_headers=use_cache,
  45. )
  46. hacs.frontend_version = FE_VERSION
  47. # Add to sidepanel if needed
  48. if DOMAIN not in hass.data.get("frontend_panels", {}):
  49. hass.components.frontend.async_register_built_in_panel(
  50. component_name="custom",
  51. sidebar_title=hacs.configuration.sidepanel_title,
  52. sidebar_icon=hacs.configuration.sidepanel_icon,
  53. frontend_url_path=DOMAIN,
  54. config={
  55. "_panel_custom": {
  56. "name": "hacs-frontend",
  57. "embed_iframe": True,
  58. "trust_external": False,
  59. "js_url": f"/hacsfiles/frontend/entrypoint.js?hacstag={FE_VERSION}",
  60. }
  61. },
  62. require_admin=True,
  63. )
  64. class HacsFrontendDev(HomeAssistantView):
  65. """Dev View Class for HACS."""
  66. requires_auth = False
  67. name = "hacs_files:frontend"
  68. url = r"/hacsfiles/frontend/{requested_file:.+}"
  69. async def get(self, request, requested_file): # pylint: disable=unused-argument
  70. """Handle HACS Web requests."""
  71. hacs: HacsBase = request.app["hass"].data.get(DOMAIN)
  72. requested = requested_file.split("/")[-1]
  73. request = await hacs.session.get(f"{hacs.configuration.frontend_repo_url}/{requested}")
  74. if request.status == 200:
  75. result = await request.read()
  76. response = web.Response(body=result)
  77. response.headers["Content-Type"] = "application/javascript"
  78. return response