diff --git a/src/panels/config/automation/ha-automation-editor.ts b/src/panels/config/automation/ha-automation-editor.ts index 07939ecdf6..26fe6cc0f6 100644 --- a/src/panels/config/automation/ha-automation-editor.ts +++ b/src/panels/config/automation/ha-automation-editor.ts @@ -167,10 +167,11 @@ export class HaAutomationEditor extends PreventUnsavedMixin( if ( this._entityRegCreated && this._newAutomationId && - changedProps.has("entityRegistry") + changedProps.has("_entityRegistry") ) { const automation = this._entityRegistry.find( (entity: EntityRegistryEntry) => + entity.platform === "automation" && entity.unique_id === this._newAutomationId ); if (automation) { @@ -927,6 +928,14 @@ export class HaAutomationEditor extends PreventUnsavedMixin( this._saving = true; this._validationErrors = undefined; + let entityRegPromise: Promise | undefined; + if (this._entityRegistryUpdate !== undefined && !this._entityId) { + this._newAutomationId = id; + entityRegPromise = new Promise((resolve) => { + this._entityRegCreated = resolve; + }); + } + try { await saveAutomationConfig(this.hass, id, this._config!); @@ -934,13 +943,8 @@ export class HaAutomationEditor extends PreventUnsavedMixin( let entityId = this._entityId; // wait for automation to appear in entity registry when creating a new automation - if (!entityId) { - this._newAutomationId = id; - const automation = await new Promise( - (resolve) => { - this._entityRegCreated = resolve; - } - ); + if (entityRegPromise) { + const automation = await entityRegPromise; entityId = automation.entity_id; } diff --git a/src/panels/config/script/ha-script-editor.ts b/src/panels/config/script/ha-script-editor.ts index 8c1d65e8ea..c1d9bc39ca 100644 --- a/src/panels/config/script/ha-script-editor.ts +++ b/src/panels/config/script/ha-script-editor.ts @@ -139,7 +139,8 @@ export class HaScriptEditor extends SubscribeMixin( changedProps.has("entityRegistry") ) { const script = this.entityRegistry.find( - (entity: EntityRegistryEntry) => entity.unique_id === this._newScriptId + (entity: EntityRegistryEntry) => + entity.platform === "script" && entity.unique_id === this._newScriptId ); if (script) { this._entityRegCreated(script); @@ -164,7 +165,8 @@ export class HaScriptEditor extends SubscribeMixin( .narrow=${this.narrow} .route=${this.route} .backCallback=${this._backTapped} - .header=${!this._config.alias ? "" : this._config.alias} + .header=${this._config.alias || + this.hass.localize("ui.panel.config.script.editor.default_name")} > ${this.scriptId && !this.narrow ? html` @@ -487,9 +489,7 @@ export class HaScriptEditor extends SubscribeMixin( if (changedProps.has("scriptId") && !this.scriptId && this.hass) { const initData = getScriptEditorInitData(); this._dirty = !!initData; - const baseConfig: Partial = { - alias: this.hass.localize("ui.panel.config.script.editor.default_name"), - }; + const baseConfig: Partial = {}; if (!initData || !("use_blueprint" in initData)) { baseConfig.sequence = []; } @@ -894,6 +894,15 @@ export class HaScriptEditor extends SubscribeMixin( const id = this.scriptId || this._entityId || Date.now(); this._saving = true; + + let entityRegPromise: Promise | undefined; + if (this._entityRegistryUpdate !== undefined && !this.scriptId) { + this._newScriptId = id.toString(); + entityRegPromise = new Promise((resolve) => { + this._entityRegCreated = resolve; + }); + } + try { await this.hass!.callApi( "POST", @@ -902,19 +911,15 @@ export class HaScriptEditor extends SubscribeMixin( ); if (this._entityRegistryUpdate !== undefined) { - let entityId = id.toString().startsWith("script.") - ? id.toString() - : `script.${id}`; + let entityId = this._entityId; // wait for new script to appear in entity registry - if (!this.scriptId) { - const script = await new Promise((resolve) => { - this._entityRegCreated = resolve; - }); + if (entityRegPromise) { + const script = await entityRegPromise; entityId = script.entity_id; } - await updateEntityRegistryEntry(this.hass, entityId, { + await updateEntityRegistryEntry(this.hass, entityId!, { categories: { script: this._entityRegistryUpdate.category || null, },