From 724a79a8e89e6288ac9d40bd0b5ea905aed716c2 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 29 Nov 2022 10:16:01 +0100 Subject: [PATCH] Add suggested_value helper for FlowHandler (#82491) * Add suggested_value helper for ConfigFlow * Move helper function to FlowHandler * Rename and adjust docstring * Fix rebase --- homeassistant/data_entry_flow.py | 29 +++++++++++++++++++ .../helpers/schema_config_entry_flow.py | 22 ++------------ 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/homeassistant/data_entry_flow.py b/homeassistant/data_entry_flow.py index 629258e01d1..866f1a5db2f 100644 --- a/homeassistant/data_entry_flow.py +++ b/homeassistant/data_entry_flow.py @@ -4,6 +4,7 @@ from __future__ import annotations import abc import asyncio from collections.abc import Iterable, Mapping +import copy from dataclasses import dataclass import logging from types import MappingProxyType @@ -443,6 +444,34 @@ class FlowHandler: """If we should show advanced options.""" return self.context.get("show_advanced_options", False) + def add_suggested_values_to_schema( + self, data_schema: vol.Schema, suggested_values: Mapping[str, Any] + ) -> vol.Schema: + """Make a copy of the schema, populated with suggested values. + + For each schema marker matching items in `suggested_values`, + the `suggested_value` will be set. The existing `suggested_value` will + be left untouched if there is no matching item. + """ + schema = {} + for key, val in data_schema.schema.items(): + if isinstance(key, vol.Marker): + # Exclude advanced field + if ( + key.description + and key.description.get("advanced") + and not self.show_advanced_options + ): + continue + + new_key = key + if key in suggested_values and isinstance(key, vol.Marker): + # Copy the marker to not modify the flow schema + new_key = copy.copy(key) + new_key.description = {"suggested_value": suggested_values[key]} + schema[new_key] = val + return vol.Schema(schema) + @callback def async_show_form( self, diff --git a/homeassistant/helpers/schema_config_entry_flow.py b/homeassistant/helpers/schema_config_entry_flow.py index 747915c71e2..8d72910b6fe 100644 --- a/homeassistant/helpers/schema_config_entry_flow.py +++ b/homeassistant/helpers/schema_config_entry_flow.py @@ -210,25 +210,9 @@ class SchemaCommonFlowHandler: if data_schema.schema: # Make a copy of the schema with suggested values set to saved options - schema = {} - for key, val in data_schema.schema.items(): - - if isinstance(key, vol.Marker): - # Exclude advanced field - if ( - key.description - and key.description.get("advanced") - and not self._handler.show_advanced_options - ): - continue - - new_key = key - if key in suggested_values and isinstance(key, vol.Marker): - # Copy the marker to not modify the flow schema - new_key = copy.copy(key) - new_key.description = {"suggested_value": suggested_values[key]} - schema[new_key] = val - data_schema = vol.Schema(schema) + data_schema = self._handler.add_suggested_values_to_schema( + data_schema, suggested_values + ) errors = {"base": str(error)} if error else None