Skip to content

Commit

Permalink
Show YAML parsing errors in automation editor (#22753)
Browse files Browse the repository at this point in the history
* Show YAML parsing errors in automation editor

* make dirty on error

* formatting
  • Loading branch information
karwosts authored Nov 11, 2024
1 parent 52a91d8 commit d763a01
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/components/ha-yaml-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,15 @@ export class HaYamlEditor extends LitElement {
this._yaml = ev.detail.value;
let parsed;
let isValid = true;
let errorMsg;

if (this._yaml) {
try {
parsed = load(this._yaml, { schema: this.yamlSchema });
} 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 = {};
Expand All @@ -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() {
Expand Down
14 changes: 13 additions & 1 deletion src/panels/config/automation/ha-automation-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<boolean> {
Expand Down Expand Up @@ -753,6 +757,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
}

private _switchUiMode() {
this._yamlErrors = undefined;
this._mode = "gui";
}

Expand Down Expand Up @@ -792,6 +797,13 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
}

private async _saveAutomation(): Promise<void> {
if (this._yamlErrors) {
showToast(this, {
message: this._yamlErrors,
});
return;
}

const id = this.automationId || String(Date.now());
if (!this.automationId) {
const saved = await this._promptAutomationAlias();
Expand Down
4 changes: 3 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d763a01

Please sign in to comment.