mirror of
https://github.com/Matysh/houseplan-card
synced 2026-07-31 08:28:31 +00:00
HP-1460-01: v1.46.0 stopped overwriting attachments, but picking a free name and taking it were two steps. Two uploads racing between them agreed on the same name, both answered 200, and one set of bytes replaced the other; files/migrate had the same check-then-copy gap. reserve_filename now claims the name with O_CREAT|O_EXCL as it picks it, and both paths use it. It also splits the extension off the RAW name and budgets the stem against MAX_FILENAME including the collision tag — a maximal name lost its '.pdf' and then grew past the limit, so the view sanitised the request back to a different name and the attachment 404'd for good. HP-1460-02: cleanup lived in an 'except Exception', which CancelledError walks past, only one tmp_path was tracked, promotion had no finally, and the collector only walks marker folders — an aborted transfer stranded a .upload-* that nothing would ever remove. An outer finally owns every temporary, a second 'file' part is refused, promotion failure cleans up, and sweep_upload_temps runs at setup, daily, and inside the commit-scoped collector. Chunks are batched to 1 MB per disk task instead of one per 64 KB. HP-1460-03: the layout event reached the static card and not the full one, so two full cards diverged until a reload. The full card subscribes now and re-reads ONLY the layout, keyed on its revision. Two hazards handled: it records revisions it produced itself, and the reaction is deferred ~200 ms because the event can beat the reply to our own write over the same socket; positions dragged but not yet sent are flushed and merged on top, so a fix for a stale UI cannot become a lost drag. Tests: smoke_layout_sync (fails on a v1.46.0 build), four pure tests for atomic reservation incl. 20-thread concurrency and the length boundary, a backend test walking every failing exit path of an upload, and — as the report asked — an HA-harness test that a repair issue disappears with its space. Docs: CHANGELOG.md + CHANGELOG.ru.md + ARCHITECTURE.md + TESTING.md + STATUS.md.
193 lines
8.3 KiB
Python
Executable File
193 lines
8.3 KiB
Python
Executable File
"""House Plan: server-side house plan configuration + Lovelace card serving."""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from datetime import timedelta
|
|
from pathlib import Path
|
|
|
|
from homeassistant.components.frontend import add_extra_js_url
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
from homeassistant.helpers.event import async_track_time_interval
|
|
|
|
from . import websocket_api as hp_ws
|
|
from .const import (
|
|
DOMAIN,
|
|
FILES_DIR,
|
|
FILES_URL,
|
|
FRONTEND_URL,
|
|
PLANS_DIR,
|
|
PLANS_URL,
|
|
VERSION,
|
|
)
|
|
from .plans import sweep_upload_temps
|
|
from .repairs import async_check_plan_files
|
|
from .store import HouseplanConfigEntry, create_data
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config) -> bool:
|
|
"""Register global handlers (survive config-entry reloads): WS commands, HTTP view."""
|
|
hass.data.setdefault(DOMAIN, {})
|
|
hp_ws.async_register(hass)
|
|
from .http_api import HouseplanContentView, HouseplanUploadView
|
|
|
|
hass.http.register_view(HouseplanUploadView())
|
|
hass.http.register_view(HouseplanContentView())
|
|
return True
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: HouseplanConfigEntry) -> bool:
|
|
"""Config entry: stores in runtime_data, static paths, card auto-registration."""
|
|
data = create_data(hass)
|
|
# test-before-setup: storage must be readable, otherwise retry later
|
|
try:
|
|
await data.store.async_load()
|
|
await data.config_store.async_load()
|
|
except Exception as err: # noqa: BLE001 — corrupt/unreadable .storage
|
|
raise ConfigEntryNotReady(f"House Plan storage is not readable: {err}") from err
|
|
entry.runtime_data = data
|
|
|
|
card_path = Path(__file__).parent / "frontend" / "houseplan-card.js"
|
|
plans_path = Path(hass.config.path(PLANS_DIR))
|
|
files_path = Path(hass.config.path(FILES_DIR))
|
|
await hass.async_add_executor_job(
|
|
lambda: (plans_path.mkdir(parents=True, exist_ok=True), files_path.mkdir(parents=True, exist_ok=True))
|
|
)
|
|
|
|
# Static paths cannot be unregistered — register once per HA run.
|
|
if not hass.data[DOMAIN].get("static_registered"):
|
|
hass.data[DOMAIN]["static_registered"] = True
|
|
static_paths = []
|
|
try:
|
|
from homeassistant.components.http import StaticPathConfig
|
|
|
|
if card_path.exists():
|
|
static_paths.append(StaticPathConfig(FRONTEND_URL, str(card_path), cache_headers=False))
|
|
# NOTE (audit B1): plans and marker files are NO LONGER static.
|
|
# They are served by HouseplanContentView, which requires auth.
|
|
# Only the card bundle stays public — Lovelace resources must be.
|
|
if static_paths:
|
|
await hass.http.async_register_static_paths(static_paths)
|
|
except ImportError: # very old HA versions
|
|
if card_path.exists():
|
|
hass.http.register_static_path(FRONTEND_URL, str(card_path), cache_headers=False)
|
|
|
|
if not card_path.exists():
|
|
_LOGGER.warning("houseplan-card.js not found next to the integration: %s", card_path)
|
|
return True
|
|
|
|
# Register the card. Preferably as a Lovelace resource (the frontend AWAITS
|
|
# resources before rendering dashboards, so the card is available even on a cold
|
|
# start of the mobile app). If the resource registry is unavailable (YAML-mode
|
|
# Lovelace, old versions) — fall back to extra_module_url.
|
|
module_url = f"{FRONTEND_URL}?v={VERSION}"
|
|
registered = await _register_lovelace_resource(hass, module_url)
|
|
if not registered:
|
|
add_extra_js_url(hass, module_url)
|
|
# Tell the user exactly where the card lives — the #1 support issue is people adding a
|
|
# Lovelace resource pointing at the on-disk path (/custom_components/...), which HA does
|
|
# not serve (wrong MIME → "Custom element doesn't exist"). The correct served URL is below.
|
|
if registered:
|
|
_LOGGER.info("House Plan card auto-registered as a Lovelace resource: %s", module_url)
|
|
else:
|
|
_LOGGER.info(
|
|
"House Plan card is served at %s . Lovelace resources look YAML-managed — add it "
|
|
"manually under `resources:` as { url: %s, type: module }. Do NOT use the on-disk "
|
|
"path /custom_components/houseplan/frontend/houseplan-card.js (HA does not serve it).",
|
|
module_url, module_url,
|
|
)
|
|
|
|
await async_check_plan_files(hass, entry)
|
|
|
|
# Abandoned upload temporaries (HP-1460-02). A request cleans up after
|
|
# itself; a hard kill mid-upload does not, and the commit-scoped collector
|
|
# only walks marker folders — so without this a `.upload-*` from a crashed
|
|
# transfer would sit there for good, and on an instance nobody edits, so
|
|
# would a cancelled attachment. Once at startup, then daily.
|
|
async def _sweep(_now=None) -> None:
|
|
files_dir = Path(hass.config.path(FILES_DIR))
|
|
try:
|
|
n = await hass.async_add_executor_job(sweep_upload_temps, files_dir)
|
|
if n:
|
|
_LOGGER.info("House Plan: removed %s abandoned upload temporaries", n)
|
|
except Exception: # noqa: BLE001 — housekeeping must never fail a setup
|
|
_LOGGER.exception("House Plan: sweeping upload temporaries failed")
|
|
|
|
await _sweep()
|
|
entry.async_on_unload(
|
|
async_track_time_interval(hass, _sweep, timedelta(hours=24))
|
|
)
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: HouseplanConfigEntry) -> bool:
|
|
"""Unload the entry.
|
|
|
|
WS commands and the HTTP view are global (async_setup) and stay registered —
|
|
their handlers resolve runtime data per call and answer `not_ready` while no
|
|
entry is loaded. Static paths cannot be unregistered by design.
|
|
"""
|
|
return True
|
|
|
|
|
|
async def async_remove_entry(hass: HomeAssistant, entry) -> None:
|
|
"""Clean up on integration removal: drop our Lovelace resource entry."""
|
|
try:
|
|
resources = _lovelace_resources(hass)
|
|
if resources is None or not hasattr(resources, "async_delete_item"):
|
|
return
|
|
for item in list(resources.async_items()):
|
|
if str(item.get("url", "")).split("?", 1)[0] == FRONTEND_URL:
|
|
await resources.async_delete_item(item["id"])
|
|
_LOGGER.debug("House Plan Lovelace resource removed: %s", item.get("url"))
|
|
except Exception as err: # noqa: BLE001 — best-effort cleanup
|
|
_LOGGER.debug("Could not remove the Lovelace resource on uninstall: %s", err)
|
|
|
|
|
|
def _lovelace_resources(hass: HomeAssistant):
|
|
lovelace = hass.data.get("lovelace")
|
|
resources = getattr(lovelace, "resources", None)
|
|
if resources is None and isinstance(lovelace, dict):
|
|
resources = lovelace.get("resources")
|
|
return resources
|
|
|
|
|
|
async def _register_lovelace_resource(hass: HomeAssistant, module_url: str) -> bool:
|
|
"""Register (or update) the card in the Lovelace resource registry.
|
|
|
|
Returns True on success. Idempotent: if a resource with our path exists —
|
|
update the URL on version change; otherwise create it. Any exception → False
|
|
(fall back to extra_module_url).
|
|
"""
|
|
try:
|
|
resources = _lovelace_resources(hass)
|
|
if resources is None:
|
|
return False
|
|
# the resource registry must be loaded
|
|
if hasattr(resources, "loaded") and not resources.loaded:
|
|
await resources.async_load()
|
|
resources.loaded = True
|
|
elif hasattr(resources, "async_get_info"):
|
|
await resources.async_get_info()
|
|
# only storage mode allows creating items
|
|
if not hasattr(resources, "async_create_item"):
|
|
return False
|
|
base = FRONTEND_URL
|
|
existing = [
|
|
item for item in resources.async_items()
|
|
if str(item.get("url", "")).split("?", 1)[0] == base
|
|
]
|
|
if existing:
|
|
item = existing[0]
|
|
if item.get("url") != module_url and hasattr(resources, "async_update_item"):
|
|
await resources.async_update_item(item["id"], {"url": module_url})
|
|
return True
|
|
await resources.async_create_item({"res_type": "module", "url": module_url})
|
|
_LOGGER.debug("House Plan card registered as a Lovelace resource: %s", module_url)
|
|
return True
|
|
except Exception as err: # noqa: BLE001 — any failure → fallback
|
|
_LOGGER.debug("Could not register the Lovelace resource (%s), falling back to extra_module_url", err)
|
|
return False
|