wallpanell/custom_components/wall_panel/frontend.py
2026-03-25 13:12:07 +03:00

69 lines
2.3 KiB
Python
Executable File

"""Frontend registration for Wall Panel."""
from __future__ import annotations
from pathlib import Path
from homeassistant.components.frontend import async_register_built_in_panel
from homeassistant.components.http import StaticPathConfig
from homeassistant.core import HomeAssistant
from .const import (
CONF_FRONTEND_URL_PATH,
CONF_PANEL_URL,
CONF_REQUIRE_ADMIN,
CONF_SIDEBAR_ICON,
CONF_SIDEBAR_TITLE,
DEFAULT_FRONTEND_URL_PATH,
DEFAULT_SIDEBAR_ICON,
DEFAULT_SIDEBAR_TITLE,
DOMAIN,
)
async def async_setup_frontend(hass: HomeAssistant, entry) -> str:
"""Register the custom panel and static frontend assets."""
frontend_dir = Path(__file__).parent / "frontend"
state = hass.data.setdefault(DOMAIN, {})
if not state.get("_static_paths_registered"):
await hass.http.async_register_static_paths([
StaticPathConfig(
f"/api/{DOMAIN}/frontend",
str(frontend_dir),
cache_headers=False,
),
])
state["_static_paths_registered"] = True
panel_url_path = str(entry.options.get(CONF_FRONTEND_URL_PATH, DEFAULT_FRONTEND_URL_PATH) or DEFAULT_FRONTEND_URL_PATH).strip()
sidebar_title = str(entry.options.get(CONF_SIDEBAR_TITLE, DEFAULT_SIDEBAR_TITLE) or DEFAULT_SIDEBAR_TITLE).strip()
sidebar_icon = str(entry.options.get(CONF_SIDEBAR_ICON, DEFAULT_SIDEBAR_ICON) or DEFAULT_SIDEBAR_ICON).strip()
require_admin = bool(entry.options.get(CONF_REQUIRE_ADMIN, False))
panel_url = str(entry.options.get(CONF_PANEL_URL, "") or "").strip()
async_register_built_in_panel(
hass,
component_name="custom",
sidebar_title=sidebar_title,
sidebar_icon=sidebar_icon,
frontend_url_path=panel_url_path,
config={
"_panel_custom": {
"name": "wall-panel-panel",
"module_url": f"/api/{DOMAIN}/frontend/panel.js",
"embed_iframe": False,
"trust_external": False,
"config": {
"panel_url": panel_url,
"panel_url_path": panel_url_path,
"entry_id": entry.entry_id,
},
}
},
require_admin=require_admin,
update=True,
)
return panel_url_path