From b43518e9b3f189fdbc3840be0fb3e66d4d49c0e9 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Thu, 12 Dec 2024 19:31:26 +0100 Subject: [PATCH] Use promise to wait for entity entry creation --- src/mixins/prevent-unsaved-mixin.ts | 2 +- .../dialog-automation-rename.ts | 5 +- .../show-dialog-automation-rename.ts | 2 + .../config/automation/ha-automation-editor.ts | 52 ++++++++++++------- src/panels/config/script/ha-script-editor.ts | 43 +++++++++++---- 5 files changed, 73 insertions(+), 31 deletions(-) diff --git a/src/mixins/prevent-unsaved-mixin.ts b/src/mixins/prevent-unsaved-mixin.ts index a1c7b5af9219..352710ba4e7a 100644 --- a/src/mixins/prevent-unsaved-mixin.ts +++ b/src/mixins/prevent-unsaved-mixin.ts @@ -30,7 +30,7 @@ export const PreventUnsavedMixin = >( window.removeEventListener("beforeunload", this._handleUnload); } - public willUpdate(changedProperties: PropertyValues): void { + protected willUpdate(changedProperties: PropertyValues): void { super.willUpdate(changedProperties); if (this.isDirty) { diff --git a/src/panels/config/automation/automation-rename-dialog/dialog-automation-rename.ts b/src/panels/config/automation/automation-rename-dialog/dialog-automation-rename.ts index 326e9a7a1bbb..f629901a6c25 100644 --- a/src/panels/config/automation/automation-rename-dialog/dialog-automation-rename.ts +++ b/src/panels/config/automation/automation-rename-dialog/dialog-automation-rename.ts @@ -56,10 +56,9 @@ class DialogAutomationRename extends LitElement implements HassDialog { `ui.panel.config.${this._params.domain}.editor.default_name` ); this._newDescription = params.config.description || ""; - this._entryUpdates = params.entityRegistryUpdate || { - labels: [], - category: "", + labels: params.entityRegistryEntry?.labels || [], + category: params.entityRegistryEntry?.categories[params.domain] || "", }; this._visibleOptionals = [ diff --git a/src/panels/config/automation/automation-rename-dialog/show-dialog-automation-rename.ts b/src/panels/config/automation/automation-rename-dialog/show-dialog-automation-rename.ts index 18ee74b9de2b..7ee809c3c08c 100644 --- a/src/panels/config/automation/automation-rename-dialog/show-dialog-automation-rename.ts +++ b/src/panels/config/automation/automation-rename-dialog/show-dialog-automation-rename.ts @@ -1,12 +1,14 @@ import { fireEvent } from "../../../../common/dom/fire_event"; import type { AutomationConfig } from "../../../../data/automation"; import type { ScriptConfig } from "../../../../data/script"; +import type { EntityRegistryEntry } from "../../../../data/entity_registry"; export const loadAutomationRenameDialog = () => import("./dialog-automation-rename"); interface BaseRenameDialogParams { entityRegistryUpdate?: EntityRegistryUpdate; + entityRegistryEntry?: EntityRegistryEntry; onClose: () => void; } diff --git a/src/panels/config/automation/ha-automation-editor.ts b/src/panels/config/automation/ha-automation-editor.ts index f03d059530cf..d269b4d1f6fb 100644 --- a/src/panels/config/automation/ha-automation-editor.ts +++ b/src/panels/config/automation/ha-automation-editor.ts @@ -135,6 +135,31 @@ export class HaAutomationEditor extends PreventUnsavedMixin( private _entityRegistryUpdate?: EntityRegistryUpdate; + private _newAutomationId?: string; + + private _entityRegCreated?: ( + value: PromiseLike | EntityRegistryEntry + ) => void; + + protected willUpdate(changedProps) { + super.willUpdate(changedProps); + + if ( + this._entityRegCreated && + this._newAutomationId && + changedProps.has("entityRegistry") + ) { + const automation = this.entityRegistry.find( + (entity: EntityRegistryEntry) => + entity.unique_id === this._newAutomationId + ); + if (automation) { + this._entityRegCreated(automation); + this._entityRegCreated = undefined; + } + } + } + protected render(): TemplateResult | typeof nothing { if (!this._config) { return nothing; @@ -503,17 +528,6 @@ export class HaAutomationEditor extends PreventUnsavedMixin( sub(this._config) ); } - - if (changedProps.has("entityRegistry") && this.automationId) { - const entry = this.entityRegistry.find( - (ent) => - ent.platform === "automation" && ent.unique_id === this.automationId - ); - this._entityRegistryUpdate = { - category: entry?.categories?.automation || "", - labels: entry?.labels || [], - }; - } } private _setEntityId() { @@ -807,6 +821,9 @@ export class HaAutomationEditor extends PreventUnsavedMixin( }, onClose: () => resolve(false), entityRegistryUpdate: this._entityRegistryUpdate, + entityRegistryEntry: this.entityRegistry.find( + (entry) => entry.unique_id === this.automationId + ), }); }); } @@ -853,14 +870,13 @@ export class HaAutomationEditor extends PreventUnsavedMixin( // wait for automation to appear in entity registry when creating a new automation if (!entityId) { - await new Promise((resolve) => { - setTimeout(resolve, 1000); - }); - - const automation = this.entityRegistry.find( - (entity: EntityRegistryEntry) => entity.unique_id === id + this._newAutomationId = id; + const automation = await new Promise( + (resolve) => { + this._entityRegCreated = resolve; + } ); - entityId = automation?.entity_id; + entityId = automation.entity_id; } if (entityId) { diff --git a/src/panels/config/script/ha-script-editor.ts b/src/panels/config/script/ha-script-editor.ts index 134b5ee56ddf..6b00eb285ac6 100644 --- a/src/panels/config/script/ha-script-editor.ts +++ b/src/panels/config/script/ha-script-editor.ts @@ -105,6 +105,30 @@ export class HaScriptEditor extends PreventUnsavedMixin( private _entityRegistryUpdate?: EntityRegistryUpdate; + private _newScriptId?: string; + + private _entityRegCreated?: ( + value: PromiseLike | EntityRegistryEntry + ) => void; + + protected willUpdate(changedProps) { + super.willUpdate(changedProps); + + if ( + this._entityRegCreated && + this._newScriptId && + changedProps.has("entityRegistry") + ) { + const script = this.entityRegistry.find( + (entity: EntityRegistryEntry) => entity.unique_id === this._newScriptId + ); + if (script) { + this._entityRegCreated(script); + this._entityRegCreated = undefined; + } + } + } + protected render(): TemplateResult | typeof nothing { if (!this._config) { return nothing; @@ -417,10 +441,6 @@ export class HaScriptEditor extends PreventUnsavedMixin( (ent) => ent.platform === "script" && ent.unique_id === this.scriptId ); this._entityId = entity?.entity_id; - this._entityRegistryUpdate = { - category: entity?.categories?.script || "", - labels: entity?.labels || [], - }; } if (changedProps.has("scriptId") && !this.scriptId && this.hass) { @@ -768,6 +788,9 @@ export class HaScriptEditor extends PreventUnsavedMixin( }, onClose: () => resolve(false), entityRegistryUpdate: this._entityRegistryUpdate, + entityRegistryEntry: this.entityRegistry.find( + (entry) => entry.unique_id === this.scriptId + ), }); }); } @@ -813,16 +836,18 @@ export class HaScriptEditor extends PreventUnsavedMixin( ); if (this._entityRegistryUpdate !== undefined) { + let entityId = id.toString().startsWith("script.") + ? id.toString() + : `script.${id}`; + // wait for new script to appear in entity registry if (!this.scriptId) { - await new Promise((resolve, _reject) => { - setTimeout(resolve, 1000); + const script = await new Promise((resolve) => { + this._entityRegCreated = resolve; }); + entityId = script.entity_id; } - const entityId = id.toString().startsWith("script.") - ? id.toString() - : `script.${id}`; await updateEntityRegistryEntry(this.hass, entityId, { categories: { script: this._entityRegistryUpdate.category || "",