Skip to content

Commit

Permalink
Do not overwrite user input when the registry changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbede committed Dec 11, 2024
1 parent 7666465 commit a55dba2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ class DialogAutomationRename extends LitElement implements HassDialog {
);
this._newDescription = params.config.description || "";

this._entryUpdates = params.entityRegistryUpdate;
this._entryUpdates = params.entityRegistryUpdate || {
labels: [],
category: "",
};

this._visibleOptionals = [
this._newDescription! ? "description" : "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const loadAutomationRenameDialog = () =>
import("./dialog-automation-rename");

interface BaseRenameDialogParams {
entityRegistryUpdate: EntityRegistryUpdate;
entityRegistryUpdate?: EntityRegistryUpdate;
onClose: () => void;
}

Expand Down
4 changes: 2 additions & 2 deletions src/panels/config/automation/dialog-new-automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,24 +205,24 @@ class DialogNewAutomation extends LitElement implements HassDialog {
return;
}
const path = (ev.currentTarget! as any).path;
this.closeDialog();
if (this._mode === "script") {
showScriptEditor({ use_blueprint: { path } });
} else {
showAutomationEditor({ use_blueprint: { path } });
}
this.closeDialog();
}

private async _blank(ev) {
if (!shouldHandleRequestSelectedEvent(ev)) {
return;
}
this.closeDialog();
if (this._mode === "script") {
showScriptEditor();
} else {
showAutomationEditor();
}
this.closeDialog();
}

