mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Add callback to SchemaFlowFormStep for suggested_values (#82706)
* Allow callback for suggested_values * docstring
This commit is contained in:
parent
20474e500c
commit
0a4549e202
@ -63,6 +63,16 @@ class SchemaFlowFormStep(SchemaFlowStep):
|
|||||||
- If `next_step` is None, the flow is ended with `FlowResultType.CREATE_ENTRY`.
|
- If `next_step` is None, the flow is ended with `FlowResultType.CREATE_ENTRY`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
suggested_values: Callable[[SchemaCommonFlowHandler], dict[str, Any]] | None = None
|
||||||
|
"""Optional property to populate suggested values.
|
||||||
|
|
||||||
|
- If `suggested_values` is None, each key in the schema will get a suggested value
|
||||||
|
from an option with the same key.
|
||||||
|
|
||||||
|
Note: if a step is retried due to a validation failure, then the user input will have
|
||||||
|
priority over the suggested values.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class SchemaFlowMenuStep(SchemaFlowStep):
|
class SchemaFlowMenuStep(SchemaFlowStep):
|
||||||
@ -167,10 +177,6 @@ class SchemaCommonFlowHandler:
|
|||||||
user_input: dict[str, Any] | None = None,
|
user_input: dict[str, Any] | None = None,
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Show form for next step."""
|
"""Show form for next step."""
|
||||||
suggested_values = dict(self._options)
|
|
||||||
if user_input:
|
|
||||||
suggested_values.update(user_input)
|
|
||||||
|
|
||||||
if isinstance(self._flow[next_step_id], SchemaFlowMenuStep):
|
if isinstance(self._flow[next_step_id], SchemaFlowMenuStep):
|
||||||
menu_step = cast(SchemaFlowMenuStep, self._flow[next_step_id])
|
menu_step = cast(SchemaFlowMenuStep, self._flow[next_step_id])
|
||||||
return self._handler.async_show_menu(
|
return self._handler.async_show_menu(
|
||||||
@ -183,6 +189,15 @@ class SchemaCommonFlowHandler:
|
|||||||
if (data_schema := self._get_schema(form_step)) is None:
|
if (data_schema := self._get_schema(form_step)) is None:
|
||||||
return self._show_next_step_or_create_entry(form_step)
|
return self._show_next_step_or_create_entry(form_step)
|
||||||
|
|
||||||
|
if form_step.suggested_values:
|
||||||
|
suggested_values = form_step.suggested_values(self)
|
||||||
|
else:
|
||||||
|
suggested_values = self._options
|
||||||
|
if user_input:
|
||||||
|
# We don't want to mutate the existing options
|
||||||
|
suggested_values = copy.deepcopy(suggested_values)
|
||||||
|
suggested_values.update(user_input)
|
||||||
|
|
||||||
if data_schema.schema:
|
if data_schema.schema:
|
||||||
# Make a copy of the schema with suggested values set to saved options
|
# Make a copy of the schema with suggested values set to saved options
|
||||||
schema = {}
|
schema = {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user