mirror of
https://github.com/Electric-Special/ha-core.git
synced 2026-03-21 03:03:17 +01:00
Update Google Gemini config flow to filter out invalid LLM APIs (#161552)
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user