diff --git a/homeassistant/components/google_generative_ai_conversation/config_flow.py b/homeassistant/components/google_generative_ai_conversation/config_flow.py index 9048304a006..0572c63085a 100644 --- a/homeassistant/components/google_generative_ai_conversation/config_flow.py +++ b/homeassistant/components/google_generative_ai_conversation/config_flow.py @@ -305,10 +305,11 @@ async def google_generative_ai_config_option_schema( ) for api in llm.async_get_apis(hass) ] - if (suggested_llm_apis := options.get(CONF_LLM_HASS_API)) and isinstance( - suggested_llm_apis, str - ): - suggested_llm_apis = [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] + known_apis = {api.id for api in llm.async_get_apis(hass)} + suggested_llm_apis = [api for api in suggested_llm_apis if api in known_apis] if is_new: if CONF_NAME in options: diff --git a/tests/components/google_generative_ai_conversation/test_config_flow.py b/tests/components/google_generative_ai_conversation/test_config_flow.py index 52def1d06bb..7f5e5685ecb 100644 --- a/tests/components/google_generative_ai_conversation/test_config_flow.py +++ b/tests/components/google_generative_ai_conversation/test_config_flow.py @@ -707,3 +707,53 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: assert hass.config_entries.async_entries(DOMAIN)[0].data == {"api_key": "1234"} assert len(mock_unload_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1 + + +@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: str | 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={CONF_LLM_HASS_API: current_llm_apis}, + ) + await hass.async_block_till_done() + + with patch( + "google.genai.models.AsyncModels.list", + return_value=get_models_pager(), + ): + 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"] == "set_options" + + # 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