diff --git a/homeassistant/components/openai_conversation/config_flow.py b/homeassistant/components/openai_conversation/config_flow.py index 5cc604ca16b..ed37569202c 100644 --- a/homeassistant/components/openai_conversation/config_flow.py +++ b/homeassistant/components/openai_conversation/config_flow.py @@ -237,10 +237,13 @@ class OpenAISubentryFlowHandler(ConfigSubentryFlow): ) for api in llm.async_get_apis(self.hass) ] - if (suggested_llm_apis := options.get(CONF_LLM_HASS_API)) and isinstance( - suggested_llm_apis, str - ): - options[CONF_LLM_HASS_API] = [suggested_llm_apis] + if suggested_llm_apis := options.get(CONF_LLM_HASS_API): + if isinstance(suggested_llm_apis, str): + suggested_llm_apis = [suggested_llm_apis] + valid_apis = {api.id for api in llm.async_get_apis(self.hass)} + options[CONF_LLM_HASS_API] = [ + api for api in suggested_llm_apis if api in valid_apis + ] step_schema: VolDictType = {} diff --git a/tests/components/openai_conversation/test_config_flow.py b/tests/components/openai_conversation/test_config_flow.py index 14a6ce49b2e..59c70d28912 100644 --- a/tests/components/openai_conversation/test_config_flow.py +++ b/tests/components/openai_conversation/test_config_flow.py @@ -1086,3 +1086,52 @@ async def test_reauth(hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reauth_successful" assert mock_config_entry.data[CONF_API_KEY] == "new_api_key" + + +@pytest.mark.parametrize( + ("current_llm_apis", "suggested_llm_apis", "expected_options"), + [ + ("assist", ["assist"], ["assist"]), + (["assist"], ["assist"], ["assist"]), + ("non-existent", [], ["assist"]), + (["non-existent"], [], ["assist"]), + (["assist", "non-existent"], ["assist"], ["assist"]), + ], +) +async def test_reconfigure_conversation_subentry_llm_api_schema( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_init_component, + 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.""" + 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() + + with patch( + "homeassistant.components.openai_conversation.config_flow.openai.AsyncOpenAI.models", + ): + subentry_flow = await mock_config_entry.start_subentry_reconfigure_flow( + hass, subentry.subentry_id + ) + + assert subentry_flow["type"] is FlowResultType.FORM + assert subentry_flow["step_id"] == "init" + + # Only valid LLM APIs should be suggested and shown as options + schema = subentry_flow["data_schema"].schema + key = next(k for k in schema if k == CONF_LLM_HASS_API) + assert key.description + assert key.description.get("suggested_value") == suggested_llm_apis + field_schema = schema[key] + assert field_schema.config + assert [ + opt["value"] for opt in field_schema.config.get("options") + ] == expected_options