Skip to content

Commit

Permalink
Use promise to wait for entity entry creation
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbede committed Dec 12, 2024
1 parent d871945 commit b43518e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/mixins/prevent-unsaved-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const PreventUnsavedMixin = <T extends Constructor<LitElement>>(
window.removeEventListener("beforeunload", this._handleUnload);
}

public willUpdate(changedProperties: PropertyValues): void {
protected willUpdate(changedProperties: PropertyValues): void {
super.willUpdate(changedProperties);

if (this.isDirty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
52 changes: 34 additions & 18 deletions src/panels/config/automation/ha-automation-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,31 @@ export class HaAutomationEditor extends PreventUnsavedMixin(

private _entityRegistryUpdate?: EntityRegistryUpdate;

private _newAutomationId?: string;

private _entityRegCreated?: (
value: PromiseLike<EntityRegistryEntry> | 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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
),
});
});
}
Expand Down Expand Up @@ -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<void>((resolve) => {
setTimeout(resolve, 1000);
});

const automation = this.entityRegistry.find(
(entity: EntityRegistryEntry) => entity.unique_id === id
this._newAutomationId = id;
const automation = await new Promise<EntityRegistryEntry>(
(resolve) => {
this._entityRegCreated = resolve;
}
);
entityId = automation?.entity_id;
entityId = automation.entity_id;
}

if (entityId) {
Expand Down
43 changes: 34 additions & 9 deletions src/panels/config/script/ha-script-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,30 @@ export class HaScriptEditor extends PreventUnsavedMixin(

private _entityRegistryUpdate?: EntityRegistryUpdate;

private _newScriptId?: string;

private _entityRegCreated?: (
value: PromiseLike<EntityRegistryEntry> | 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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
),
});
});
}
Expand Down Expand Up @@ -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<EntityRegistryEntry>((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 || "",
Expand Down

0 comments on commit b43518e

Please sign in to comment.