wallpanell/custom_components/wall_panel/__init__.py
2026-03-25 23:55:49 +03:00

76 lines
2.7 KiB
Python
Executable File

"""Wall Panel integration."""
from __future__ import annotations
import logging
from homeassistant.core import HomeAssistant
from .const import DEFAULT_DASHBOARD_URL_PATH
from .const import DEFAULT_FRONTEND_URL_PATH
from .const import DOMAIN
from .frontend import async_setup_frontend
from .helpers import current_entry_config
from .views import handle_config_request, handle_panel_request, handle_proxy_request
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass: HomeAssistant, config: dict) -> bool:
"""Set up the integration-level HTTP views."""
state = hass.data.setdefault(DOMAIN, {})
if not state.get("_direct_routes_registered"):
app = hass.http.app
app.router.add_route("*", "/api/wall_panel/config/{entry_id}", handle_config_request)
app.router.add_route("*", "/api/wall_panel/panel", handle_panel_request)
app.router.add_route("*", "/api/wall_panel/proxy/{entry_id}/{path:.*}", handle_proxy_request)
state["_direct_routes_registered"] = True
_LOGGER.warning("Wall Panel direct routes registered")
_LOGGER.warning("Wall Panel integration views registered")
return True
async def async_setup_entry(hass: HomeAssistant, entry) -> bool:
"""Set up Wall Panel from a config entry."""
state = hass.data.setdefault(DOMAIN, {})
state[entry.entry_id] = {
"entry": entry,
"config": current_entry_config(entry),
}
panel_url_path = await async_setup_frontend(hass, entry)
state[entry.entry_id]["panel_url_path"] = panel_url_path
entry.async_on_unload(entry.add_update_listener(_async_options_updated))
return True
async def async_unload_entry(hass: HomeAssistant, entry) -> bool:
"""Unload Wall Panel."""
from homeassistant.components.frontend import async_remove_panel
state = hass.data.get(DOMAIN, {})
panel_url_path = str(state.get(entry.entry_id, {}).get("panel_url_path") or DEFAULT_FRONTEND_URL_PATH).strip()
dashboard_url_path = str(state.get(entry.entry_id, {}).get("dashboard_url_path") or DEFAULT_DASHBOARD_URL_PATH).strip()
async_remove_panel(hass, panel_url_path)
async_remove_panel(hass, dashboard_url_path)
lovelace = hass.data.get("lovelace")
dashboards = getattr(lovelace, "dashboards", None)
if dashboards is None and isinstance(lovelace, dict):
dashboards = lovelace.get("dashboards")
if dashboards is not None:
dashboards.pop(dashboard_url_path, None)
state.pop(entry.entry_id, None)
return True
async def _async_options_updated(hass: HomeAssistant, entry) -> None:
"""Reload the integration when options change."""
await hass.config_entries.async_reload(entry.entry_id)