Skip to content

Commit

Permalink
Simplify feature
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Jun 25, 2024
1 parent b369e05 commit 643cc49
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 87 deletions.
66 changes: 2 additions & 64 deletions src/panels/config/automation/blueprint-automation-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@ import "@material/mwc-button/mwc-button";
import { HassEntity } from "home-assistant-js-websocket";
import { html, nothing } from "lit";
import { customElement, property } from "lit/decorators";
import { navigate } from "../../../common/navigate";
import { nextRender } from "../../../common/util/render-status";
import "../../../components/ha-alert";
import "../../../components/ha-markdown";
import {
BlueprintAutomationConfig,
normalizeAutomationConfig,
showAutomationEditor,
} from "../../../data/automation";
import { fetchBlueprints, substituteBlueprint } from "../../../data/blueprint";
import { showConfirmationDialog } from "../../lovelace/custom-card-helpers";
import { BlueprintAutomationConfig } from "../../../data/automation";
import { fetchBlueprints } from "../../../data/blueprint";
import { HaBlueprintGenericEditor } from "../blueprint/blueprint-generic-editor";
import "./manual-automation-editor";

Expand Down Expand Up @@ -73,61 +66,6 @@ export class HaBlueprintAutomationEditor extends HaBlueprintGenericEditor {
entity_id: this.stateObj.entity_id,
});
}

public async substituteBlueprint(): Promise<void> {
try {
const substitute = await substituteBlueprint(
this.hass,
"automation",
this.config.use_blueprint.path,
this.config.use_blueprint.input || {}
);

const config = normalizeAutomationConfig(substitute.substituted_config);

const convert = await showConfirmationDialog(this, {
title: "Take control of blueprint automation",
text: html`<manual-automation-editor
.hass=${this.hass}
disabled
.config=${config}
></manual-automation-editor>`,
confirmText: "Create automation",
});

if (convert) {
if (this.dirty) {
const confirmed = await showConfirmationDialog(this, {
title: "Unsaved changes",
text: "You have unsaved changes. Do you want to continue and lose these changes?",
confirmText: "Continue",
});
if (!confirmed) {
return;
}
}
while (
location.pathname === "/config/automation/edit/new" &&
history.length > 1
) {
history.back();
// eslint-disable-next-line no-await-in-loop
await nextRender();
}
if (location.pathname === "/config/automation/edit/new") {
navigate("/config/automation");
await nextRender();
}
showAutomationEditor({
alias: this.config.alias,
description: `${this.config.description ? this.config.description : this._blueprint && "metadata" in this._blueprint ? this._blueprint.metadata.description : ""}${this._blueprint && "metadata" in this._blueprint ? `(Originated from blueprint ${this._blueprint?.metadata.name})` : ""}`,
...substitute.substituted_config,
});
}
} catch (err: any) {
alert(`Failed to substitute blueprint: ${err.message}`);
}
}
}
declare global {
interface HTMLElementTagNameMap {
Expand Down
60 changes: 43 additions & 17 deletions src/panels/config/automation/ha-automation-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import "../../../components/ha-yaml-editor";
import {
AutomationConfig,
AutomationEntity,
BlueprintAutomationConfig,
deleteAutomation,
fetchAutomationFileConfig,
getAutomationEditorInitData,
Expand All @@ -67,6 +68,7 @@ import { showAutomationModeDialog } from "./automation-mode-dialog/show-dialog-a
import { showAutomationRenameDialog } from "./automation-rename-dialog/show-dialog-automation-rename";
import "./blueprint-automation-editor";
import "./manual-automation-editor";
import { substituteBlueprint } from "../../../data/blueprint";

declare global {
interface HTMLElementTagNameMap {
Expand Down Expand Up @@ -219,16 +221,21 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
></ha-svg-icon>
</ha-list-item>
`
: html`<ha-list-item
graphic="icon"
@click=${this._substituteBlueprint}
.disabled=${this._readOnly}
>
${this.hass.localize(
"ui.panel.config.automation.editor.subtitute_blueprint"
)}
<ha-svg-icon slot="graphic" .path=${mdiFileEdit}></ha-svg-icon>
</ha-list-item>`}
: html`
<ha-list-item
graphic="icon"
@click=${this._takeControl}
.disabled=${this._readOnly || this._mode === "yaml"}
>
${this.hass.localize(
"ui.panel.config.automation.editor.take_control"
)}
<ha-svg-icon
slot="graphic"
.path=${mdiFileEdit}
></ha-svg-icon>
</ha-list-item>
`}
<ha-list-item
.disabled=${!this._readOnly && !this.automationId}
Expand Down Expand Up @@ -342,7 +349,6 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
.stateObj=${stateObj}
.config=${this._config}
.disabled=${Boolean(this._readOnly)}
.dirty=${this._dirty}
@value-changed=${this._valueChanged}
@duplicate=${this._duplicate}
></blueprint-automation-editor>
Expand All @@ -355,8 +361,6 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
.stateObj=${stateObj}
.config=${this._config}
.disabled=${Boolean(this._readOnly)}
.readOnly=${Boolean(this._readOnly)}
.dirty=${this._dirty}
@value-changed=${this._valueChanged}
@duplicate=${this._duplicate}
></manual-automation-editor>
Expand Down Expand Up @@ -640,10 +644,32 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
}
};

private _substituteBlueprint() {
this.renderRoot
.querySelector("blueprint-automation-editor")
?.substituteBlueprint();
private async _takeControl() {
const config = this._config as BlueprintAutomationConfig;

const confirmation = await showConfirmationDialog(this, {
title: "Take control of automation?",
text: "This automation is using a blueprint. By taking control, you will be able to edit it directly. Are you sure you want to take control?",
});

if (!confirmation) return;

const result = await substituteBlueprint(
this.hass,
"automation",
config.use_blueprint.path,
config.use_blueprint.input || {}
);

const newConfig = {
alias: config.alias,
description: config.description,
...normalizeAutomationConfig(result.substituted_config),
};

this._config = newConfig;
this._dirty = true;
this._errors = undefined;
}

private async _duplicate() {
Expand Down
4 changes: 1 addition & 3 deletions src/panels/config/automation/manual-automation-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ export class HaManualAutomationEditor extends LitElement {

@property({ type: Boolean }) public disabled = false;

@property({ type: Boolean }) public readOnly = false;

@property({ attribute: false }) public config!: ManualAutomationConfig;

@property({ attribute: false }) public stateObj?: HassEntity;

protected render() {
return html`
${this.readOnly
${this.disabled
? html`<ha-alert alert-type="warning">
${this.hass.localize("ui.panel.config.automation.editor.read_only")}
<mwc-button slot="action" @click=${this._duplicate}>
Expand Down
2 changes: 0 additions & 2 deletions src/panels/config/blueprint/blueprint-generic-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export abstract class HaBlueprintGenericEditor extends LitElement {

@property({ type: Boolean }) public disabled = false;

@property({ type: Boolean }) public dirty = false;

@property({ type: Boolean, reflect: true }) public narrow = false;

@state() protected _blueprints?: Blueprints;
Expand Down
2 changes: 1 addition & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2759,7 +2759,7 @@
"unavailable": "Automation is unavailable",
"migrate": "Migrate",
"duplicate": "[%key:ui::common::duplicate%]",
"subtitute_blueprint": "Take control of automation",
"take_control": "Take control",
"run": "[%key:ui::panel::config::automation::editor::actions::run%]",
"rename": "[%key:ui::panel::config::automation::editor::triggers::rename%]",
"show_trace": "Traces",
Expand Down

0 comments on commit 643cc49

Please sign in to comment.