Skip to content

Commit

Permalink
Fix script more-info when entity_id != unique_id (#21880)
Browse files Browse the repository at this point in the history
* Fix script more-info when entity_id != unique_id

* Update src/dialogs/more-info/controls/more-info-script.ts

Co-authored-by: Bram Kragten <[email protected]>

---------

Co-authored-by: Bram Kragten <[email protected]>
  • Loading branch information
karwosts and bramkragten authored Sep 10, 2024
1 parent d34c43e commit 1a67bd0
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/dialogs/more-info/controls/more-info-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ 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 {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ attribute: false }) public stateObj?: ScriptEntity;

@property({ attribute: false }) public entry?: ExtEntityRegistryEntry;

@state() private _scriptData: Record<string, any> = {};

@state() private narrow = false;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 };
}
}
}

Expand All @@ -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
);
}
Expand Down

0 comments on commit 1a67bd0

Please sign in to comment.