diff --git a/homeassistant/components/ollama/config_flow.py b/homeassistant/components/ollama/config_flow.py index 68deb00d205..bf953d54016 100644 --- a/homeassistant/components/ollama/config_flow.py +++ b/homeassistant/components/ollama/config_flow.py @@ -306,6 +306,14 @@ class OllamaSubentryFlowHandler(ConfigSubentryFlow): async_step_reconfigure = async_step_set_options +def filter_invalid_llm_apis(hass: HomeAssistant, selected_apis: list[str]) -> list[str]: + """Accepts a list of LLM API IDs and filters this against those currently available.""" + + valid_llm_apis = [api.id for api in llm.async_get_apis(hass)] + + return [api for api in selected_apis if api in valid_llm_apis] + + def ollama_config_option_schema( hass: HomeAssistant, is_new: bool, @@ -326,6 +334,10 @@ def ollama_config_option_schema( else: schema = {} + selected_llm_apis = filter_invalid_llm_apis( + hass, options.get(CONF_LLM_HASS_API, []) + ) + schema.update( { vol.Required( @@ -349,7 +361,7 @@ def ollama_config_option_schema( ): TemplateSelector(), vol.Optional( CONF_LLM_HASS_API, - description={"suggested_value": options.get(CONF_LLM_HASS_API)}, + description={"suggested_value": selected_llm_apis}, ): SelectSelector( SelectSelectorConfig( options=[ diff --git a/tests/components/ollama/conftest.py b/tests/components/ollama/conftest.py index f3406bf5566..4b6b9a41fa3 100644 --- a/tests/components/ollama/conftest.py +++ b/tests/components/ollama/conftest.py @@ -68,6 +68,23 @@ def mock_config_entry_with_assist( return mock_config_entry +@pytest.fixture +def mock_config_entry_with_assist_invalid_api( + hass: HomeAssistant, mock_config_entry: MockConfigEntry +) -> MockConfigEntry: + """Mock a config entry with assist.""" + 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: [llm.LLM_API_ASSIST, "invalid_api"], + }, + ) + return mock_config_entry + + @pytest.fixture async def mock_init_component(hass: HomeAssistant, mock_config_entry: MockConfigEntry): """Initialize integration.""" diff --git a/tests/components/ollama/test_config_flow.py b/tests/components/ollama/test_config_flow.py index 1a873c2adb7..ce52cfaf9ec 100644 --- a/tests/components/ollama/test_config_flow.py +++ b/tests/components/ollama/test_config_flow.py @@ -8,7 +8,7 @@ import pytest from homeassistant import config_entries from homeassistant.components import ollama -from homeassistant.const import CONF_NAME +from homeassistant.const import CONF_LLM_HASS_API, CONF_NAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -463,6 +463,27 @@ async def test_subentry_reconfigure_with_download( } +async def test_filter_invalid_llms( + hass: HomeAssistant, + mock_init_component, + mock_config_entry_with_assist_invalid_api: MockConfigEntry, +) -> None: + """Test reconfiguring subentry when one of the configured LLM APIs has been removed.""" + subentry = next(iter(mock_config_entry_with_assist_invalid_api.subentries.values())) + + assert len(subentry.data.get(CONF_LLM_HASS_API)) == 2 + assert "invalid_api" in subentry.data.get(CONF_LLM_HASS_API) + assert "assist" in subentry.data.get(CONF_LLM_HASS_API) + + valid_apis = ollama.config_flow.filter_invalid_llm_apis( + hass, subentry.data[CONF_LLM_HASS_API] + ) + + assert len(valid_apis) == 1 + assert "invalid_api" not in valid_apis + assert "assist" in valid_apis + + async def test_creating_ai_task_subentry( hass: HomeAssistant, mock_config_entry: MockConfigEntry,