File tree Expand file tree Collapse file tree 3 files changed +52
-2
lines changed
homeassistant/components/ollama Expand file tree Collapse file tree 3 files changed +52
-2
lines changed Original file line number Diff line number Diff line change @@ -306,6 +306,14 @@ async def async_step_finish(
306306 async_step_reconfigure = async_step_set_options
307307
308308
309+ def filter_invalid_llm_apis (hass : HomeAssistant , selected_apis : list [str ]) -> list [str ]:
310+ """Accepts a list of LLM API IDs and filters this against those currently available."""
311+
312+ valid_llm_apis = [api .id for api in llm .async_get_apis (hass )]
313+
314+ return [api for api in selected_apis if api in valid_llm_apis ]
315+
316+
309317def ollama_config_option_schema (
310318 hass : HomeAssistant ,
311319 is_new : bool ,
@@ -326,6 +334,10 @@ def ollama_config_option_schema(
326334 else :
327335 schema = {}
328336
337+ selected_llm_apis = filter_invalid_llm_apis (
338+ hass , options .get (CONF_LLM_HASS_API , [])
339+ )
340+
329341 schema .update (
330342 {
331343 vol .Required (
@@ -349,7 +361,7 @@ def ollama_config_option_schema(
349361 ): TemplateSelector (),
350362 vol .Optional (
351363 CONF_LLM_HASS_API ,
352- description = {"suggested_value" : options . get ( CONF_LLM_HASS_API ) },
364+ description = {"suggested_value" : selected_llm_apis },
353365 ): SelectSelector (
354366 SelectSelectorConfig (
355367 options = [
Original file line number Diff line number Diff line change @@ -68,6 +68,23 @@ def mock_config_entry_with_assist(
6868 return mock_config_entry
6969
7070
71+ @pytest .fixture
72+ def mock_config_entry_with_assist_invalid_api (
73+ hass : HomeAssistant , mock_config_entry : MockConfigEntry
74+ ) -> MockConfigEntry :
75+ """Mock a config entry with assist."""
76+ subentry = next (iter (mock_config_entry .subentries .values ()))
77+ hass .config_entries .async_update_subentry (
78+ mock_config_entry ,
79+ subentry ,
80+ data = {
81+ ** subentry .data ,
82+ CONF_LLM_HASS_API : [llm .LLM_API_ASSIST , "invalid_api" ],
83+ },
84+ )
85+ return mock_config_entry
86+
87+
7188@pytest .fixture
7289async def mock_init_component (hass : HomeAssistant , mock_config_entry : MockConfigEntry ):
7390 """Initialize integration."""
Original file line number Diff line number Diff line change 88
99from homeassistant import config_entries
1010from homeassistant .components import ollama
11- from homeassistant .const import CONF_NAME
11+ from homeassistant .const import CONF_LLM_HASS_API , CONF_NAME
1212from homeassistant .core import HomeAssistant
1313from homeassistant .data_entry_flow import FlowResultType
1414
@@ -463,6 +463,27 @@ async def delayed_pull(self, model: str) -> None:
463463 }
464464
465465
466+ async def test_filter_invalid_llms (
467+ hass : HomeAssistant ,
468+ mock_init_component ,
469+ mock_config_entry_with_assist_invalid_api : MockConfigEntry ,
470+ ) -> None :
471+ """Test reconfiguring subentry when one of the configured LLM APIs has been removed."""
472+ subentry = next (iter (mock_config_entry_with_assist_invalid_api .subentries .values ()))
473+
474+ assert len (subentry .data .get (CONF_LLM_HASS_API )) == 2
475+ assert "invalid_api" in subentry .data .get (CONF_LLM_HASS_API )
476+ assert "assist" in subentry .data .get (CONF_LLM_HASS_API )
477+
478+ valid_apis = ollama .config_flow .filter_invalid_llm_apis (
479+ hass , subentry .data [CONF_LLM_HASS_API ]
480+ )
481+
482+ assert len (valid_apis ) == 1
483+ assert "invalid_api" not in valid_apis
484+ assert "assist" in valid_apis
485+
486+
466487async def test_creating_ai_task_subentry (
467488 hass : HomeAssistant ,
468489 mock_config_entry : MockConfigEntry ,
You can’t perform that action at this time.
0 commit comments