Skip to content

Commit 828070c

Browse files
Add docs for OptionsFlowWithReload (#2730)
Co-authored-by: Abílio Costa <[email protected]>
1 parent 777501a commit 828070c

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

docs/config_entries_options_flow_handler.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,47 @@ def async_get_options_flow(
2525
The Flow handler works just like the config flow handler, except that the first step in the flow will always be `async_step_init`. The current config entry details are available through the `self.config_entry` property.
2626

2727
```python
28-
OPTIONS_SCHEMA=vol.Schema(
28+
from homeassistant.config_entries import OptionsFlow
29+
30+
OPTIONS_SCHEMA = vol.Schema(
31+
{
32+
vol.Required("show_things"): bool,
33+
}
34+
)
35+
class OptionsFlowHandler(OptionsFlow):
36+
async def async_step_init(
37+
self, user_input: dict[str, Any] | None = None
38+
) -> ConfigFlowResult:
39+
"""Manage the options."""
40+
if user_input is not None:
41+
return self.async_create_entry(data=user_input)
42+
43+
return self.async_show_form(
44+
step_id="init",
45+
data_schema=self.add_suggested_values_to_schema(
46+
OPTIONS_SCHEMA, self.config_entry.options
47+
),
48+
)
49+
```
50+
51+
## Options flow with automatic reload
52+
53+
If the integration should be reloaded after the config options change, it can subclass from `OptionsFlowWithReload` instead of `OptionsFlow`. `OptionsFlowWithReload` will automatically reload the integration once the options change.
54+
55+
Since the most common reason to add an update listener is to reload the integration when the options have changed, `OptionsFlowWithReload` avoids the need for that listener.
56+
57+
```python
58+
from homeassistant.config_entries import OptionsFlowWithReload
59+
60+
OPTIONS_SCHEMA = vol.Schema(
2961
{
3062
vol.Required("show_things"): bool,
3163
}
3264
)
33-
class OptionsFlowHandler(config_entries.OptionsFlow):
65+
class MyOptionsFlow(OptionsFlowWithReload):
3466
async def async_step_init(
3567
self, user_input: dict[str, Any] | None = None
36-
) -> FlowResult:
68+
) -> ConfigFlowResult:
3769
"""Manage the options."""
3870
if user_input is not None:
3971
return self.async_create_entry(data=user_input)
@@ -57,6 +89,6 @@ entry.async_on_unload(entry.add_update_listener(update_listener))
5789
Using the above means the Listener is attached when the entry is loaded and detached at unload. The Listener shall be an async function that takes the same input as async_setup_entry. Options can then be accessed from `entry.options`.
5890

5991
```python
60-
async def update_listener(hass, entry):
92+
async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry):
6193
"""Handle options update."""
6294
```

0 commit comments

Comments
 (0)