From 11d0b1b9f1ef0ed0bf81c2e5dc9b97902a4b78ae Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 23 Oct 2026 11:47:52 +0000 Subject: [PATCH] Card and tiles also as a dashboard resource (0.7.1) The companion app did not always load the card from the extra modules of the start page, then the tiles showed a configuration error. The integration now also keeps card.js (which loads the tiles) as a dashboard resource (storage mode), updates its version and removes it with the integration. Same fix as in AI Heating Assistant 1.4.2. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GC3Jbmd6Vr4qXPkRDxcSs3 --- README.en.md | 4 +-- README.md | 4 +-- .../shutter_assistant/__init__.py | 3 +- .../shutter_assistant/manifest.json | 4 +-- custom_components/shutter_assistant/panel.py | 36 ++++++++++++++++++- tests/ha/test_card_resource.py | 36 +++++++++++++++++++ 6 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 tests/ha/test_card_resource.py diff --git a/README.en.md b/README.en.md index 8ca56d3..05c8ef2 100644 --- a/README.en.md +++ b/README.en.md @@ -6,7 +6,7 @@ Custom integration for Home Assistant that controls roller shutters and blinds a It typically replaces a bundle of YAML automations with many helpers (input_boolean, input_number, timer) for sun protection, auto up, evening run, closing after the window was closed, ventilation, override and vacation. -> Status: version 0.6.0. +> Status: version 0.7.1. ## How it works @@ -208,7 +208,7 @@ Events for own automations: ## Dashboard card -For every user (also without admin rights) there is the card **Shutter Assistant** in the card dialog: all windows with status, position, window contact, next action and a running override (with "Resume automatic"), buttons to open, stop and close (they act like a button on the wall, so they start an override), active weather protection and a small sun compass. Nothing can be set up from the card. +For every user (also without admin rights) there is the card **Shutter Assistant** in the card dialog: all windows with status, position, window contact, next action and a running override (with "Resume automatic"), buttons to open, stop and close (they act like a button on the wall, so they start an override), active weather protection and a small sun compass. Nothing can be set up from the card. The card and the tiles are loaded automatically (with resources managed in the UI they are also added as a dashboard resource, so the companion app loads them reliably). ```yaml type: custom:shutter-assistant-card diff --git a/README.md b/README.md index 82664c7..001ff92 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Custom Integration für Home Assistant, die Rollos und Raffstores automatisch st Die Integration ersetzt typischerweise ein Bündel von YAML-Automationen mit vielen Helfern (input_boolean, input_number, timer) für Sonnenschutz, Auto-Hoch, Abendlauf, Nachziehen, Lüften, Übersteuerung und Urlaub. -> Status: Version 0.6.0. +> Status: Version 0.7.1. ## Funktionsweise @@ -208,7 +208,7 @@ Ereignisse für eigene Automationen: ## Dashboard-Karte -Für alle Nutzer (auch ohne Admin-Rechte) gibt es die Karte **Shutter Assistant** im Karten-Dialog: alle Fenster mit Status, Position, Fensterkontakt, nächster Aktion und laufender Übersteuerung (mit „Automatik fortsetzen“), Knöpfe für Auf, Stopp und Ab (wirken wie ein Taster, starten also eine Übersteuerung), aktiver Wetterschutz und ein kleiner Sonnenkompass. Einstellen lässt sich über die Karte nichts. +Für alle Nutzer (auch ohne Admin-Rechte) gibt es die Karte **Shutter Assistant** im Karten-Dialog: alle Fenster mit Status, Position, Fensterkontakt, nächster Aktion und laufender Übersteuerung (mit „Automatik fortsetzen“), Knöpfe für Auf, Stopp und Ab (wirken wie ein Taster, starten also eine Übersteuerung), aktiver Wetterschutz und ein kleiner Sonnenkompass. Einstellen lässt sich über die Karte nichts. Karte und Kacheln werden automatisch geladen (bei im UI verwalteten Ressourcen auch als Dashboard-Ressource eingetragen, damit die Companion-App sie zuverlässig lädt). ```yaml type: custom:shutter-assistant-card diff --git a/custom_components/shutter_assistant/__init__.py b/custom_components/shutter_assistant/__init__.py index 5808e85..319132b 100644 --- a/custom_components/shutter_assistant/__init__.py +++ b/custom_components/shutter_assistant/__init__.py @@ -12,7 +12,7 @@ from .const import CONF_SHOW_PANEL, DEFAULTS, DOMAIN, PLATFORMS, STORAGE_VERSION from .engine import ShutterConfigEntry, ShutterEngine from .entity import global_device_info -from .panel import async_register_panel, async_unregister_panel +from .panel import async_register_panel, async_set_card_resource, async_unregister_panel from .services import async_register_services from .websocket import async_register_websocket @@ -64,6 +64,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ShutterConfigEntry) -> async def async_remove_entry(hass: HomeAssistant, entry: ShutterConfigEntry) -> None: """Delete the stored state, the panel and the repair issues when the integration is removed.""" async_unregister_panel(hass) + await async_set_card_resource(hass, None) for domain, issue_id in list(ir.async_get(hass).issues): if domain == DOMAIN: ir.async_delete_issue(hass, DOMAIN, issue_id) diff --git a/custom_components/shutter_assistant/manifest.json b/custom_components/shutter_assistant/manifest.json index afdda09..298371a 100644 --- a/custom_components/shutter_assistant/manifest.json +++ b/custom_components/shutter_assistant/manifest.json @@ -1,7 +1,7 @@ { "domain": "shutter_assistant", "name": "Shutter Assistant", - "after_dependencies": ["frontend", "http", "panel_custom", "websocket_api"], + "after_dependencies": ["frontend", "http", "lovelace", "panel_custom", "websocket_api"], "codeowners": ["@Jumpmaster2"], "config_flow": true, "documentation": "https://github.com/Jumpmaster2/Shutter-Assistant", @@ -10,5 +10,5 @@ "issue_tracker": "https://github.com/Jumpmaster2/Shutter-Assistant/issues", "requirements": [], "single_config_entry": true, - "version": "0.7.0" + "version": "0.7.1" } diff --git a/custom_components/shutter_assistant/panel.py b/custom_components/shutter_assistant/panel.py index 1850e3e..0e0e174 100644 --- a/custom_components/shutter_assistant/panel.py +++ b/custom_components/shutter_assistant/panel.py @@ -16,6 +16,7 @@ DATA_SIDEBAR = f"{DOMAIN}_panel_in_sidebar" # url of the dashboard card module (loaded on every dashboard, for every user) DATA_CARD = f"{DOMAIN}_card_url" +CARD_PATH = f"{STATIC_URL}/card.js" async def async_register_panel(hass: HomeAssistant, entry_id: str, sidebar: bool = True) -> None: @@ -36,12 +37,13 @@ async def async_register_panel(hass: HomeAssistant, entry_id: str, sidebar: bool ) hass.data[DATA_STATIC] = True version = await hass.async_add_executor_job(_version) - card = f"{STATIC_URL}/card.js?v={version}" + card = f"{CARD_PATH}?v={version}" if hass.data.get(DATA_CARD) != card: if old := hass.data.get(DATA_CARD): frontend.remove_extra_js_url(hass, old) frontend.add_extra_js_url(hass, card) hass.data[DATA_CARD] = card + await async_set_card_resource(hass, card) if frontend.async_panel_exists(hass, PANEL_URL_PATH): if hass.data.get(DATA_SIDEBAR) == sidebar: return @@ -79,3 +81,35 @@ def _version() -> str: LOGGER.debug("Frontend files not found") stamp = 0 return str(stamp) + + +async def async_set_card_resource(hass: HomeAssistant, url: str | None) -> None: + """Keep the card and the tiles as a dashboard resource (None removes it). + + The companion app does not always load the extra modules of the start page, + then the tiles show a configuration error. Its resources it loads with every + dashboard. Only for resources managed in the UI (storage mode). + """ + try: + from homeassistant.components.lovelace.const import LOVELACE_DATA + except ImportError: + return + data = hass.data.get(LOVELACE_DATA) + if data is None or getattr(data, "resource_mode", None) != "storage": + return + resources = data.resources + try: + if not resources.loaded: + await resources.async_load() + resources.loaded = True + ours = [i for i in resources.async_items() if i.get("url", "").split("?")[0] == CARD_PATH] + for item in ours[1:] if url else ours: + await resources.async_delete_item(item["id"]) + if not url: + return + if not ours: + await resources.async_create_item({"res_type": "module", "url": url}) + elif ours[0].get("url") != url: + await resources.async_update_item(ours[0]["id"], {"res_type": "module", "url": url}) + except Exception: # the card is optional, the control must not fail + LOGGER.warning("Could not register the dashboard card as a dashboard resource") diff --git a/tests/ha/test_card_resource.py b/tests/ha/test_card_resource.py new file mode 100644 index 0000000..ab56a64 --- /dev/null +++ b/tests/ha/test_card_resource.py @@ -0,0 +1,36 @@ +"""The dashboard card (with the tiles) as a dashboard resource.""" + +from __future__ import annotations + +from homeassistant.components.lovelace.const import LOVELACE_DATA +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component + +from custom_components.shutter_assistant.panel import CARD_PATH, async_set_card_resource + + +async def _urls(hass: HomeAssistant) -> list[str]: + return [item["url"] for item in hass.data[LOVELACE_DATA].resources.async_items()] + + +async def test_card_resource_is_added_updated_and_removed(hass: HomeAssistant) -> None: + assert await async_setup_component(hass, "lovelace", {}) + resources = hass.data[LOVELACE_DATA].resources + await resources.async_load() + resources.loaded = True + await resources.async_create_item({"res_type": "module", "url": "/local/other.js"}) + + await async_set_card_resource(hass, f"{CARD_PATH}?v=1") + assert await _urls(hass) == ["/local/other.js", f"{CARD_PATH}?v=1"] + + # a new version replaces the entry instead of adding a second one + await async_set_card_resource(hass, f"{CARD_PATH}?v=2") + assert await _urls(hass) == ["/local/other.js", f"{CARD_PATH}?v=2"] + assert resources.async_items()[1]["type"] == "module" + + await async_set_card_resource(hass, None) + assert await _urls(hass) == ["/local/other.js"] + + +async def test_card_resource_without_lovelace(hass: HomeAssistant) -> None: + await async_set_card_resource(hass, f"{CARD_PATH}?v=1") # nothing to do, no error