Skip to content

Commit bec2b74

Browse files
gjohansson-STjoostlekabmantis
authored
Docs for SchemaConfigFlowHandler (#2801)
Co-authored-by: Joost Lekkerkerker <[email protected]> Co-authored-by: Abílio Costa <[email protected]>
1 parent 04f1605 commit bec2b74

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

docs/config_entries_config_flow_handler.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,75 @@ class ExampleFlow(ConfigFlow):
522522
)
523523
```
524524

525+
## Use SchemaConfigFlowHandler for simple flows
526+
527+
For helpers and integrations with simple config flows, you can use the `SchemaConfigFlowHandler` instead.
528+
529+
Compared to using a full config flow, the `SchemaConfigFlowHandler` comes with certain limitations and needs to be considered:
530+
531+
- All user input is saved in the `options` dictionary of the resulting config entry. Therefore it's not suitable to use in integrations which uses connection data, api key's or other information that should be stored in the config entry `data`.
532+
- It may be simpler to use the normal config flow handler if you have extensive validation, setting unique id or checking for duplicated config entries.
533+
- Starting the flow with other steps besides `user` and `import` is discouraged.
534+
535+
```python
536+
537+
from homeassistant.helpers.schema_config_entry_flow import (
538+
SchemaCommonFlowHandler,
539+
SchemaConfigFlowHandler,
540+
SchemaFlowError,
541+
SchemaFlowFormStep,
542+
)
543+
544+
async def validate_setup(
545+
handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
546+
) -> dict[str, Any]:
547+
"""Validate options."""
548+
if user_input[CONF_SOME_SETTING] == "error":
549+
# 'setup_error' needs to be existing in string.json config errors section
550+
raise SchemaFlowError("setup_error")
551+
return user_input
552+
553+
DATA_SCHEMA_SETUP = vol.Schema(
554+
{
555+
vol.Required(CONF_NAME, default=DEFAULT_NAME): TextSelector()
556+
}
557+
)
558+
DATA_SCHEMA_OPTIONS = vol.Schema(
559+
{
560+
vol.Optional(CONF_SOME_SETTING): TextSelector()
561+
}
562+
)
563+
564+
CONFIG_FLOW = {
565+
"user": SchemaFlowFormStep(
566+
schema=DATA_SCHEMA_SETUP,
567+
next_step="options",
568+
),
569+
"options": SchemaFlowFormStep(
570+
schema=DATA_SCHEMA_OPTIONS,
571+
validate_user_input=validate_setup,
572+
),
573+
}
574+
OPTIONS_FLOW = {
575+
"init": SchemaFlowFormStep(
576+
DATA_SCHEMA_OPTIONS,
577+
validate_user_input=validate_setup,
578+
),
579+
}
580+
581+
class MyConfigFlowHandler(SchemaConfigFlowHandler, domain=DOMAIN):
582+
"""Handle a config flow."""
583+
584+
config_flow = CONFIG_FLOW
585+
options_flow = OPTIONS_FLOW
586+
options_flow_reloads = True # Reload without a config entry listener
587+
588+
def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
589+
"""Return config entry title from input."""
590+
return cast(str, options[CONF_NAME])
591+
592+
```
593+
525594
## Testing your config flow
526595

527596
Integrations with a config flow require full test coverage of all code in `config_flow.py` to be accepted into core. [Test your code](development_testing.md#running-a-limited-test-suite) includes more details on how to generate a coverage report.

0 commit comments

Comments
 (0)