Skip to content

Commit 3b2a1ed

Browse files
Copilotwendevlin
andcommitted
Changes before error encountered
Co-authored-by: wendevlin <12148533+wendevlin@users.noreply.github.com>
1 parent 1c50432 commit 3b2a1ed

3 files changed

Lines changed: 145 additions & 227 deletions

File tree

src/panels/config/automation/ha-automation-editor.ts

Lines changed: 4 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ import { css, html, LitElement, nothing } from "lit";
2727
import { customElement, property, query, state } from "lit/decorators";
2828
import { classMap } from "lit/directives/class-map";
2929
import { UndoRedoController } from "../../../common/controllers/undo-redo-controller";
30-
import { transform } from "../../../common/decorators/transform";
3130
import { fireEvent } from "../../../common/dom/fire_event";
3231
import { goBack, navigate } from "../../../common/navigate";
3332
import { promiseTimeout } from "../../../common/util/promise-timeout";
34-
import { afterNextRender } from "../../../common/util/render-status";
3533
import "../../../components/ha-button";
3634
import "../../../components/ha-dropdown";
3735
import "../../../components/ha-dropdown-item";
@@ -72,27 +70,24 @@ import {
7270
showAlertDialog,
7371
showConfirmationDialog,
7472
} from "../../../dialogs/generic/show-dialog-box";
75-
import { showMoreInfoDialog } from "../../../dialogs/more-info/show-ha-more-info-dialog";
7673
import "../../../layouts/hass-subpage";
7774
import { KeyboardShortcutMixin } from "../../../mixins/keyboard-shortcut-mixin";
7875
import { PreventUnsavedMixin } from "../../../mixins/prevent-unsaved-mixin";
7976
import { haStyle } from "../../../resources/styles";
8077
import type {
8178
Entries,
82-
HomeAssistant,
83-
Route,
8479
ValueChangedEvent,
8580
} from "../../../types";
8681
import { isMac } from "../../../util/is_mac";
8782
import { showToast } from "../../../util/toast";
8883
import { showAssignCategoryDialog } from "../category/show-dialog-assign-category";
8984
import { showAutomationModeDialog } from "./automation-mode-dialog/show-dialog-automation-mode";
9085
import {
91-
type EntityRegistryUpdate,
9286
showAutomationSaveDialog,
9387
} from "./automation-save-dialog/show-dialog-automation-save";
9488
import { showAutomationSaveTimeoutDialog } from "./automation-save-timeout-dialog/show-dialog-automation-save-timeout";
9589
import "./blueprint-automation-editor";
90+
import { AutomationScriptEditorMixin } from "./ha-automation-script-editor-mixin";
9691
import "./manual-automation-editor";
9792
import type { HaManualAutomationEditor } from "./manual-automation-editor";
9893
import type { HaDropdownSelectEvent } from "../../../components/ha-dropdown";
@@ -119,53 +114,13 @@ declare global {
119114
}
120115

