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 ( if (
this._entityRegCreated && this._entityRegCreated &&
this._newAutomationId && this._newAutomationId &&
changedProps.has("entityRegistry") changedProps.has("_entityRegistry")
) { ) {
const automation = this._entityRegistry.find( const automation = this._entityRegistry.find(
(entity: EntityRegistryEntry) => (entity: EntityRegistryEntry) =>
entity.platform === "automation" &&
entity.unique_id === this._newAutomationId entity.unique_id === this._newAutomationId
); );
if (automation) { if (automation) {
@ -927,6 +928,14 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
this._saving = true; this._saving = true;
this._validationErrors = undefined; 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 { try {
await saveAutomationConfig(this.hass, id, this._config!); await saveAutomationConfig(this.hass, id, this._config!);
@ -934,13 +943,8 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
let entityId = this._entityId; let entityId = this._entityId;
// wait for automation to appear in entity registry when creating a new automation // wait for automation to appear in entity registry when creating a new automation
if (!entityId) { if (entityRegPromise) {
this._newAutomationId = id; const automation = await entityRegPromise;
const automation = await new Promise<EntityRegistryEntry>(
(resolve) => {
this._entityRegCreated = resolve;
}
);
entityId = automation.entity_id; entityId = automation.entity_id;
} }

View File

@ -139,7 +139,8 @@ export class HaScriptEditor extends SubscribeMixin(
changedProps.has("entityRegistry") changedProps.has("entityRegistry")
) { ) {
const script = this.entityRegistry.find( const script = this.entityRegistry.find(
(entity: EntityRegistryEntry) => entity.unique_id === this._newScriptId (entity: EntityRegistryEntry) =>
entity.platform === "script" && entity.unique_id === this._newScriptId
); );
if (script) { if (script) {
this._entityRegCreated(script); this._entityRegCreated(script);
@ -164,7 +165,8 @@ export class HaScriptEditor extends SubscribeMixin(
.narrow=${this.narrow} .narrow=${this.narrow}
.route=${this.route} .route=${this.route}
.backCallback=${this._backTapped} .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 ${this.scriptId && !this.narrow
? html` ? html`
@ -487,9 +489,7 @@ export class HaScriptEditor extends SubscribeMixin(
if (changedProps.has("scriptId") && !this.scriptId && this.hass) { if (changedProps.has("scriptId") && !this.scriptId && this.hass) {
const initData = getScriptEditorInitData(); const initData = getScriptEditorInitData();
this._dirty = !!initData; this._dirty = !!initData;
const baseConfig: Partial<ScriptConfig> = { const baseConfig: Partial<ScriptConfig> = {};
alias: this.hass.localize("ui.panel.config.script.editor.default_name"),
};
if (!initData || !("use_blueprint" in initData)) { if (!initData || !("use_blueprint" in initData)) {
baseConfig.sequence = []; baseConfig.sequence = [];
} }
@ -894,6 +894,15 @@ export class HaScriptEditor extends SubscribeMixin(
const id = this.scriptId || this._entityId || Date.now(); const id = this.scriptId || this._entityId || Date.now();
this._saving = true; 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 { try {
await this.hass!.callApi( await this.hass!.callApi(
"POST", "POST",
@ -902,19 +911,15 @@ export class HaScriptEditor extends SubscribeMixin(
); );
if (this._entityRegistryUpdate !== undefined) { if (this._entityRegistryUpdate !== undefined) {
let entityId = id.toString().startsWith("script.") let entityId = this._entityId;
? id.toString()
: `script.${id}`;
// wait for new script to appear in entity registry // wait for new script to appear in entity registry
if (!this.scriptId) { if (entityRegPromise) {
const script = await new Promise<EntityRegistryEntry>((resolve) => { const script = await entityRegPromise;
this._entityRegCreated = resolve;
});
entityId = script.entity_id; entityId = script.entity_id;
} }
await updateEntityRegistryEntry(this.hass, entityId, { await updateEntityRegistryEntry(this.hass, entityId!, {
categories: { categories: {
script: this._entityRegistryUpdate.category || null, script: this._entityRegistryUpdate.category || null,
}, },