From e77875179b1506e6baceaf4e150d7ec08cf42ab7 Mon Sep 17 00:00:00 2001 From: Zack Date: Tue, 22 Mar 2022 15:35:16 -0500 Subject: [PATCH 1/2] Fix Duration Default --- .../ha-selector/ha-selector-duration.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/components/ha-selector/ha-selector-duration.ts b/src/components/ha-selector/ha-selector-duration.ts index 1471750d90d3..520afa975679 100644 --- a/src/components/ha-selector/ha-selector-duration.ts +++ b/src/components/ha-selector/ha-selector-duration.ts @@ -1,8 +1,9 @@ -import "../ha-duration-input"; -import { html, LitElement } from "lit"; +import { html, LitElement, PropertyValues } from "lit"; import { customElement, property } from "lit/decorators"; +import { fireEvent } from "../../common/dom/fire_event"; import { DurationSelector } from "../../data/selector"; import { HomeAssistant } from "../../types"; +import "../ha-duration-input"; @customElement("ha-selector-duration") export class HaTimeDuration extends LitElement { @@ -18,6 +19,19 @@ export class HaTimeDuration extends LitElement { @property({ type: Boolean }) public required = true; + protected firstUpdated(changedProps: PropertyValues): void { + super.firstUpdated(changedProps); + if (!changedProps.has("selector")) { + return; + } + // Set the initial value via event so HA Form is aware + if (["", undefined].includes(this.value)) { + fireEvent(this, "value-changed", { + value: { hours: 0, minutes: 0, seconds: 0 }, + }); + } + } + protected render() { return html` Date: Tue, 22 Mar 2022 18:22:23 -0500 Subject: [PATCH 2/2] USe initial form data function --- .../ha-form/compute-initial-ha-form-data.ts | 22 ++++++++++++++++++- .../ha-selector/ha-selector-duration.ts | 20 +++-------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/components/ha-form/compute-initial-ha-form-data.ts b/src/components/ha-form/compute-initial-ha-form-data.ts index 0e80433c740b..7c5d728d91cd 100644 --- a/src/components/ha-form/compute-initial-ha-form-data.ts +++ b/src/components/ha-form/compute-initial-ha-form-data.ts @@ -1,4 +1,5 @@ -import { HaFormSchema } from "./types"; +import type { Selector } from "../../data/selector"; +import type { HaFormSchema } from "./types"; export const computeInitialHaFormData = ( schema: HaFormSchema[] @@ -31,6 +32,25 @@ export const computeInitialHaFormData = ( minutes: 0, seconds: 0, }; + } else if ("selector" in field) { + const selector: Selector = field.selector; + if ("boolean" in selector) { + data[field.name] = false; + } else if ("text" in selector) { + data[field.name] = ""; + } else if ("number" in selector) { + data[field.name] = "min" in selector.number ? selector.number.min : 0; + } else if ("select" in selector) { + if (selector.select.options.length) { + data[field.name] = selector.select.options[0][0]; + } + } else if ("duration" in selector) { + data[field.name] = { + hours: 0, + minutes: 0, + seconds: 0, + }; + } } }); return data; diff --git a/src/components/ha-selector/ha-selector-duration.ts b/src/components/ha-selector/ha-selector-duration.ts index 520afa975679..1f5e88b1460e 100644 --- a/src/components/ha-selector/ha-selector-duration.ts +++ b/src/components/ha-selector/ha-selector-duration.ts @@ -1,8 +1,7 @@ -import { html, LitElement, PropertyValues } from "lit"; +import { html, LitElement } from "lit"; import { customElement, property } from "lit/decorators"; -import { fireEvent } from "../../common/dom/fire_event"; -import { DurationSelector } from "../../data/selector"; -import { HomeAssistant } from "../../types"; +import type { DurationSelector } from "../../data/selector"; +import type { HomeAssistant } from "../../types"; import "../ha-duration-input"; @customElement("ha-selector-duration") @@ -19,19 +18,6 @@ export class HaTimeDuration extends LitElement { @property({ type: Boolean }) public required = true; - protected firstUpdated(changedProps: PropertyValues): void { - super.firstUpdated(changedProps); - if (!changedProps.has("selector")) { - return; - } - // Set the initial value via event so HA Form is aware - if (["", undefined].includes(this.value)) { - fireEvent(this, "value-changed", { - value: { hours: 0, minutes: 0, seconds: 0 }, - }); - } - } - protected render() { return html`