diff --git a/src/components/ha-yaml-editor.ts b/src/components/ha-yaml-editor.ts index a08a1f49a791..ee16a8c8a85b 100644 --- a/src/components/ha-yaml-editor.ts +++ b/src/components/ha-yaml-editor.ts @@ -130,6 +130,7 @@ export class HaYamlEditor extends LitElement { this._yaml = ev.detail.value; let parsed; let isValid = true; + let errorMsg; if (this._yaml) { try { @@ -137,6 +138,7 @@ export class HaYamlEditor extends LitElement { } catch (err: any) { // Invalid YAML isValid = false; + errorMsg = `${this.hass.localize("ui.components.yaml-editor.error", { reason: err.reason })}${err.mark ? ` (${this.hass.localize("ui.components.yaml-editor.error_location", { line: err.mark.line + 1, column: err.mark.column + 1 })})` : ""}`; } } else { parsed = {}; @@ -145,7 +147,11 @@ export class HaYamlEditor extends LitElement { this.value = parsed; this.isValid = isValid; - fireEvent(this, "value-changed", { value: parsed, isValid } as any); + fireEvent(this, "value-changed", { + value: parsed, + isValid, + errorMsg, + } as any); } get yaml() { diff --git a/src/panels/config/automation/ha-automation-editor.ts b/src/panels/config/automation/ha-automation-editor.ts index 37c9f8b77b1a..62f0f127a565 100644 --- a/src/panels/config/automation/ha-automation-editor.ts +++ b/src/panels/config/automation/ha-automation-editor.ts @@ -103,6 +103,8 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) { @state() private _errors?: string; + @state() private _yamlErrors?: string; + @state() private _entityId?: string; @state() private _mode: "gui" | "yaml" = "gui"; @@ -629,15 +631,17 @@ export class HaAutomationEditor 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 = { id: this._config?.id, ...normalizeAutomationConfig(ev.detail.value), }; this._errors = undefined; - this._dirty = true; } private async confirmUnsavedChanged(): Promise { @@ -753,6 +757,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) { } private _switchUiMode() { + this._yamlErrors = undefined; this._mode = "gui"; } @@ -792,6 +797,13 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) { } private async _saveAutomation(): Promise { + if (this._yamlErrors) { + showToast(this, { + message: this._yamlErrors, + }); + return; + } + const id = this.automationId || String(Date.now()); if (!this.automationId) { const saved = await this._promptAutomationAlias(); diff --git a/src/translations/en.json b/src/translations/en.json index 08efd2ec1acf..230b82a49097 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -1089,7 +1089,9 @@ "temperature_down": "Decrease temperature" }, "yaml-editor": { - "copy_to_clipboard": "[%key:ui::panel::config::automation::editor::copy_to_clipboard%]" + "copy_to_clipboard": "[%key:ui::panel::config::automation::editor::copy_to_clipboard%]", + "error": "Error in parsing YAML: {reason}", + "error_location": "line: {line}, column: {column}" }, "state-content-picker": { "state": "State",