fix saving new automation/script (#23433)

This commit is contained in:
Bram Kragten 2024-12-24 14:44:23 +01:00 committed by GitHub
parent 94f679e387
commit 361caafab9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 21 deletions

View File

@ -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<EntityRegistryEntry> | undefined;
if (this._entityRegistryUpdate !== undefined && !this._entityId) {
this._newAutomationId = id;
entityRegPromise = new Promise<EntityRegistryEntry>((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<EntityRegistryEntry>(
(resolve) => {
this._entityRegCreated = resolve;
}
);
if (entityRegPromise) {
const automation = await entityRegPromise;
entityId = automation.entity_id;
}

View File

@ -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<ScriptConfig> = {
alias: this.hass.localize("ui.panel.config.script.editor.default_name"),
};
const baseConfig: Partial<ScriptConfig> = {};
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<EntityRegistryEntry> | undefined;
if (this._entityRegistryUpdate !== undefined && !this.scriptId) {
this._newScriptId = id.toString();
entityRegPromise = new Promise<EntityRegistryEntry>((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<EntityRegistryEntry>((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,
},