From 825e707a8009cb58d64c65583afe0fdcb8c559db Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Thu, 3 Apr 2025 12:10:11 +0200 Subject: [PATCH] Refresh dashboard strategy when registries changed (#24902) * Refresh dashboard strategy when registries changed * Display toast before refreshing dashboard * Apply suggestions --- src/panels/lovelace/ha-panel-lovelace.ts | 53 ++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/panels/lovelace/ha-panel-lovelace.ts b/src/panels/lovelace/ha-panel-lovelace.ts index bc7471d9c3..8c17d07796 100644 --- a/src/panels/lovelace/ha-panel-lovelace.ts +++ b/src/panels/lovelace/ha-panel-lovelace.ts @@ -178,6 +178,59 @@ export class LovelacePanel extends LitElement { } } + protected updated(changedProperties: PropertyValues): void { + super.updated(changedProperties); + if (!changedProperties.has("hass")) { + return; + } + const oldHass = changedProperties.get("hass") as HomeAssistant | undefined; + if ( + oldHass && + this.hass && + (oldHass.entities !== this.hass.entities || + oldHass.devices !== this.hass.devices || + oldHass.areas !== this.hass.areas || + oldHass.floors !== this.hass.floors) + ) { + this._registriesChanged(); + } + } + + private _registriesChanged = () => { + if (this.lovelace && isStrategyDashboard(this.lovelace.rawConfig)) { + showToast(this, { + message: this.hass!.localize("ui.panel.lovelace.changed_toast.message"), + action: { + action: () => this._refreshConfig(), + text: this.hass!.localize("ui.common.refresh"), + }, + duration: -1, + id: "entity-registry-changed", + dismissable: false, + }); + } + }; + + private async _refreshConfig() { + if (!this.hass || !this.lovelace) { + return; + } + + const rawConf = this.lovelace.rawConfig; + + if (!isStrategyDashboard(rawConf)) { + return; + } + + try { + const conf = await generateLovelaceDashboardStrategy(rawConf, this.hass!); + this._setLovelaceConfig(conf, rawConf, "generated"); + } catch (err: any) { + // eslint-disable-next-line no-console + console.error(err); + } + } + private _handleConnectionStatus = (ev) => { // reload lovelace on reconnect so we are sure we have the latest config if (ev.detail === "connected") {