Skip to content

Commit a90ac61

Browse files
authored
Allow dynamically creating menu options in SchemaFlowHandler (home-assistant#151191)
1 parent e2faa70 commit a90ac61

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

homeassistant/helpers/schema_config_entry_flow.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,15 @@ class SchemaFlowMenuStep(SchemaFlowStep):
107107
"""Define a config or options flow menu step."""
108108

109109
# Menu options
110-
options: Container[str]
110+
options: (
111+
Container[str]
112+
| Callable[[SchemaCommonFlowHandler], Coroutine[Any, Any, Container[str]]]
113+
)
114+
"""Menu options, or function which returns menu options.
115+
116+
- If a function is specified, the function will be passed the current
117+
`SchemaCommonFlowHandler`.
118+
"""
111119

112120

113121
class SchemaCommonFlowHandler:
@@ -152,6 +160,11 @@ async def async_step(
152160
return await self._async_form_step(step_id, user_input)
153161
return await self._async_menu_step(step_id, user_input)
154162

163+
async def _get_options(self, form_step: SchemaFlowMenuStep) -> Container[str]:
164+
if isinstance(form_step.options, Container):
165+
return form_step.options
166+
return await form_step.options(self)
167+
155168
async def _get_schema(self, form_step: SchemaFlowFormStep) -> vol.Schema | None:
156169
if form_step.schema is None:
157170
return None
@@ -255,7 +268,7 @@ async def _show_next_step(
255268
menu_step = cast(SchemaFlowMenuStep, self._flow[next_step_id])
256269
return self._handler.async_show_menu(
257270
step_id=next_step_id,
258-
menu_options=menu_step.options,
271+
menu_options=await self._get_options(menu_step),
259272
)
260273

261274
form_step = cast(SchemaFlowFormStep, self._flow[next_step_id])
@@ -308,7 +321,7 @@ async def _async_menu_step(
308321
menu_step: SchemaFlowMenuStep = cast(SchemaFlowMenuStep, self._flow[step_id])
309322
return self._handler.async_show_menu(
310323
step_id=step_id,
311-
menu_options=menu_step.options,
324+
menu_options=await self._get_options(menu_step),
312325
)
313326

314327

tests/helpers/test_schema_config_entry_flow.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,17 @@ async def test_menu_step(hass: HomeAssistant) -> None:
317317
"""Test menu step."""
318318

319319
MENU_1 = ["option1", "option2"]
320-
MENU_2 = ["option3", "option4"]
320+
321+
async def menu_2(handler: SchemaCommonFlowHandler) -> list[str]:
322+
return ["option3", "option4"]
321323

322324
async def _option1_next_step(_: dict[str, Any]) -> str:
323325
return "menu2"
324326

325327
CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = {
326328
"user": SchemaFlowMenuStep(MENU_1),
327329
"option1": SchemaFlowFormStep(vol.Schema({}), next_step=_option1_next_step),
328-
"menu2": SchemaFlowMenuStep(MENU_2),
330+
"menu2": SchemaFlowMenuStep(menu_2),
329331
"option3": SchemaFlowFormStep(vol.Schema({}), next_step="option4"),
330332
"option4": SchemaFlowFormStep(vol.Schema({})),
331333
}

0 commit comments

Comments
 (0)