diff --git a/homeassistant/components/open_router/config_flow.py b/homeassistant/components/open_router/config_flow.py index 561dca60721..db9af4c0f26 100644 --- a/homeassistant/components/open_router/config_flow.py +++ b/homeassistant/components/open_router/config_flow.py @@ -173,6 +173,12 @@ class ConversationFlowHandler(OpenRouterSubentryFlowHandler): for api in llm.async_get_apis(self.hass) ] + if suggested_llm_apis := self.options.get(CONF_LLM_HASS_API): + valid_api_ids = {api["value"] for api in hass_apis} + self.options[CONF_LLM_HASS_API] = [ + api for api in suggested_llm_apis if api in valid_api_ids + ] + return self.async_show_form( step_id="init", data_schema=vol.Schema( diff --git a/tests/components/open_router/test_config_flow.py b/tests/components/open_router/test_config_flow.py index a980b10121d..523ef69aa3b 100644 --- a/tests/components/open_router/test_config_flow.py +++ b/tests/components/open_router/test_config_flow.py @@ -365,3 +365,51 @@ async def test_reconfigure_ai_task_abort( result = await mock_config_entry.start_subentry_reconfigure_flow(hass, subentry_id) assert result["type"] is FlowResultType.ABORT assert result["reason"] == reason + + +@pytest.mark.parametrize( + ("current_llm_apis", "suggested_llm_apis", "expected_options"), + [ + (["assist"], ["assist"], ["assist"]), + (["non-existent"], [], ["assist"]), + (["assist", "non-existent"], ["assist"], ["assist"]), + ], +) +async def test_reconfigure_conversation_subentry_llm_api_schema( + hass: HomeAssistant, + mock_open_router_client: AsyncMock, + mock_setup_entry: AsyncMock, + mock_config_entry: MockConfigEntry, + current_llm_apis: list[str], + suggested_llm_apis: list[str], + expected_options: list[str], +) -> None: + """Test llm_hass_api field values when reconfiguring a conversation subentry.""" + await setup_integration(hass, mock_config_entry) + + subentry = next(iter(mock_config_entry.subentries.values())) + hass.config_entries.async_update_subentry( + mock_config_entry, + subentry, + data={**subentry.data, CONF_LLM_HASS_API: current_llm_apis}, + ) + await hass.async_block_till_done() + + result = await mock_config_entry.start_subentry_reconfigure_flow( + hass, subentry.subentry_id + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "init" + + # Only valid LLM APIs should be suggested and shown as options + schema = result["data_schema"].schema + key = next(k for k in schema if k == CONF_LLM_HASS_API) + + assert key.default() == suggested_llm_apis + + field_schema = schema[key] + assert field_schema.config + assert [ + opt["value"] for opt in field_schema.config.get("options") + ] == expected_options