From 643cc491d51db83cb2eaaa4729125fbbc36fd879 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Tue, 25 Jun 2024 18:21:14 +0200 Subject: [PATCH] Simplify feature --- .../automation/blueprint-automation-editor.ts | 66 +------------------ .../config/automation/ha-automation-editor.ts | 60 ++++++++++++----- .../automation/manual-automation-editor.ts | 4 +- .../blueprint/blueprint-generic-editor.ts | 2 - src/translations/en.json | 2 +- 5 files changed, 47 insertions(+), 87 deletions(-) diff --git a/src/panels/config/automation/blueprint-automation-editor.ts b/src/panels/config/automation/blueprint-automation-editor.ts index 799fdc1c5def..90fe16a13070 100644 --- a/src/panels/config/automation/blueprint-automation-editor.ts +++ b/src/panels/config/automation/blueprint-automation-editor.ts @@ -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"; @@ -73,61 +66,6 @@ export class HaBlueprintAutomationEditor extends HaBlueprintGenericEditor { entity_id: this.stateObj.entity_id, }); } - - public async substituteBlueprint(): Promise { - 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``, - 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 { diff --git a/src/panels/config/automation/ha-automation-editor.ts b/src/panels/config/automation/ha-automation-editor.ts index 30dccd80551c..f2d39862c6b3 100644 --- a/src/panels/config/automation/ha-automation-editor.ts +++ b/src/panels/config/automation/ha-automation-editor.ts @@ -41,6 +41,7 @@ import "../../../components/ha-yaml-editor"; import { AutomationConfig, AutomationEntity, + BlueprintAutomationConfig, deleteAutomation, fetchAutomationFileConfig, getAutomationEditorInitData, @@ -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 { @@ -219,16 +221,21 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) { > ` - : html` - ${this.hass.localize( - "ui.panel.config.automation.editor.subtitute_blueprint" - )} - - `} + : html` + + ${this.hass.localize( + "ui.panel.config.automation.editor.take_control" + )} + + + `} @@ -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} > @@ -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() { diff --git a/src/panels/config/automation/manual-automation-editor.ts b/src/panels/config/automation/manual-automation-editor.ts index fcfdb0d74a44..ceb4da52e033 100644 --- a/src/panels/config/automation/manual-automation-editor.ts +++ b/src/panels/config/automation/manual-automation-editor.ts @@ -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` ${this.hass.localize("ui.panel.config.automation.editor.read_only")} diff --git a/src/panels/config/blueprint/blueprint-generic-editor.ts b/src/panels/config/blueprint/blueprint-generic-editor.ts index 640f4727866b..bae63f6dc7e3 100644 --- a/src/panels/config/blueprint/blueprint-generic-editor.ts +++ b/src/panels/config/blueprint/blueprint-generic-editor.ts @@ -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; diff --git a/src/translations/en.json b/src/translations/en.json index 9a5f4d015247..8b1b3fd453a5 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -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",