mirror of
https://github.com/home-assistant/core.git
synced 2025-07-12 15:57:06 +00:00
Add multiple LLM API support for MCP Server (#147785)
* Add multiple LLM API support for MCP Server * Update homeassistant/components/mcp_server/config_flow.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * ruff * Update tests/components/mcp_server/conftest.py Co-authored-by: Allen Porter <allen.porter@gmail.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Allen Porter <allen.porter@gmail.com>
This commit is contained in:
parent
073a467fb2
commit
7deca35172
@ -32,10 +32,17 @@ class ModelContextServerProtocolConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle the initial step."""
|
"""Handle the initial step."""
|
||||||
|
errors: dict[str, str] = {}
|
||||||
llm_apis = {api.id: api.name for api in llm.async_get_apis(self.hass)}
|
llm_apis = {api.id: api.name for api in llm.async_get_apis(self.hass)}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
|
if not user_input[CONF_LLM_HASS_API]:
|
||||||
|
errors[CONF_LLM_HASS_API] = "llm_api_required"
|
||||||
|
else:
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=llm_apis[user_input[CONF_LLM_HASS_API]], data=user_input
|
title=", ".join(
|
||||||
|
llm_apis[api_id] for api_id in user_input[CONF_LLM_HASS_API]
|
||||||
|
),
|
||||||
|
data=user_input,
|
||||||
)
|
)
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
@ -44,7 +51,7 @@ class ModelContextServerProtocolConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
{
|
{
|
||||||
vol.Optional(
|
vol.Optional(
|
||||||
CONF_LLM_HASS_API,
|
CONF_LLM_HASS_API,
|
||||||
default=llm.LLM_API_ASSIST,
|
default=[llm.LLM_API_ASSIST],
|
||||||
): SelectSelector(
|
): SelectSelector(
|
||||||
SelectSelectorConfig(
|
SelectSelectorConfig(
|
||||||
options=[
|
options=[
|
||||||
@ -53,10 +60,12 @@ class ModelContextServerProtocolConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
value=llm_api_id,
|
value=llm_api_id,
|
||||||
)
|
)
|
||||||
for llm_api_id, name in llm_apis.items()
|
for llm_api_id, name in llm_apis.items()
|
||||||
]
|
],
|
||||||
|
multiple=True,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
description_placeholders={"more_info_url": MORE_INFO_URL},
|
description_placeholders={"more_info_url": MORE_INFO_URL},
|
||||||
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
@ -42,7 +42,7 @@ def _format_tool(
|
|||||||
|
|
||||||
|
|
||||||
async def create_server(
|
async def create_server(
|
||||||
hass: HomeAssistant, llm_api_id: str, llm_context: llm.LLMContext
|
hass: HomeAssistant, llm_api_id: str | list[str], llm_context: llm.LLMContext
|
||||||
) -> Server:
|
) -> Server:
|
||||||
"""Create a new Model Context Protocol Server.
|
"""Create a new Model Context Protocol Server.
|
||||||
|
|
||||||
|
@ -11,6 +11,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"error": {
|
||||||
|
"llm_api_required": "At least one LLM API must be configured."
|
||||||
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
||||||
}
|
}
|
||||||
|
@ -23,13 +23,15 @@ def mock_setup_entry() -> Generator[AsyncMock]:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="llm_hass_api")
|
@pytest.fixture(name="llm_hass_api")
|
||||||
def llm_hass_api_fixture() -> str:
|
def llm_hass_api_fixture() -> list[str]:
|
||||||
"""Fixture for the config entry llm_hass_api."""
|
"""Fixture for the config entry llm_hass_api."""
|
||||||
return llm.LLM_API_ASSIST
|
return [llm.LLM_API_ASSIST]
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="config_entry")
|
@pytest.fixture(name="config_entry")
|
||||||
def mock_config_entry(hass: HomeAssistant, llm_hass_api: str) -> MockConfigEntry:
|
def mock_config_entry(
|
||||||
|
hass: HomeAssistant, llm_hass_api: str | list[str]
|
||||||
|
) -> MockConfigEntry:
|
||||||
"""Fixture to load the integration."""
|
"""Fixture to load the integration."""
|
||||||
config_entry = MockConfigEntry(
|
config_entry = MockConfigEntry(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
|
@ -16,7 +16,7 @@ from homeassistant.data_entry_flow import FlowResultType
|
|||||||
"params",
|
"params",
|
||||||
[
|
[
|
||||||
{},
|
{},
|
||||||
{CONF_LLM_HASS_API: "assist"},
|
{CONF_LLM_HASS_API: ["assist"]},
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_form(
|
async def test_form(
|
||||||
@ -38,4 +38,33 @@ async def test_form(
|
|||||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == "Assist"
|
assert result["title"] == "Assist"
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
assert result["data"] == {CONF_LLM_HASS_API: "assist"}
|
assert result["data"] == {CONF_LLM_HASS_API: ["assist"]}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("params", "errors"),
|
||||||
|
[
|
||||||
|
({CONF_LLM_HASS_API: []}, {CONF_LLM_HASS_API: "llm_api_required"}),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_form_errors(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
params: dict[str, Any],
|
||||||
|
errors: dict[str, str],
|
||||||
|
) -> None:
|
||||||
|
"""Test we get the errors on invalid user input."""
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
|
)
|
||||||
|
assert result["type"] == FlowResultType.FORM
|
||||||
|
assert not result["errors"]
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
params,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result["type"] == FlowResultType.FORM
|
||||||
|
assert result["errors"] == errors
|
||||||
|
@ -194,7 +194,7 @@ async def test_http_sse_multiple_config_entries(
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
config_entry = MockConfigEntry(
|
config_entry = MockConfigEntry(
|
||||||
domain="mcp_server", data={CONF_LLM_HASS_API: "llm-api-id"}
|
domain="mcp_server", data={CONF_LLM_HASS_API: ["llm-api-id"]}
|
||||||
)
|
)
|
||||||
config_entry.add_to_hass(hass)
|
config_entry.add_to_hass(hass)
|
||||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user