From f51bc402032081c3c5417a36b4c0a5db3dc5ea2b Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Sun, 17 Nov 2024 07:09:03 -0800 Subject: [PATCH] Catch yaml errors in script editor (#22853) --- src/panels/config/script/ha-script-editor.ts | 29 ++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/panels/config/script/ha-script-editor.ts b/src/panels/config/script/ha-script-editor.ts index a3eaaea6a4..88da99c7af 100644 --- a/src/panels/config/script/ha-script-editor.ts +++ b/src/panels/config/script/ha-script-editor.ts @@ -79,6 +79,8 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) { @state() private _errors?: string; + @state() private _yamlErrors?: string; + @state() private _entityId?: string; @state() private _mode: "gui" | "yaml" = "gui"; @@ -602,12 +604,14 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) { private _yamlChanged(ev: CustomEvent) { ev.stopPropagation(); + this._dirty = true; if (!ev.detail.isValid) { + this._yamlErrors = ev.detail.errorMsg; return; } + this._yamlErrors = undefined; this._config = ev.detail.value; this._errors = undefined; - this._dirty = true; } private async confirmUnsavedChanged(): Promise { @@ -723,7 +727,21 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) { history.back(); } - private _switchUiMode() { + private async _switchUiMode() { + if (this._yamlErrors) { + const result = await showConfirmationDialog(this, { + text: html`${this.hass.localize( + "ui.panel.config.automation.editor.switch_ui_yaml_error" + )}

${this._yamlErrors}`, + confirmText: this.hass!.localize("ui.common.continue"), + destructive: true, + dismissText: this.hass!.localize("ui.common.cancel"), + }); + if (!result) { + return; + } + } + this._yamlErrors = undefined; this._mode = "gui"; } @@ -763,6 +781,13 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) { } private async _saveScript(): Promise { + if (this._yamlErrors) { + showToast(this, { + message: this._yamlErrors, + }); + return; + } + if (!this.scriptId) { const saved = await this._promptScriptAlias(); if (!saved) {