Esempio n. 1
0
async def async_setup_frontend():
    """Configure the HACS frontend elements."""
    hacs = get_hacs()
    hacs.log.info("Setup task %s", HacsSetupTask.FRONTEND)
    hass = hacs.hass

    # Register themes
    hass.http.register_static_path(f"{URL_BASE}/themes",
                                   hass.config.path("themes"))

    # Register frontend
    if hacs.configuration.frontend_repo_url:
        getLogger().warning(
            "Frontend development mode enabled. Do not run in production.")
        hass.http.register_view(HacsFrontendDev())
    else:
        #
        hass.http.register_static_path(f"{URL_BASE}/frontend",
                                       locate_dir(),
                                       cache_headers=False)

    # Custom iconset
    hass.http.register_static_path(f"{URL_BASE}/iconset.js",
                                   str(hacs.integration_dir / "iconset.js"))
    if "frontend_extra_module_url" not in hass.data:
        hass.data["frontend_extra_module_url"] = set()
    hass.data["frontend_extra_module_url"].add("/hacsfiles/iconset.js")

    # Register www/community for all other files
    hass.http.register_static_path(URL_BASE,
                                   hass.config.path("www/community"),
                                   cache_headers=False)

    hacs.frontend.version_running = FE_VERSION
    hacs.frontend.version_expected = await hass.async_add_executor_job(
        get_frontend_version)

    # Add to sidepanel
    if "hacs" not in hass.data.get("frontend_panels", {}):
        hass.components.frontend.async_register_built_in_panel(
            component_name="custom",
            sidebar_title=hacs.configuration.sidepanel_title,
            sidebar_icon=hacs.configuration.sidepanel_icon,
            frontend_url_path="hacs",
            config={
                "_panel_custom": {
                    "name": "hacs-frontend",
                    "embed_iframe": True,
                    "trust_external": False,
                    "js_url": "/hacsfiles/frontend/entrypoint.js",
                }
            },
            require_admin=True,
        )
Esempio n. 2
0
    async def async_execute(self) -> None:

        # Register themes
        self.hass.http.register_static_path(f"{URL_BASE}/themes",
                                            self.hass.config.path("themes"))

        # Register frontend
        if self.hacs.configuration.frontend_repo_url:
            self.log.warning(
                "Frontend development mode enabled. Do not run in production!")
            self.hass.http.register_view(HacsFrontendDev())
        else:
            #
            self.hass.http.register_static_path(f"{URL_BASE}/frontend",
                                                locate_dir(),
                                                cache_headers=False)

        # Custom iconset
        self.hass.http.register_static_path(
            f"{URL_BASE}/iconset.js",
            str(self.hacs.integration_dir / "iconset.js"))
        if "frontend_extra_module_url" not in self.hass.data:
            self.hass.data["frontend_extra_module_url"] = set()
        self.hass.data["frontend_extra_module_url"].add(
            f"{URL_BASE}/iconset.js")

        # Register www/community for all other files
        use_cache = self.hacs.core.lovelace_mode == "storage"
        self.log.info(
            "%s mode, cache for /hacsfiles/: %s",
            self.hacs.core.lovelace_mode,
            use_cache,
        )
        self.hass.http.register_static_path(
            URL_BASE,
            self.hass.config.path("www/community"),
            cache_headers=use_cache,
        )

        self.hacs.frontend.version_running = FE_VERSION
        for requirement in self.hacs.integration.requirements:
            if "hacs_frontend" in requirement:
                self.hacs.frontend.version_expected = requirement.split(
                    "==")[-1]

        # Add to sidepanel if needed
        if DOMAIN not in self.hass.data.get("frontend_panels", {}):
            self.hass.components.frontend.async_register_built_in_panel(
                component_name="custom",
                sidebar_title=self.hacs.configuration.sidepanel_title,
                sidebar_icon=self.hacs.configuration.sidepanel_icon,
                frontend_url_path=DOMAIN,
                config={
                    "_panel_custom": {
                        "name":
                        "hacs-frontend",
                        "embed_iframe":
                        True,
                        "trust_external":
                        False,
                        "js_url":
                        f"/hacsfiles/frontend/entrypoint.js?hacstag={FE_VERSION}",
                    }
                },
                require_admin=True,
            )