Allow next_step to be a string (#82647)
* Allow next_step to be a string * Adjust docstrings * Add test
This commit is contained in:
parent
285aff154d
commit
caa99ea9fb
3 changed files with 25 additions and 13 deletions
|
@ -134,7 +134,7 @@ DATA_SCHEMA_SENSOR = vol.Schema(SENSOR_SETUP)
|
||||||
CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = {
|
CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = {
|
||||||
"user": SchemaFlowFormStep(
|
"user": SchemaFlowFormStep(
|
||||||
schema=DATA_SCHEMA_RESOURCE,
|
schema=DATA_SCHEMA_RESOURCE,
|
||||||
next_step=lambda _: "sensor",
|
next_step="sensor",
|
||||||
validate_user_input=validate_rest_setup,
|
validate_user_input=validate_rest_setup,
|
||||||
),
|
),
|
||||||
"sensor": SchemaFlowFormStep(
|
"sensor": SchemaFlowFormStep(
|
||||||
|
|
|
@ -32,18 +32,22 @@ class SchemaFlowFormStep:
|
||||||
vol.Schema | None,
|
vol.Schema | None,
|
||||||
] | None
|
] | None
|
||||||
|
|
||||||
# Optional function to validate user input.
|
|
||||||
# The validate_user_input function is called if the schema validates successfully.
|
|
||||||
# The validate_user_input function is passed the user input from the current step.
|
|
||||||
# The validate_user_input should raise SchemaFlowError is user input is invalid.
|
|
||||||
validate_user_input: Callable[[dict[str, Any]], dict[str, Any]] = lambda x: x
|
validate_user_input: Callable[[dict[str, Any]], dict[str, Any]] = lambda x: x
|
||||||
|
"""Optional function to validate user input.
|
||||||
|
|
||||||
# Optional function to identify next step.
|
- The `validate_user_input` function is called if the schema validates successfully.
|
||||||
# The next_step function is called if the schema validates successfully or if no
|
- The `validate_user_input` function is passed the user input from the current step.
|
||||||
# schema is defined. The next_step function is passed the union of config entry
|
- The `validate_user_input` should raise `SchemaFlowError` is user input is invalid.
|
||||||
# options and user input from previous steps.
|
"""
|
||||||
# If next_step is None, the flow is ended with FlowResultType.CREATE_ENTRY.
|
|
||||||
next_step: Callable[[dict[str, Any]], str] | None = None
|
next_step: Callable[[dict[str, Any]], str] | str | None = None
|
||||||
|
"""Optional property to identify next step.
|
||||||
|
|
||||||
|
- If `next_step` is a function, it is called if the schema validates successfully or
|
||||||
|
if no schema is defined. The `next_step` function is passed the union of config entry
|
||||||
|
options and user input from previous steps.
|
||||||
|
- If `next_step` is None, the flow is ended with `FlowResultType.CREATE_ENTRY`.
|
||||||
|
"""
|
||||||
|
|
||||||
# Optional function to allow amending a form schema.
|
# Optional function to allow amending a form schema.
|
||||||
# The update_form_schema function is called before async_show_form is called. The
|
# The update_form_schema function is called before async_show_form is called. The
|
||||||
|
@ -140,6 +144,9 @@ class SchemaCommonFlowHandler:
|
||||||
# Flow done, create entry or update config entry options
|
# Flow done, create entry or update config entry options
|
||||||
return self._handler.async_create_entry(data=self._options)
|
return self._handler.async_create_entry(data=self._options)
|
||||||
|
|
||||||
|
if isinstance(form_step.next_step, str):
|
||||||
|
next_step_id = form_step.next_step
|
||||||
|
else:
|
||||||
next_step_id = form_step.next_step(self._options)
|
next_step_id = form_step.next_step(self._options)
|
||||||
|
|
||||||
return self._show_next_step(next_step_id)
|
return self._show_next_step(next_step_id)
|
||||||
|
|
|
@ -29,7 +29,8 @@ async def test_menu_step(hass: HomeAssistant) -> None:
|
||||||
"user": SchemaFlowMenuStep(MENU_1),
|
"user": SchemaFlowMenuStep(MENU_1),
|
||||||
"option1": SchemaFlowFormStep(vol.Schema({}), next_step=lambda _: "menu2"),
|
"option1": SchemaFlowFormStep(vol.Schema({}), next_step=lambda _: "menu2"),
|
||||||
"menu2": SchemaFlowMenuStep(MENU_2),
|
"menu2": SchemaFlowMenuStep(MENU_2),
|
||||||
"option3": SchemaFlowFormStep(vol.Schema({})),
|
"option3": SchemaFlowFormStep(vol.Schema({}), next_step="option4"),
|
||||||
|
"option4": SchemaFlowFormStep(vol.Schema({})),
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestConfigFlow(SchemaConfigFlowHandler, domain=TEST_DOMAIN):
|
class TestConfigFlow(SchemaConfigFlowHandler, domain=TEST_DOMAIN):
|
||||||
|
@ -63,5 +64,9 @@ async def test_menu_step(hass: HomeAssistant) -> None:
|
||||||
assert result["type"] == FlowResultType.FORM
|
assert result["type"] == FlowResultType.FORM
|
||||||
assert result["step_id"] == "option3"
|
assert result["step_id"] == "option3"
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||||
|
assert result["type"] == FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "option4"
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||||
|
|
Loading…
Add table
Reference in a new issue