Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion custom_components/shutter_assistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions custom_components/shutter_assistant/manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
}
36 changes: 35 additions & 1 deletion custom_components/shutter_assistant/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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")
36 changes: 36 additions & 0 deletions tests/ha/test_card_resource.py
Original file line number Diff line number Diff line change
@@ -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
Loading