static get styles(): CSSResultGroup {
Expand Down
67 changes: 22 additions & 45 deletions src/panels/config/automation/ha-automation-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import type { CSSResultGroup, PropertyValues, TemplateResult } from "lit";
import { LitElement, css, html, nothing } from "lit";
import { property, state } from "lit/decorators";
import { classMap } from "lit/directives/class-map";
import memoizeOne from "memoize-one";
import { fireEvent } from "../../../common/dom/fire_event";
import { navigate } from "../../../common/navigate";
import { computeRTL } from "../../../common/util/compute_rtl";
Expand Down Expand Up @@ -51,10 +50,7 @@ import { substituteBlueprint } from "../../../data/blueprint";
import { validateConfig } from "../../../data/config";
import { UNAVAILABLE } from "../../../data/entity";
import type { EntityRegistryEntry } from "../../../data/entity_registry";
import {
fetchEntityRegistry,
updateEntityRegistryEntry,
} from "../../../data/entity_registry";
import { updateEntityRegistryEntry } from "../../../data/entity_registry";
import {
showAlertDialog,
showConfirmationDialog,
Expand Down Expand Up @@ -128,23 +124,16 @@ export class HaAutomationEditor extends PreventUnsavedMixin(

@state() private _blueprintConfig?: BlueprintAutomationConfig;

@state() private _saving = false;

private _configSubscriptions: Record<
string,
(config?: AutomationConfig) => void
> = {};

private _configSubscriptionsId = 1;

private _entityRegistryUpdate!: EntityRegistryUpdate;

@state() private _saving = false;

private _getAutomationEntity = memoizeOne(
(automations: EntityRegistryEntry[], entity_id: string | undefined) =>
automations.find(
(entity: EntityRegistryEntry) => entity.entity_id === entity_id
)
);
private _entityRegistryUpdate?: EntityRegistryUpdate;

protected render(): TemplateResult | typeof nothing {
if (!this._config) {
Expand Down Expand Up @@ -438,7 +427,6 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
slot="fab"
class=${classMap({
dirty: !this._readOnly && this._dirty,
saving: this._saving,
})}
.label=${this.hass.localize("ui.panel.config.automation.editor.save")}
.disabled=${this._saving}
Expand Down Expand Up @@ -516,10 +504,10 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
);
}

if (changedProps.has("entityRegistry")) {
const entry = this._getAutomationEntity(
this.entityRegistry,
this._entityId
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 || "",
Expand Down Expand Up @@ -572,8 +560,7 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
this._config = normalizeAutomationConfig(config);
this._checkValidation();
} catch (err: any) {
const entityRegistry = await fetchEntityRegistry(this.hass.connection);
const entity = entityRegistry.find(
const entity = this.entityRegistry.find(
(ent) =>
ent.platform === "automation" && ent.unique_id === this.automationId
);
Expand Down Expand Up @@ -864,33 +851,26 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
if (this._entityRegistryUpdate !== undefined) {
let entityId = this._entityId;

// no entity id check if we have it in the automations list
// wait for automation to appear in entity registry when creating a new automation
if (!entityId) {
let automation = this.automations.find(
(entity: AutomationEntity) => entity.attributes.id === id
await new Promise<void>((resolve) => {
setTimeout(resolve, 1000);
});

const automation = this.entityRegistry.find(
(entity: EntityRegistryEntry) => entity.unique_id === id
);
entityId = automation?.entity_id;

// wait for new automation to appear in entity registry
if (!entityId) {
await new Promise<void>((resolve, _reject) => {
setTimeout(resolve, 3000);
if (entityId) {
await updateEntityRegistryEntry(this.hass, entityId, {
categories: {
automation: this._entityRegistryUpdate.category || "",
},
labels: this._entityRegistryUpdate.labels || [],
});
automation = this.automations.find(
(entity: AutomationEntity) => entity.attributes.id === id
);
entityId = automation?.entity_id;
}
}

if (entityId) {
await updateEntityRegistryEntry(this.hass, entityId, {
categories: {
automation: this._entityRegistryUpdate.category,
},
labels: this._entityRegistryUpdate.labels,
});
}
}

this._dirty = false;
Expand Down Expand Up @@ -977,9 +957,6 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
ha-fab.dirty {
bottom: 0;
}
ha-fab.saving {
opacity: var(--light-disabled-opacity);
}
li[role="separator"] {
border-bottom-color: var(--divider-color);
}
Expand Down
14 changes: 5 additions & 9 deletions src/panels/config/script/ha-script-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class HaScriptEditor extends PreventUnsavedMixin(

@state() private _saving = false;

private _entityRegistryUpdate!: EntityRegistryUpdate;
private _entityRegistryUpdate?: EntityRegistryUpdate;

protected render(): TemplateResult | typeof nothing {
if (!this._config) {
Expand Down Expand Up @@ -378,7 +378,6 @@ export class HaScriptEditor extends PreventUnsavedMixin(
slot="fab"
class=${classMap({
dirty: !this._readOnly && this._dirty,
saving: this._saving,
})}
.label=${this.hass.localize(
"ui.panel.config.script.editor.save_script"
Expand Down Expand Up @@ -816,8 +815,8 @@ export class HaScriptEditor extends PreventUnsavedMixin(
if (this._entityRegistryUpdate !== undefined) {
// wait for new script to appear in entity registry
if (!this.scriptId) {
await new Promise<void>((resolve, _reject) => {
setTimeout(resolve, 3000);
await new Promise((resolve, _reject) => {
setTimeout(resolve, 1000);
});
}

Expand All @@ -826,9 +825,9 @@ export class HaScriptEditor extends PreventUnsavedMixin(
: `script.${id}`;
await updateEntityRegistryEntry(this.hass, entityId, {
categories: {
script: this._entityRegistryUpdate.category,
script: this._entityRegistryUpdate.category || "",
},
labels: this._entityRegistryUpdate.labels,
labels: this._entityRegistryUpdate.labels || [],
});
}

Expand Down Expand Up @@ -912,9 +911,6 @@ export class HaScriptEditor extends PreventUnsavedMixin(
ha-fab.dirty {
bottom: 0;
}
ha-fab.saving {
opacity: var(--light-disabled-opacity);
}
li[role="separator"] {
border-bottom-color: var(--divider-color);
}
Expand Down

0 comments on commit a55dba2

Please sign in to comment.