116 lines
4.1 KiB
Python
116 lines
4.1 KiB
Python
"""NextFloor: draw your home in Home Assistant and control it in 3D."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
from pathlib import Path
|
|
|
|
from homeassistant.components import frontend, panel_custom
|
|
from homeassistant.components.http import StaticPathConfig
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers import config_validation as cv
|
|
from homeassistant.helpers.typing import ConfigType
|
|
from homeassistant.loader import async_get_integration
|
|
|
|
from .announce import async_register_announce
|
|
from .card_resource import async_remove_card_resource, async_schedule_card_resource
|
|
from .const import (
|
|
DOMAIN,
|
|
MAIN_BUNDLE,
|
|
PANEL_COMPONENT,
|
|
PANEL_ICON,
|
|
PANEL_TITLE,
|
|
PANEL_URL_PATH,
|
|
URL_BASE,
|
|
)
|
|
from .meshes import async_register_mesh_view
|
|
from .storage import NextFloorData
|
|
from .store_notice import async_notify_official_store
|
|
from .websocket import async_register_commands
|
|
|
|
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
|
|
|
_STATIC_REGISTERED = f"{DOMAIN}_static_registered"
|
|
# sensors from the plan: open windows and doors, lights on (per floor and for the house)
|
|
PLATFORMS = [Platform.SENSOR]
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|
"""Register the websocket commands and the actions once."""
|
|
async_register_commands(hass)
|
|
async_register_mesh_view(hass)
|
|
async_register_announce(hass)
|
|
return True
|
|
|
|
|
|
_MAIN_URL = f"{DOMAIN}_main_url"
|
|
|
|
|
|
def _bundle_hash() -> str:
|
|
"""A short hash of the main bundle: a rebuilt bundle gets a new URL even when the version stays."""
|
|
path = Path(__file__).parent / "frontend" / MAIN_BUNDLE
|
|
try:
|
|
return hashlib.sha256(path.read_bytes()).hexdigest()[:10]
|
|
except OSError:
|
|
return "0"
|
|
|
|
|
|
async def _async_main_url(hass: HomeAssistant) -> str:
|
|
# computed once per run, so adding and removing the script use the same URL
|
|
if url := hass.data.get(_MAIN_URL):
|
|
return url
|
|
integration = await async_get_integration(hass, DOMAIN)
|
|
digest = await hass.async_add_executor_job(_bundle_hash)
|
|
url = f"{URL_BASE}/{MAIN_BUNDLE}?v={integration.version}-{digest}"
|
|
hass.data[_MAIN_URL] = url
|
|
return url
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Load the stores and register the frontend (panel and card)."""
|
|
data = NextFloorData(hass)
|
|
await data.async_load()
|
|
hass.data[DOMAIN] = data
|
|
|
|
# static paths cannot be unregistered, so they are registered once per run
|
|
if not hass.data.get(_STATIC_REGISTERED):
|
|
await hass.http.async_register_static_paths(
|
|
[StaticPathConfig(URL_BASE, str(Path(__file__).parent / "frontend"), cache_headers=False)]
|
|
)
|
|
hass.data[_STATIC_REGISTERED] = True
|
|
|
|
url = await _async_main_url(hass)
|
|
frontend.add_extra_js_url(hass, url)
|
|
# and as a dashboard resource, so a dashboard never draws the card before its script is there (#252)
|
|
async_schedule_card_resource(hass, url)
|
|
await panel_custom.async_register_panel(
|
|
hass,
|
|
frontend_url_path=PANEL_URL_PATH,
|
|
webcomponent_name=PANEL_COMPONENT,
|
|
sidebar_title=PANEL_TITLE,
|
|
sidebar_icon=PANEL_ICON,
|
|
module_url=url,
|
|
require_admin=False,
|
|
config={},
|
|
)
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
await async_notify_official_store(hass, entry)
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Remove panel, card and sensors."""
|
|
await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
frontend.async_remove_panel(hass, PANEL_URL_PATH)
|
|
frontend.remove_extra_js_url(hass, await _async_main_url(hass))
|
|
hass.data.pop(DOMAIN, None)
|
|
return True
|
|
|
|
|
|
async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
|
"""The plan, its pictures and packs stay in .storage when the integration is removed: they are the
|
|
user's work, and removing and re-adding the integration must never cost it."""
|
|
await async_remove_card_resource(hass)
|