mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Hide the LLM API picker when all MCP server LLM APIs are exposed (#184516)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
7150b5e767
commit
ceb65ccc2c
@@ -58,31 +58,26 @@ def _options_schema(
|
||||
require_admin: bool,
|
||||
) -> probatio.Schema:
|
||||
"""Return the schema for the options flow."""
|
||||
return probatio.Schema(
|
||||
{
|
||||
probatio.Required(
|
||||
CONF_ALL_LLM_APIS, default=all_llm_apis
|
||||
): BooleanSelector(),
|
||||
probatio.Optional(
|
||||
CONF_LLM_HASS_API,
|
||||
default=default,
|
||||
): SelectSelector(
|
||||
SelectSelectorConfig(
|
||||
options=[
|
||||
SelectOptionDict(
|
||||
label=name,
|
||||
value=llm_api_id,
|
||||
)
|
||||
for llm_api_id, name in llm_apis.items()
|
||||
],
|
||||
multiple=True,
|
||||
)
|
||||
),
|
||||
probatio.Required(
|
||||
CONF_REQUIRE_ADMIN, default=require_admin
|
||||
): BooleanSelector(),
|
||||
}
|
||||
schema: dict[probatio.Marker, Any] = {
|
||||
probatio.Required(CONF_ALL_LLM_APIS, default=all_llm_apis): BooleanSelector(),
|
||||
}
|
||||
if not all_llm_apis:
|
||||
schema[probatio.Optional(CONF_LLM_HASS_API, default=default)] = SelectSelector(
|
||||
SelectSelectorConfig(
|
||||
options=[
|
||||
SelectOptionDict(
|
||||
label=name,
|
||||
value=llm_api_id,
|
||||
)
|
||||
for llm_api_id, name in llm_apis.items()
|
||||
],
|
||||
multiple=True,
|
||||
)
|
||||
)
|
||||
schema[probatio.Required(CONF_REQUIRE_ADMIN, default=require_admin)] = (
|
||||
BooleanSelector()
|
||||
)
|
||||
return probatio.Schema(schema)
|
||||
|
||||
|
||||
class ModelContextServerProtocolConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
@@ -123,46 +118,50 @@ class ModelContextServerProtocolConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
class ModelContextServerProtocolOptionsFlow(OptionsFlow):
|
||||
"""Handle an options flow to change the exposed LLM APIs."""
|
||||
|
||||
last_rendered_all_llm_apis = False
|
||||
|
||||
async def async_step_init(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle the options step."""
|
||||
errors: dict[str, str] = {}
|
||||
llm_apis = _llm_api_names(self.hass)
|
||||
current = _selected_llm_apis(self.config_entry, llm_apis)
|
||||
# A disabled config entry has not migrated yet
|
||||
current_all = self.config_entry.data.get(CONF_ALL_LLM_APIS, False)
|
||||
current = _selected_llm_apis(self.config_entry, llm_apis)
|
||||
if user_input is not None:
|
||||
if not user_input[CONF_ALL_LLM_APIS] and not user_input[CONF_LLM_HASS_API]:
|
||||
errors[CONF_LLM_HASS_API] = "llm_api_required"
|
||||
else:
|
||||
updates: dict[str, Any] = {
|
||||
"data": {**self.config_entry.data, **user_input}
|
||||
}
|
||||
# Keep a title the user renamed, only refresh a generated one.
|
||||
if self.config_entry.title == _llm_api_title(
|
||||
llm_apis, current_all, current
|
||||
):
|
||||
updates["title"] = _llm_api_title(
|
||||
llm_apis,
|
||||
user_input[CONF_ALL_LLM_APIS],
|
||||
user_input[CONF_LLM_HASS_API],
|
||||
)
|
||||
self.hass.config_entries.async_update_entry(
|
||||
self.config_entry, **updates
|
||||
require_admin = self.config_entry.data.get(CONF_REQUIRE_ADMIN, False)
|
||||
|
||||
if user_input is None:
|
||||
self.last_rendered_all_llm_apis = current_all
|
||||
elif user_input[CONF_ALL_LLM_APIS] != self.last_rendered_all_llm_apis:
|
||||
# Re-render the form to show or hide the LLM API picker
|
||||
self.last_rendered_all_llm_apis = user_input[CONF_ALL_LLM_APIS]
|
||||
require_admin = user_input[CONF_REQUIRE_ADMIN]
|
||||
elif not user_input[CONF_ALL_LLM_APIS] and not user_input[CONF_LLM_HASS_API]:
|
||||
errors[CONF_LLM_HASS_API] = "llm_api_required"
|
||||
else:
|
||||
updates: dict[str, Any] = {"data": {**self.config_entry.data, **user_input}}
|
||||
# Keep a title the user renamed, only refresh a generated one.
|
||||
if self.config_entry.title == _llm_api_title(
|
||||
llm_apis, current_all, current
|
||||
):
|
||||
updates["title"] = _llm_api_title(
|
||||
llm_apis,
|
||||
user_input[CONF_ALL_LLM_APIS],
|
||||
user_input.get(CONF_LLM_HASS_API, []),
|
||||
)
|
||||
# An open SSE session keeps serving the APIs it started with.
|
||||
await self.hass.config_entries.async_reload(self.config_entry.entry_id)
|
||||
return self.async_create_entry(data={})
|
||||
self.hass.config_entries.async_update_entry(self.config_entry, **updates)
|
||||
# An open SSE session keeps serving the APIs it started with.
|
||||
await self.hass.config_entries.async_reload(self.config_entry.entry_id)
|
||||
return self.async_create_entry(data={})
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="init",
|
||||
data_schema=_options_schema(
|
||||
llm_apis,
|
||||
current_all,
|
||||
self.last_rendered_all_llm_apis,
|
||||
current,
|
||||
# A disabled config entry has not migrated yet
|
||||
self.config_entry.data.get(CONF_REQUIRE_ADMIN, False),
|
||||
require_admin,
|
||||
),
|
||||
description_placeholders={"more_info_url": MORE_INFO_URL},
|
||||
errors=errors,
|
||||
|
||||
@@ -24,9 +24,9 @@
|
||||
"require_admin": "Require an administrator account"
|
||||
},
|
||||
"data_description": {
|
||||
"all_llm_apis": "Expose every LLM API, including LLM APIs added later. When enabled, the selected LLM APIs are ignored.",
|
||||
"all_llm_apis": "Expose every LLM API, including LLM APIs added later.",
|
||||
"llm_hass_api": "The methods for controlling Home Assistant to expose with the Model Context Protocol.",
|
||||
"require_admin": "Only allow administrator accounts to use the Model Context Protocol endpoint."
|
||||
"require_admin": "Only allow administrator accounts to use this Model Context Protocol endpoint."
|
||||
},
|
||||
"description": "See the [integration documentation]({more_info_url}) for setup instructions."
|
||||
}
|
||||
|
||||
@@ -193,13 +193,33 @@ async def test_options_flow_unmigrated_entry(hass: HomeAssistant) -> None:
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("all_llm_apis", "llm_hass_api", "title", "user_input", "expected_title"),
|
||||
(
|
||||
"all_llm_apis",
|
||||
"llm_hass_api",
|
||||
"title",
|
||||
"initial_form",
|
||||
"rerendered_form",
|
||||
"user_input",
|
||||
"expected_data",
|
||||
"expected_title",
|
||||
),
|
||||
[
|
||||
pytest.param(
|
||||
False,
|
||||
[llm.LLM_API_ASSIST],
|
||||
"Assist",
|
||||
{CONF_ALL_LLM_APIS: True, CONF_LLM_HASS_API: []},
|
||||
{
|
||||
CONF_ALL_LLM_APIS: False,
|
||||
CONF_LLM_HASS_API: [llm.LLM_API_ASSIST],
|
||||
CONF_REQUIRE_ADMIN: False,
|
||||
},
|
||||
{CONF_ALL_LLM_APIS: True, CONF_REQUIRE_ADMIN: True},
|
||||
{},
|
||||
{
|
||||
CONF_ALL_LLM_APIS: True,
|
||||
CONF_LLM_HASS_API: [llm.LLM_API_ASSIST],
|
||||
CONF_REQUIRE_ADMIN: True,
|
||||
},
|
||||
"All LLM APIs",
|
||||
id="select_all",
|
||||
),
|
||||
@@ -207,7 +227,18 @@ async def test_options_flow_unmigrated_entry(hass: HomeAssistant) -> None:
|
||||
True,
|
||||
[],
|
||||
"All LLM APIs",
|
||||
{CONF_ALL_LLM_APIS: False, CONF_LLM_HASS_API: [TEST_LLM_API_ID]},
|
||||
{CONF_ALL_LLM_APIS: True, CONF_REQUIRE_ADMIN: False},
|
||||
{
|
||||
CONF_ALL_LLM_APIS: False,
|
||||
CONF_LLM_HASS_API: [],
|
||||
CONF_REQUIRE_ADMIN: True,
|
||||
},
|
||||
{CONF_LLM_HASS_API: [TEST_LLM_API_ID]},
|
||||
{
|
||||
CONF_ALL_LLM_APIS: False,
|
||||
CONF_LLM_HASS_API: [TEST_LLM_API_ID],
|
||||
CONF_REQUIRE_ADMIN: True,
|
||||
},
|
||||
"Test",
|
||||
id="select_individual",
|
||||
),
|
||||
@@ -216,8 +247,12 @@ async def test_options_flow_unmigrated_entry(hass: HomeAssistant) -> None:
|
||||
async def test_options_flow_all_llm_apis(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
all_llm_apis: bool,
|
||||
title: str,
|
||||
user_input: dict[str, bool | list[str]],
|
||||
initial_form: dict[str, bool | list[str]],
|
||||
rerendered_form: dict[str, bool | list[str]],
|
||||
user_input: dict[str, list[str]],
|
||||
expected_data: dict[str, bool | list[str]],
|
||||
expected_title: str,
|
||||
) -> None:
|
||||
"""Test switching between all LLM APIs and individual LLM APIs."""
|
||||
@@ -226,11 +261,23 @@ async def test_options_flow_all_llm_apis(
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
assert result["data_schema"]({}) == initial_form
|
||||
|
||||
# Changing the toggle re-renders the form to show or hide the LLM API picker
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"], {**user_input, CONF_REQUIRE_ADMIN: True}
|
||||
result["flow_id"],
|
||||
{CONF_ALL_LLM_APIS: not all_llm_apis, CONF_REQUIRE_ADMIN: True},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
assert result["data_schema"]({}) == rerendered_form
|
||||
assert config_entry.data[CONF_ALL_LLM_APIS] is all_llm_apis
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"], {**rerendered_form, **user_input}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert config_entry.data == {**user_input, CONF_REQUIRE_ADMIN: True}
|
||||
assert config_entry.data == expected_data
|
||||
assert config_entry.title == expected_title
|
||||
|
||||
Reference in New Issue
Block a user