121116
@customElement("ha-automation-editor")
122-
export class HaAutomationEditor extends PreventUnsavedMixin(
123-
KeyboardShortcutMixin(LitElement)
117+
export class HaAutomationEditor extends AutomationScriptEditorMixin<AutomationConfig>(
118+
PreventUnsavedMixin(KeyboardShortcutMixin(LitElement))
124119
) {
125-
@property({ attribute: false }) public hass!: HomeAssistant;
126-
127120
@property({ attribute: false }) public automationId: string | null = null;
128121

129-
@property({ attribute: false }) public entityId: string | null = null;
130-
131122
@property({ attribute: false }) public automations!: AutomationEntity[];
132123

133-
@property({ attribute: "is-wide", type: Boolean }) public isWide = false;
134-
135-
@property({ type: Boolean }) public narrow = false;
136-
137-
@property({ attribute: false }) public route!: Route;
138-
139-
@state() private _config?: AutomationConfig;
140-
141-
@state() private _dirty = false;
142-
143-
@state() private _errors?: string;
144-
145-
@state() private _yamlErrors?: string;
146-
147-
@state() private _entityId?: string;
148-
149-
@state() private _mode: "gui" | "yaml" = "gui";
150-
151-
@state() private _readOnly = false;
152-
153-
@state() private _validationErrors?: (string | TemplateResult)[];
154-
155-
@state() private _blueprintConfig?: BlueprintAutomationConfig;
156-
157-
@state()
158-
@consume({ context: fullEntitiesContext, subscribe: true })
159-
@transform<EntityRegistryEntry[], EntityRegistryEntry>({
160-
transformer: function (this: HaAutomationEditor, value) {
161-
return value.find(({ entity_id }) => entity_id === this._entityId);
162-
},
163-
watch: ["_entityId"],
164-
})
165-
private _registryEntry?: EntityRegistryEntry;
166-
167-
@state() private _saving = false;
168-
169124
@state()
170125
@consume({ context: fullEntitiesContext, subscribe: true })
171126
_entityRegistry!: EntityRegistryEntry[];
@@ -180,14 +135,8 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
180135

181136
private _configSubscriptionsId = 1;
182137

183-
private _entityRegistryUpdate?: EntityRegistryUpdate;
184-
185138
private _newAutomationId?: string;
186139

187-
private _entityRegCreated?: (
188-
value: PromiseLike<EntityRegistryEntry> | EntityRegistryEntry
189-
) => void;
190-
191140
private _undoRedoController = new UndoRedoController<AutomationConfig>(this, {
192141
apply: (config) => this._applyUndoRedo(config),
193142
currentConfig: () => this._config!,
@@ -780,13 +729,6 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
780729
fireEvent(this, "hass-more-info", { entityId: this._entityId });
781730
}
782731

783-
private _showSettings() {
784-
showMoreInfoDialog(this, {
785-
entityId: this._entityId!,
786-
view: "settings",
787-
});
788-
}
789-
790732
private _editCategory() {
791733
if (!this._registryEntry) {
792734
showAlertDialog(this, {
@@ -861,7 +803,7 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
861803
this._errors = undefined;
862804
}
863805

864-
private async _confirmUnsavedChanged(): Promise<boolean> {
806+
protected async _confirmUnsavedChanged(): Promise<boolean> {
865807
if (!this._dirty) {
866808
return true;
867809
}
@@ -906,13 +848,6 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
906848
});
907849
}
908850

909-
private _backTapped = async () => {
910-
const result = await this._confirmUnsavedChanged();
911-
if (result) {
912-
afterNextRender(() => goBack("/config"));
913-
}
914-
};
915-
916851
private async _takeControl() {
917852
const config = this._config as BlueprintAutomationConfig;
918853

@@ -943,21 +878,6 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
943878
}
944879
}
945880

946-
private _revertBlueprint() {
947-
this._config = this._blueprintConfig;
948-
if (this._mode === "yaml") {
949-
this.renderRoot.querySelector("ha-yaml-editor")?.setValue(this._config);
950-
}
951-
this._blueprintConfig = undefined;
952-
this._readOnly = false;
953-
}
954-
955-
private _takeControlSave() {
956-
this._readOnly = false;
957-
this._dirty = true;
958-
this._blueprintConfig = undefined;
959-
}
960-
961881
private async _duplicate() {
962882
const result = this._readOnly
963883
? await showConfirmationDialog(this, {
@@ -1001,28 +921,6 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
1001921
}
1002922
}
1003923

1004-
private async _switchUiMode() {
1005-
if (this._yamlErrors) {
1006-
const result = await showConfirmationDialog(this, {
1007-
text: html`${this.hass.localize(
1008-
"ui.panel.config.automation.editor.switch_ui_yaml_error"
1009-
)}<br /><br />${this._yamlErrors}`,
1010-
confirmText: this.hass!.localize("ui.common.continue"),
1011-
destructive: true,
1012-
dismissText: this.hass!.localize("ui.common.cancel"),
1013-
});
1014-
if (!result) {
1015-
return;
1016-
}
1017-
}
1018-
this._yamlErrors = undefined;
1019-
this._mode = "gui";
1020-
}
1021-
1022-
private _switchYamlMode() {
1023-
this._mode = "yaml";
1024-
}
1025-
1026924
private async _promptAutomationAlias(): Promise<boolean> {
1027925
return new Promise((resolve) => {
1028926
showAutomationSaveDialog(this, {
@@ -1173,14 +1071,6 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
11731071
};
11741072
}
11751073

1176-
protected get isDirty() {
1177-
return this._dirty;
1178-
}
1179-
1180-
protected async promptDiscardChanges() {
1181-
return this._confirmUnsavedChanged();
1182-
}
1183-
11841074
// @ts-ignore
11851075
private _collapseAll() {
11861076
this._manualEditor?.collapseAll();
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { consume } from "@lit/context";
2+
import type { TemplateResult } from "lit";
3+
import { html, LitElement } from "lit";
4+
import { property, state } from "lit/decorators";
5+
import { transform } from "../../../common/decorators/transform";
6+
import { goBack } from "../../../common/navigate";
7+
import { afterNextRender } from "../../../common/util/render-status";
8+
import { fullEntitiesContext } from "../../../data/context";
9+
import type { EntityRegistryEntry } from "../../../data/entity/entity_registry";
10+
import {
11+
showConfirmationDialog,
12+
} from "../../../dialogs/generic/show-dialog-box";
13+
import { showMoreInfoDialog } from "../../../dialogs/more-info/show-ha-more-info-dialog";
14+
import type { Constructor, HomeAssistant, Route } from "../../../types";
15+
import type { EntityRegistryUpdate } from "./automation-save-dialog/show-dialog-automation-save";
16+
17+
type BaseEditorConfig = { alias?: string };
18+
19+
export const AutomationScriptEditorMixin = <
20+
TConfig extends BaseEditorConfig,
21+
T extends Constructor<LitElement>,
22+
>(
23+
superClass: T
24+
) =>
25+
class AutomationScriptEditorClass extends superClass {
26+
@property({ attribute: false }) public hass!: HomeAssistant;
27+
28+
@property({ attribute: "is-wide", type: Boolean }) public isWide = false;
29+
30+
@property({ type: Boolean }) public narrow = false;
31+
32+
@property({ attribute: false }) public route!: Route;
33+
34+
@property({ attribute: false }) public entityId: string | null = null;
35+
36+
@state() protected _dirty = false;
37+
38+
@state() protected _errors?: string;
39+
40+
@state() protected _yamlErrors?: string;
41+
42+
@state() protected _entityId?: string;
43+
44+
@state() protected _mode: "gui" | "yaml" = "gui";
45+
46+
@state() protected _readOnly = false;
47+
48+
@state() protected _saving = false;
49+
50+
@state() protected _validationErrors?: (string | TemplateResult)[];
51+
52+
@state() protected _config?: TConfig;
53+
54+
@state() protected _blueprintConfig?: TConfig;
55+
56+
@state()
57+
@consume({ context: fullEntitiesContext, subscribe: true })
58+
@transform<EntityRegistryEntry[], EntityRegistryEntry>({
59+
transformer: function (this: { _entityId?: string }, value) {
60+
return value.find(({ entity_id }) => entity_id === this._entityId);
61+
},
62+
watch: ["_entityId"],
63+
})
64+
protected _registryEntry?: EntityRegistryEntry;
65+
66+
protected _entityRegistryUpdate?: EntityRegistryUpdate;
67+
68+
protected _entityRegCreated?: (
69+
value: PromiseLike<EntityRegistryEntry> | EntityRegistryEntry
70+
) => void;
71+
72+
protected _showSettings() {
73+
showMoreInfoDialog(this, {
74+
entityId: this._entityId!,
75+
view: "settings",
76+
});
77+
}
78+
79+
protected async _switchUiMode() {
80+
if (this._yamlErrors) {
81+
const result = await showConfirmationDialog(this, {
82+
text: html`${this.hass.localize(
83+
"ui.panel.config.automation.editor.switch_ui_yaml_error"
84+
)}<br /><br />${this._yamlErrors}`,
85+
confirmText: this.hass!.localize("ui.common.continue"),
86+
destructive: true,
87+
dismissText: this.hass!.localize("ui.common.cancel"),
88+
});
89+
if (!result) {
90+
return;
91+
}
92+
}
93+
this._yamlErrors = undefined;
94+
this._mode = "gui";
95+
}
96+
97+
protected _switchYamlMode() {
98+
this._mode = "yaml";
99+
}
100+
101+
protected _takeControlSave() {
102+
this._readOnly = false;
103+
this._dirty = true;
104+
this._blueprintConfig = undefined;
105+
}
106+
107+
protected _revertBlueprint() {
108+
this._config = this._blueprintConfig;
109+
if (this._mode === "yaml") {
110+
this.renderRoot.querySelector("ha-yaml-editor")?.setValue(this._config);
111+
}
112+
this._blueprintConfig = undefined;
113+
this._readOnly = false;
114+
}
115+
116+
protected _backTapped = async () => {
117+
const result = await this._confirmUnsavedChanged();
118+
if (result) {
119+
afterNextRender(() => goBack("/config"));
120+
}
121+
};
122+
123+
protected get isDirty() {
124+
return this._dirty;
125+
}
126+
127+
protected async promptDiscardChanges() {
128+
return this._confirmUnsavedChanged();
129+
}
130+
131+
// eslint-disable-next-line @typescript-eslint/class-literal-property-style
132+
protected _confirmUnsavedChanged(): Promise<boolean> {
133+
return Promise.resolve(true);
134+
}
135+
};

0 commit comments

Comments
 (0)