Show YAML parsing errors in automation editor (#22753)

* Show YAML parsing errors in automation editor

* make dirty on error

* formatting
This commit is contained in:
karwosts 2024-11-11 07:40:20 -08:00 committed by GitHub
parent 52a91d8403
commit d763a014ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 3 deletions

View File

@ -130,6 +130,7 @@ export class HaYamlEditor extends LitElement {
this._yaml = ev.detail.value; this._yaml = ev.detail.value;
let parsed; let parsed;
let isValid = true; let isValid = true;
let errorMsg;
if (this._yaml) { if (this._yaml) {
try { try {
@ -137,6 +138,7 @@ export class HaYamlEditor extends LitElement {
} catch (err: any) { } catch (err: any) {
// Invalid YAML // Invalid YAML
isValid = false; 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 { } else {
parsed = {}; parsed = {};
@ -145,7 +147,11 @@ export class HaYamlEditor extends LitElement {
this.value = parsed; this.value = parsed;
this.isValid = isValid; this.isValid = isValid;
fireEvent(this, "value-changed", { value: parsed, isValid } as any); fireEvent(this, "value-changed", {
value: parsed,
isValid,
errorMsg,
} as any);
} }
get yaml() { get yaml() {

View File

@ -103,6 +103,8 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
@state() private _errors?: string; @state() private _errors?: string;
@state() private _yamlErrors?: string;
@state() private _entityId?: string; @state() private _entityId?: string;
@state() private _mode: "gui" | "yaml" = "gui"; @state() private _mode: "gui" | "yaml" = "gui";
@ -629,15 +631,17 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
private _yamlChanged(ev: CustomEvent) { private _yamlChanged(ev: CustomEvent) {
ev.stopPropagation(); ev.stopPropagation();
this._dirty = true;
if (!ev.detail.isValid) { if (!ev.detail.isValid) {
this._yamlErrors = ev.detail.errorMsg;
return; return;
} }
this._yamlErrors = undefined;
this._config = { this._config = {
id: this._config?.id, id: this._config?.id,
...normalizeAutomationConfig(ev.detail.value), ...normalizeAutomationConfig(ev.detail.value),
}; };
this._errors = undefined; this._errors = undefined;
this._dirty = true;
} }
private async confirmUnsavedChanged(): Promise<boolean> { private async confirmUnsavedChanged(): Promise<boolean> {
@ -753,6 +757,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
} }
private _switchUiMode() { private _switchUiMode() {
this._yamlErrors = undefined;
this._mode = "gui"; this._mode = "gui";
} }
@ -792,6 +797,13 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
} }
private async _saveAutomation(): Promise<void> { private async _saveAutomation(): Promise<void> {
if (this._yamlErrors) {
showToast(this, {
message: this._yamlErrors,
});
return;
}
const id = this.automationId || String(Date.now()); const id = this.automationId || String(Date.now());
if (!this.automationId) { if (!this.automationId) {
const saved = await this._promptAutomationAlias(); const saved = await this._promptAutomationAlias();

View File

@ -1089,7 +1089,9 @@
"temperature_down": "Decrease temperature" "temperature_down": "Decrease temperature"
}, },
"yaml-editor": { "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-content-picker": {
"state": "State", "state": "State",