From f6e00e726213d79972494bd66ed5af7ba22d606c Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Fri, 11 Oct 2024 13:13:17 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20entity=20id=20setting=20on=20newly=20crea?= =?UTF-8?q?ted=20scripts,=20handle=20update=20of=20enti=E2=80=A6=20(#22272?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix entity id setting on newly created scripts, handle update of entity id --- src/panels/config/script/ha-script-editor.ts | 41 +++++++------------- src/translations/en.json | 3 -- 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/src/panels/config/script/ha-script-editor.ts b/src/panels/config/script/ha-script-editor.ts index 551c3f1592..3f69de66a1 100644 --- a/src/panels/config/script/ha-script-editor.ts +++ b/src/panels/config/script/ha-script-editor.ts @@ -83,8 +83,6 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) { @state() private _config?: ScriptConfig; - @state() private _idError = false; - @state() private _dirty = false; @state() private _errors?: string; @@ -414,6 +412,18 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) { this._loadConfig(); } + if ( + (changedProps.has("scriptId") || changedProps.has("entityRegistry")) && + this.scriptId && + this.entityRegistry + ) { + // find entity for when script entity id changed + const entity = this.entityRegistry.find( + (ent) => ent.platform === "script" && ent.unique_id === this.scriptId + ); + this._entityId = entity?.entity_id; + } + if (changedProps.has("scriptId") && !this.scriptId && this.hass) { const initData = getScriptEditorInitData(); this._dirty = !!initData; @@ -448,15 +458,6 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) { } } - private _setEntityId(id?: string) { - this._entityId = id; - if (this.hass.states[`script.${this._entityId}`]) { - this._idError = true; - } else { - this._idError = false; - } - } - private async _checkValidation() { this._validationErrors = undefined; if (!this._entityId || !this._config) { @@ -766,28 +767,12 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) { } private async _saveScript(): Promise { - if (this._idError) { - showToast(this, { - message: this.hass.localize( - "ui.panel.config.script.editor.id_already_exists_save_error" - ), - dismissable: false, - duration: -1, - action: { - action: () => {}, - text: this.hass.localize("ui.dialogs.generic.ok"), - }, - }); - return; - } - if (!this.scriptId) { const saved = await this._promptScriptAlias(); if (!saved) { return; } - const entityId = this._computeEntityIdFromAlias(this._config!.alias); - this._setEntityId(entityId); + this._entityId = this._computeEntityIdFromAlias(this._config!.alias); } const id = this.scriptId || this._entityId || Date.now(); diff --git a/src/translations/en.json b/src/translations/en.json index 029a4cffc8..64f46f3330 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -3684,9 +3684,6 @@ "editor": { "alias": "Name", "icon": "Icon", - "id": "Entity ID", - "id_already_exists_save_error": "You can't save this script because the ID is not unique, pick another ID or leave it blank to automatically generate one.", - "id_already_exists": "This ID already exists", "introduction": "Use scripts to run a sequence of actions.", "show_trace": "[%key:ui::panel::config::automation::editor::show_trace%]", "show_info": "[%key:ui::panel::config::automation::editor::show_info%]",