From 1687d90d02bd6e8c7e095d52df9700b27fb692b1 Mon Sep 17 00:00:00 2001 From: On Freund Date: Fri, 1 May 2020 11:36:51 +0300 Subject: [PATCH] Allow passing the current value to config flow input fields (#5603) * Allow passing the current value to config flow input fields * Fix lint errors * Apply suggestions from code review Co-Authored-By: Bram Kragten * Rename current_value to suggested_value to open up more use cases * Update ha-form-integer.ts Co-authored-by: Bram Kragten --- src/components/ha-form/ha-form-integer.ts | 7 ++++++- src/components/ha-form/ha-form.ts | 2 +- src/dialogs/config-flow/step-flow-form.ts | 4 +++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/components/ha-form/ha-form-integer.ts b/src/components/ha-form/ha-form-integer.ts index 9fe0929b84..0e5674edfb 100644 --- a/src/components/ha-form/ha-form-integer.ts +++ b/src/components/ha-form/ha-form-integer.ts @@ -77,7 +77,12 @@ export class HaFormInteger extends LitElement implements HaFormElement { } private get _value() { - return this.data || this.schema.default || 0; + return ( + this.data || + this.schema.description?.suggested_value || + this.schema.default || + 0 + ); } private _handleCheckboxChange(ev: Event) { diff --git a/src/components/ha-form/ha-form.ts b/src/components/ha-form/ha-form.ts index ff452c6590..be6b4eb1da 100644 --- a/src/components/ha-form/ha-form.ts +++ b/src/components/ha-form/ha-form.ts @@ -30,7 +30,7 @@ export interface HaFormBaseSchema { default?: HaFormData; required?: boolean; optional?: boolean; - description?: { suffix?: string }; + description?: { suffix?: string; suggested_value?: HaFormData }; } export interface HaFormIntegerSchema extends HaFormBaseSchema { diff --git a/src/dialogs/config-flow/step-flow-form.ts b/src/dialogs/config-flow/step-flow-form.ts index 5fe6c98cd5..591f3d7135 100644 --- a/src/dialogs/config-flow/step-flow-form.ts +++ b/src/dialogs/config-flow/step-flow-form.ts @@ -122,7 +122,9 @@ class StepFlowForm extends LitElement { const data = {}; this.step.data_schema.forEach((field) => { - if ("default" in field) { + if (field.description?.suggested_value) { + data[field.name] = field.description.suggested_value; + } else if ("default" in field) { data[field.name] = field.default; } });