Skip to content

Commit

Permalink
Make things more general
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbede committed Dec 10, 2024
1 parent c5a714d commit 9334c97
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { haStyle, haStyleDialog } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types";
import type {
AutomationRenameDialogParams,
EntityRegistryUpdate,
ScriptRenameDialogParams,
} from "./show-dialog-automation-rename";

Expand All @@ -31,12 +32,10 @@ class DialogAutomationRename extends LitElement implements HassDialog {

@state() private _error?: string;

@state() private _category?: string;

@state() private _labels?: string[];

@state() private _visibleOptionals: string[] = [];

@state() private _entryUpdates!: EntityRegistryUpdate;

private _params!: AutomationRenameDialogParams | ScriptRenameDialogParams;

private _newName?: string;
Expand All @@ -57,14 +56,14 @@ class DialogAutomationRename extends LitElement implements HassDialog {
`ui.panel.config.${this._params.domain}.editor.default_name`
);
this._newDescription = params.config.description || "";
this._labels = params.labels || [];
this._category = params.category || "";

this._entryUpdates = params.entityRegistryUpdate;

this._visibleOptionals = [
this._newDescription! ? "description" : "",
this._newIcon! ? "icon" : "",
this._category! ? "category" : "",
this._labels.length > 0 ? "labels" : "",
this._entryUpdates.category ? "category" : "",
this._entryUpdates.labels.length > 0 ? "labels" : "",
];
}

Expand Down Expand Up @@ -173,17 +172,19 @@ class DialogAutomationRename extends LitElement implements HassDialog {
: nothing}
${this._visibleOptionals.includes("category")
? html` <ha-category-picker
id="category"
.hass=${this.hass}
.scope=${this._params.domain}
.value=${this._category}
@value-changed=${this._categoryChanged}
.value=${this._entryUpdates.category}
@value-changed=${this._registryEntryChanged}
></ha-category-picker>`
: nothing}
${this._visibleOptionals.includes("labels")
? html` <ha-labels-picker
id="labels"
.hass=${this.hass}
.value=${this._labels}
@value-changed=${this._labelsChanged}
.value=${this._entryUpdates.labels}
@value-changed=${this._registryEntryChanged}
></ha-labels-picker>`
: nothing}
Expand Down Expand Up @@ -238,14 +239,12 @@ class DialogAutomationRename extends LitElement implements HassDialog {
this._visibleOptionals = [...this._visibleOptionals, option];
}

