diff --git a/homeassistant/helpers/schema_config_entry_flow.py b/homeassistant/helpers/schema_config_entry_flow.py index 8bc773d85f7..0ee406a7a19 100644 --- a/homeassistant/helpers/schema_config_entry_flow.py +++ b/homeassistant/helpers/schema_config_entry_flow.py @@ -107,7 +107,15 @@ class SchemaFlowMenuStep(SchemaFlowStep): """Define a config or options flow menu step.""" # Menu options - options: Container[str] + options: ( + Container[str] + | Callable[[SchemaCommonFlowHandler], Coroutine[Any, Any, Container[str]]] + ) + """Menu options, or function which returns menu options. + + - If a function is specified, the function will be passed the current + `SchemaCommonFlowHandler`. + """ class SchemaCommonFlowHandler: @@ -152,6 +160,11 @@ class SchemaCommonFlowHandler: return await self._async_form_step(step_id, user_input) return await self._async_menu_step(step_id, user_input) + async def _get_options(self, form_step: SchemaFlowMenuStep) -> Container[str]: + if isinstance(form_step.options, Container): + return form_step.options + return await form_step.options(self) + async def _get_schema(self, form_step: SchemaFlowFormStep) -> vol.Schema | None: if form_step.schema is None: return None @@ -255,7 +268,7 @@ class SchemaCommonFlowHandler: menu_step = cast(SchemaFlowMenuStep, self._flow[next_step_id]) return self._handler.async_show_menu( step_id=next_step_id, - menu_options=menu_step.options, + menu_options=await self._get_options(menu_step), ) form_step = cast(SchemaFlowFormStep, self._flow[next_step_id]) @@ -308,7 +321,7 @@ class SchemaCommonFlowHandler: menu_step: SchemaFlowMenuStep = cast(SchemaFlowMenuStep, self._flow[step_id]) return self._handler.async_show_menu( step_id=step_id, - menu_options=menu_step.options, + menu_options=await self._get_options(menu_step), ) diff --git a/tests/helpers/test_schema_config_entry_flow.py b/tests/helpers/test_schema_config_entry_flow.py index e76faf9ee52..0ad21a1950a 100644 --- a/tests/helpers/test_schema_config_entry_flow.py +++ b/tests/helpers/test_schema_config_entry_flow.py @@ -317,7 +317,9 @@ async def test_menu_step(hass: HomeAssistant) -> None: """Test menu step.""" MENU_1 = ["option1", "option2"] - MENU_2 = ["option3", "option4"] + + async def menu_2(handler: SchemaCommonFlowHandler) -> list[str]: + return ["option3", "option4"] async def _option1_next_step(_: dict[str, Any]) -> str: return "menu2" @@ -325,7 +327,7 @@ async def test_menu_step(hass: HomeAssistant) -> None: CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { "user": SchemaFlowMenuStep(MENU_1), "option1": SchemaFlowFormStep(vol.Schema({}), next_step=_option1_next_step), - "menu2": SchemaFlowMenuStep(MENU_2), + "menu2": SchemaFlowMenuStep(menu_2), "option3": SchemaFlowFormStep(vol.Schema({}), next_step="option4"), "option4": SchemaFlowFormStep(vol.Schema({})), }