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 <mail@bramkragten.nl>

---------

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
karwosts 2024-09-10 00:35:40 -07:00 committed by GitHub
parent d34c43e292
commit 1a67bd0414
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -21,6 +21,7 @@ import { isUnavailableState } from "../../../data/entity";
import { computeObjectId } from "../../../common/entity/compute_object_id"; import { computeObjectId } from "../../../common/entity/compute_object_id";
import { listenMediaQuery } from "../../../common/dom/media_query"; import { listenMediaQuery } from "../../../common/dom/media_query";
import "../components/ha-more-info-state-header"; import "../components/ha-more-info-state-header";
import { ExtEntityRegistryEntry } from "../../../data/entity_registry";
@customElement("more-info-script") @customElement("more-info-script")
class MoreInfoScript extends LitElement { class MoreInfoScript extends LitElement {
@ -28,6 +29,8 @@ class MoreInfoScript extends LitElement {
@property({ attribute: false }) public stateObj?: ScriptEntity; @property({ attribute: false }) public stateObj?: ScriptEntity;
@property({ attribute: false }) public entry?: ExtEntityRegistryEntry;
@state() private _scriptData: Record<string, any> = {}; @state() private _scriptData: Record<string, any> = {};
@state() private narrow = false; @state() private narrow = false;
@ -59,8 +62,9 @@ class MoreInfoScript extends LitElement {
const stateObj = this.stateObj; const stateObj = this.stateObj;
const fields = const fields =
this.hass.services.script[computeObjectId(this.stateObj.entity_id)] this.hass.services.script[
?.fields; this.entry?.unique_id || computeObjectId(this.stateObj.entity_id)
]?.fields;
const hasFields = fields && Object.keys(fields).length > 0; const hasFields = fields && Object.keys(fields).length > 0;
@ -138,17 +142,31 @@ class MoreInfoScript extends LitElement {
protected override willUpdate(changedProperties: PropertyValues): void { protected override willUpdate(changedProperties: PropertyValues): void {
super.willUpdate(changedProperties); super.willUpdate(changedProperties);
if (!changedProperties.has("stateObj")) { if (changedProperties.has("stateObj")) {
return; 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 if (this.entry?.unique_id && changedProperties.has("entry")) {
| HassEntity const action = `script.${this.entry?.unique_id}`;
| undefined; if (this._scriptData?.action !== action) {
const newState = this.stateObj; this._scriptData = { ...this._scriptData, action };
}
if (newState && (!oldState || oldState.entity_id !== newState.entity_id)) {
this._scriptData = { action: newState.entity_id, data: {} };
} }
} }
@ -161,7 +179,7 @@ class MoreInfoScript extends LitElement {
ev.stopPropagation(); ev.stopPropagation();
this.hass.callService( this.hass.callService(
"script", "script",
computeObjectId(this.stateObj!.entity_id), this.entry?.unique_id || computeObjectId(this.stateObj!.entity_id),
this._scriptData.data this._scriptData.data
); );
} }