private _categoryChanged(ev: CustomEvent): void {
private _registryEntryChanged(ev) {
ev.stopPropagation();
this._category = ev.detail.value;
}
const id: string = ev.target.id;
const value = ev.detail.value;

private _labelsChanged(ev: CustomEvent) {
ev.stopPropagation();
this._labels = ev.detail.value;
this._entryUpdates = { ...this._entryUpdates, [id]: value };
}

private _iconChanged(ev: CustomEvent) {
Expand All @@ -268,6 +267,7 @@ class DialogAutomationRename extends LitElement implements HassDialog {
this._error = "Name is required";
return;
}

if (this._params.domain === "script") {
this._params.updateConfig(
{
Expand All @@ -276,8 +276,7 @@ class DialogAutomationRename extends LitElement implements HassDialog {
description: this._newDescription,
icon: this._newIcon,
},
this._category,
this._labels
this._entryUpdates
);
} else {
this._params.updateConfig(
Expand All @@ -286,8 +285,7 @@ class DialogAutomationRename extends LitElement implements HassDialog {
alias: this._newName,
description: this._newDescription,
},
this._category,
this._labels
this._entryUpdates
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ export const loadAutomationRenameDialog = () =>
import("./dialog-automation-rename");

interface BaseRenameDialogParams {
category?: string;
labels?: string[];
entityRegistryUpdate: EntityRegistryUpdate;
onClose: () => void;
}

export interface EntityRegistryUpdate {
labels: string[];
category: string;
}

export interface AutomationRenameDialogParams extends BaseRenameDialogParams {
config: AutomationConfig;
domain: "automation";
updateConfig: (
config: AutomationConfig,
category?: string,
labels?: string[]
entityRegistryUpdate: EntityRegistryUpdate
) => void;
}

Expand All @@ -26,8 +29,7 @@ export interface ScriptRenameDialogParams extends BaseRenameDialogParams {
domain: "script";
updateConfig: (
config: ScriptConfig,
category?: string,
labels?: string[]
entityRegistryUpdate: EntityRegistryUpdate
) => void;
}

Expand Down
42 changes: 23 additions & 19 deletions src/panels/config/automation/ha-automation-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ import type { Entries, HomeAssistant, Route } from "../../../types";
import { showToast } from "../../../util/toast";
import "../ha-config-section";
import { showAutomationModeDialog } from "./automation-mode-dialog/show-dialog-automation-mode";
import { showAutomationRenameDialog } from "./automation-rename-dialog/show-dialog-automation-rename";
import {
type EntityRegistryUpdate,
showAutomationRenameDialog,
} from "./automation-rename-dialog/show-dialog-automation-rename";
import "./blueprint-automation-editor";
import "./manual-automation-editor";

Expand Down Expand Up @@ -96,6 +99,8 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {

@property({ attribute: false }) public automations!: AutomationEntity[];

@property({ attribute: false }) public entityRegistry!: EntityRegistryEntry[];

@property({ attribute: "is-wide", type: Boolean }) public isWide = false;

@property({ type: Boolean }) public narrow = false;
Expand Down Expand Up @@ -127,9 +132,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {

private _configSubscriptionsId = 1;

@state() private _category?: string;

@state() private _labels?: string[];
private _entityRegistryUpdate!: EntityRegistryUpdate;

@state() private _saving = false;

Expand Down Expand Up @@ -509,14 +512,17 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
sub(this._config)
);
}
}

public async firstUpdated(changedProps: PropertyValues) {
super.firstUpdated(changedProps);
const entityRegistry = await fetchEntityRegistry(this.hass.connection);
const entry = this._getAutomationEntity(entityRegistry, this._entityId);
this._category = entry?.categories?.automation;
this._labels = entry?.labels;
if (changedProps.has("entityRegistry")) {
const entry = this._getAutomationEntity(
this.entityRegistry,
this._entityId
);
this._entityRegistryUpdate = {
category: entry?.categories?.automation || "",
labels: entry?.labels || [],
};
}
}

private _setEntityId() {
Expand Down Expand Up @@ -802,17 +808,15 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
showAutomationRenameDialog(this, {
config: this._config!,
domain: "automation",
updateConfig: (config, category, labels) => {
updateConfig: (config, entityRegistryUpdate) => {
this._config = config;
this._category = category;
this._labels = labels;
this._entityRegistryUpdate = entityRegistryUpdate;
this._dirty = true;
this.requestUpdate();
resolve(true);
},
onClose: () => resolve(false),
category: this._category,
labels: this._labels,
entityRegistryUpdate: this._entityRegistryUpdate,
});
});
}
Expand Down Expand Up @@ -854,7 +858,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
try {
await saveAutomationConfig(this.hass, id, this._config!);

if (this._category !== undefined || this._labels !== undefined) {
if (this._entityRegistryUpdate !== undefined) {
let entityId = this._entityId;

// no entity id check if we have it in the automations list
Expand All @@ -879,9 +883,9 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
if (entityId) {
await updateEntityRegistryEntry(this.hass, entityId, {
categories: {
automation: this._category || "",
automation: this._entityRegistryUpdate.category,
},
labels: this._labels || [],
labels: this._entityRegistryUpdate.labels,
});
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/panels/config/automation/ha-config-automation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { HassEntities } from "home-assistant-js-websocket";
import type { PropertyValues } from "lit";
import { customElement, property } from "lit/decorators";
import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { consume } from "@lit-labs/context";
import { computeStateDomain } from "../../../common/entity/compute_state_domain";
import { debounce } from "../../../common/util/debounce";
import type { AutomationEntity } from "../../../data/automation";
Expand All @@ -10,6 +11,8 @@ import { HassRouterPage } from "../../../layouts/hass-router-page";
import type { HomeAssistant } from "../../../types";
import "./ha-automation-editor";
import "./ha-automation-picker";
import { fullEntitiesContext } from "../../../data/context";
import type { EntityRegistryEntry } from "../../../data/entity_registry";

const equal = (a: AutomationEntity[], b: AutomationEntity[]): boolean => {
if (a.length !== b.length) {
Expand All @@ -30,6 +33,10 @@ class HaConfigAutomation extends HassRouterPage {

@property({ attribute: false }) public automations: AutomationEntity[] = [];

@state()
@consume({ context: fullEntitiesContext, subscribe: true })
_entityReg!: EntityRegistryEntry[];

private _debouncedUpdateAutomations = debounce((pageEl) => {
const newAutomations = this._getAutomations(this.hass.states);
if (!equal(newAutomations, pageEl.automations)) {
Expand Down Expand Up @@ -77,6 +84,7 @@ class HaConfigAutomation extends HassRouterPage {
pageEl.isWide = this.isWide;
pageEl.route = this.routeTail;
pageEl.showAdvanced = this.showAdvanced;
pageEl.entityRegistry = this._entityReg;

if (this.hass) {
if (!pageEl.automations || !changedProps) {
Expand Down
27 changes: 13 additions & 14 deletions src/panels/config/script/ha-script-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { haStyle } from "../../../resources/styles";
import type { Entries, HomeAssistant, Route } from "../../../types";
import { showToast } from "../../../util/toast";
import { showAutomationModeDialog } from "../automation/automation-mode-dialog/show-dialog-automation-mode";
import type { EntityRegistryUpdate } from "../automation/automation-rename-dialog/show-dialog-automation-rename";
import { showAutomationRenameDialog } from "../automation/automation-rename-dialog/show-dialog-automation-rename";
import "./blueprint-script-editor";
import "./manual-script-editor";
Expand Down Expand Up @@ -97,12 +98,10 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {

@state() private _blueprintConfig?: BlueprintScriptConfig;

@state() private _category?: string;

@state() private _labels?: string[];

@state() private _saving = false;

private _entityRegistryUpdate!: EntityRegistryUpdate;

protected render(): TemplateResult | typeof nothing {
if (!this._config) {
return nothing;
Expand Down Expand Up @@ -416,8 +415,10 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
(ent) => ent.platform === "script" && ent.unique_id === this.scriptId
);
this._entityId = entity?.entity_id;
this._category = entity?.categories?.script;
this._labels = entity?.labels;
this._entityRegistryUpdate = {
category: entity?.categories?.script || "",
labels: entity?.labels || [],
};
}

if (changedProps.has("scriptId") && !this.scriptId && this.hass) {
Expand Down Expand Up @@ -756,17 +757,15 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
showAutomationRenameDialog(this, {
config: this._config!,
domain: "script",
updateConfig: (config, category, labels) => {
updateConfig: (config, entityRegistryUpdate) => {
this._config = config;
this._category = category;
this._labels = labels;
this._entityRegistryUpdate = entityRegistryUpdate;
this._dirty = true;
this.requestUpdate();
resolve(true);
},
onClose: () => resolve(false),
category: this._category,
labels: this._labels,
entityRegistryUpdate: this._entityRegistryUpdate,
});
});
}
Expand Down Expand Up @@ -811,7 +810,7 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
this._config
);

if (this._category !== undefined || this._labels !== undefined) {
if (this._entityRegistryUpdate !== undefined) {
// wait for new script to appear in entity registry
if (!this.scriptId) {
await new Promise<void>((resolve, _reject) => {
Expand All @@ -824,9 +823,9 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
: `script.${id}`;
await updateEntityRegistryEntry(this.hass, entityId, {
categories: {
script: this._category || "",
script: this._entityRegistryUpdate.category,
},
labels: this._labels || [],
labels: this._entityRegistryUpdate.labels,
});
}

Expand Down

0 comments on commit 9334c97

Please sign in to comment.