From c11bbcf442c88e577841f3b442f180a038037a24 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Thu, 1 Apr 2021 23:37:46 +0200 Subject: [PATCH] Add blueprint config to trace (#8751) Co-authored-by: Paulus Schoutsen --- src/data/automation.ts | 3 + src/data/trace.ts | 6 +- .../config/automation/ha-automation-editor.ts | 65 +++++++++---------- .../ha-automation-trace-blueprint-config.ts | 34 ++++++++++ .../automation/trace/ha-automation-trace.ts | 24 ++++++- 5 files changed, 97 insertions(+), 35 deletions(-) create mode 100644 src/panels/config/automation/trace/ha-automation-trace-blueprint-config.ts diff --git a/src/data/automation.ts b/src/data/automation.ts index acf8e04fa2..d137f76600 100644 --- a/src/data/automation.ts +++ b/src/data/automation.ts @@ -238,6 +238,9 @@ export const deleteAutomation = (hass: HomeAssistant, id: string) => let inititialAutomationEditorData: Partial | undefined; +export const getAutomationConfig = (hass: HomeAssistant, id: string) => + hass.callApi("GET", `config/automation/config/${id}`); + export const showAutomationEditor = ( el: HTMLElement, data?: Partial diff --git a/src/data/trace.ts b/src/data/trace.ts index 6953353b87..abe9d06830 100644 --- a/src/data/trace.ts +++ b/src/data/trace.ts @@ -1,6 +1,9 @@ import { strStartsWith } from "../common/string/starts-with"; import { HomeAssistant, Context } from "../types"; -import { ManualAutomationConfig } from "./automation"; +import { + BlueprintAutomationConfig, + ManualAutomationConfig, +} from "./automation"; interface BaseTraceStep { path: string; @@ -87,6 +90,7 @@ export interface AutomationTraceExtended extends AutomationTrace { trace: Record; context: Context; config: ManualAutomationConfig; + blueprint_inputs?: BlueprintAutomationConfig; error?: string; } diff --git a/src/panels/config/automation/ha-automation-editor.ts b/src/panels/config/automation/ha-automation-editor.ts index a7af6b6879..19c73ac31b 100644 --- a/src/panels/config/automation/ha-automation-editor.ts +++ b/src/panels/config/automation/ha-automation-editor.ts @@ -36,6 +36,7 @@ import { AutomationConfig, AutomationEntity, deleteAutomation, + getAutomationConfig, getAutomationEditorInitData, showAutomationEditor, triggerAutomationActions, @@ -303,39 +304,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) { oldAutomationId !== this.automationId ) { this._setEntityId(); - this.hass - .callApi( - "GET", - `config/automation/config/${this.automationId}` - ) - .then( - (config) => { - // Normalize data: ensure trigger, action and condition are lists - // Happens when people copy paste their automations into the config - for (const key of ["trigger", "condition", "action"]) { - const value = config[key]; - if (value && !Array.isArray(value)) { - config[key] = [value]; - } - } - this._dirty = false; - this._config = config; - }, - (resp) => { - showAlertDialog(this, { - text: - resp.status_code === 404 - ? this.hass.localize( - "ui.panel.config.automation.editor.load_error_not_editable" - ) - : this.hass.localize( - "ui.panel.config.automation.editor.load_error_unknown", - "err_no", - resp.status_code - ), - }).then(() => history.back()); - } - ); + this._loadConfig(); } if (changedProps.has("automationId") && !this.automationId && this.hass) { @@ -378,6 +347,36 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) { this._entityId = automation?.entity_id; } + private async _loadConfig() { + try { + const config = await getAutomationConfig(this.hass, this.automationId); + + // Normalize data: ensure trigger, action and condition are lists + // Happens when people copy paste their automations into the config + for (const key of ["trigger", "condition", "action"]) { + const value = config[key]; + if (value && !Array.isArray(value)) { + config[key] = [value]; + } + } + this._dirty = false; + this._config = config; + } catch (err) { + showAlertDialog(this, { + text: + err.status_code === 404 + ? this.hass.localize( + "ui.panel.config.automation.editor.load_error_not_editable" + ) + : this.hass.localize( + "ui.panel.config.automation.editor.load_error_unknown", + "err_no", + err.status_code + ), + }).then(() => history.back()); + } + } + private _valueChanged(ev: CustomEvent<{ value: AutomationConfig }>) { ev.stopPropagation(); this._config = ev.detail.value; diff --git a/src/panels/config/automation/trace/ha-automation-trace-blueprint-config.ts b/src/panels/config/automation/trace/ha-automation-trace-blueprint-config.ts new file mode 100644 index 0000000000..4f9d02403e --- /dev/null +++ b/src/panels/config/automation/trace/ha-automation-trace-blueprint-config.ts @@ -0,0 +1,34 @@ +import { safeDump } from "js-yaml"; +import { + customElement, + html, + LitElement, + property, + TemplateResult, +} from "lit-element"; +import "../../../../components/ha-icon-button"; +import "../../../../components/ha-code-editor"; +import { HomeAssistant } from "../../../../types"; +import { AutomationTraceExtended } from "../../../../data/trace"; + +@customElement("ha-automation-trace-blueprint-config") +export class HaAutomationTraceBlueprintConfig extends LitElement { + @property({ attribute: false }) public hass!: HomeAssistant; + + @property() public trace!: AutomationTraceExtended; + + protected render(): TemplateResult { + return html` + + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "ha-automation-trace-blueprint-config": HaAutomationTraceBlueprintConfig; + } +} diff --git a/src/panels/config/automation/trace/ha-automation-trace.ts b/src/panels/config/automation/trace/ha-automation-trace.ts index 13f9f51c2e..0d712fb5f7 100644 --- a/src/panels/config/automation/trace/ha-automation-trace.ts +++ b/src/panels/config/automation/trace/ha-automation-trace.ts @@ -39,6 +39,7 @@ import { mdiRefresh, mdiDownload, } from "@mdi/js"; +import "./ha-automation-trace-blueprint-config"; @customElement("ha-automation-trace") export class HaAutomationTrace extends LitElement { @@ -70,7 +71,8 @@ export class HaAutomationTrace extends LitElement { | "details" | "config" | "timeline" - | "logbook" = "details"; + | "logbook" + | "blueprint" = "details"; protected render(): TemplateResult { const stateObj = this._entityId @@ -197,6 +199,19 @@ export class HaAutomationTrace extends LitElement { ` )} + ${this._trace.blueprint_inputs + ? html` +
+ Blueprint Config +
+ ` + : ""} ${this._selected === undefined || this._logbookEntries === undefined || @@ -227,6 +242,13 @@ export class HaAutomationTrace extends LitElement { .entries=${this._logbookEntries} > ` + : this._view === "blueprint" + ? html` + + ` : html`