From e92be566a00af39e6d4741fa528e3c34da7b9c1c Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Mon, 30 Sep 2024 09:28:17 +0200 Subject: [PATCH] Handle falsy value in ha-yaml-editor (object selector) (#22142) * Handle falsy value in ha-yaml-editor (object selector) * handle explicit `null` --- src/components/ha-service-control.ts | 3 ++- src/components/ha-yaml-editor.ts | 19 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/components/ha-service-control.ts b/src/components/ha-service-control.ts index 481160fd786a..6be50e0191d1 100644 --- a/src/components/ha-service-control.ts +++ b/src/components/ha-service-control.ts @@ -805,7 +805,8 @@ export class HaServiceControl extends LitElement { const value = ev.detail.value; if ( this._value?.data?.[key] === value || - (!this._value?.data?.[key] && (value === "" || value === undefined)) + ((!this._value?.data || !(key in this._value.data)) && + (value === "" || value === undefined)) ) { return; } diff --git a/src/components/ha-yaml-editor.ts b/src/components/ha-yaml-editor.ts index 1eb94c0aeab9..4782b4463702 100644 --- a/src/components/ha-yaml-editor.ts +++ b/src/components/ha-yaml-editor.ts @@ -18,7 +18,7 @@ import type { HaCodeEditor } from "./ha-code-editor"; import "./ha-button"; const isEmpty = (obj: Record): boolean => { - if (typeof obj !== "object") { + if (typeof obj !== "object" || obj === null) { return false; } for (const key in obj) { @@ -59,14 +59,13 @@ export class HaYamlEditor extends LitElement { public setValue(value): void { try { - this._yaml = - value && !isEmpty(value) - ? dump(value, { - schema: this.yamlSchema, - quotingType: '"', - noRefs: true, - }) - : ""; + this._yaml = !isEmpty(value) + ? dump(value, { + schema: this.yamlSchema, + quotingType: '"', + noRefs: true, + }) + : ""; } catch (err: any) { // eslint-disable-next-line no-console console.error(err, value); @@ -75,7 +74,7 @@ export class HaYamlEditor extends LitElement { } protected firstUpdated(): void { - if (this.defaultValue) { + if (this.defaultValue !== undefined) { this.setValue(this.defaultValue); } }