From 1a67bd0414b9f25617d939f69950e2f789f3acf5 Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Tue, 10 Sep 2024 00:35:40 -0700 Subject: [PATCH] Fix script more-info when entity_id != unique_id (#21880) * Fix script more-info when entity_id != unique_id * Update src/dialogs/more-info/controls/more-info-script.ts Co-authored-by: Bram Kragten --------- Co-authored-by: Bram Kragten --- .../more-info/controls/more-info-script.ts | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/dialogs/more-info/controls/more-info-script.ts b/src/dialogs/more-info/controls/more-info-script.ts index 83daa7dccfdd..f872733045c5 100644 --- a/src/dialogs/more-info/controls/more-info-script.ts +++ b/src/dialogs/more-info/controls/more-info-script.ts @@ -21,6 +21,7 @@ import { isUnavailableState } from "../../../data/entity"; import { computeObjectId } from "../../../common/entity/compute_object_id"; import { listenMediaQuery } from "../../../common/dom/media_query"; import "../components/ha-more-info-state-header"; +import { ExtEntityRegistryEntry } from "../../../data/entity_registry"; @customElement("more-info-script") class MoreInfoScript extends LitElement { @@ -28,6 +29,8 @@ class MoreInfoScript extends LitElement { @property({ attribute: false }) public stateObj?: ScriptEntity; + @property({ attribute: false }) public entry?: ExtEntityRegistryEntry; + @state() private _scriptData: Record = {}; @state() private narrow = false; @@ -59,8 +62,9 @@ class MoreInfoScript extends LitElement { const stateObj = this.stateObj; const fields = - this.hass.services.script[computeObjectId(this.stateObj.entity_id)] - ?.fields; + this.hass.services.script[ + this.entry?.unique_id || computeObjectId(this.stateObj.entity_id) + ]?.fields; const hasFields = fields && Object.keys(fields).length > 0; @@ -138,17 +142,31 @@ class MoreInfoScript extends LitElement { protected override willUpdate(changedProperties: PropertyValues): void { super.willUpdate(changedProperties); - if (!changedProperties.has("stateObj")) { - return; + if (changedProperties.has("stateObj")) { + const oldState = changedProperties.get("stateObj") as + | HassEntity + | undefined; + const newState = this.stateObj; + + if ( + newState && + (!oldState || oldState.entity_id !== newState.entity_id) + ) { + this._scriptData = { + action: + this.entry?.entity_id === newState.entity_id + ? `script.${this.entry.unique_id}` + : newState.entity_id, + data: {}, + }; + } } - const oldState = changedProperties.get("stateObj") as - | HassEntity - | undefined; - const newState = this.stateObj; - - if (newState && (!oldState || oldState.entity_id !== newState.entity_id)) { - this._scriptData = { action: newState.entity_id, data: {} }; + if (this.entry?.unique_id && changedProperties.has("entry")) { + const action = `script.${this.entry?.unique_id}`; + if (this._scriptData?.action !== action) { + this._scriptData = { ...this._scriptData, action }; + } } } @@ -161,7 +179,7 @@ class MoreInfoScript extends LitElement { ev.stopPropagation(); this.hass.callService( "script", - computeObjectId(this.stateObj!.entity_id), + this.entry?.unique_id || computeObjectId(this.stateObj!.entity_id), this._scriptData.data ); }