feat: one entry per provider, endpoints per type and model from a live dropdown
Reshapes setup around how a multi-model proxy is actually used: one config entry per server/provider, then one endpoint per model added by hand, with the model chosen from what that provider advertises right now. Setup (async_step_user): - Adds a Name field. With one entry per provider the title matters, and it was hardcoded to "ChatGPT" regardless of where the entry pointed. Blank falls back to the endpoint host via url_util.title_from_base_url. Deliberately absent from the reauth schema (now split out as STEP_REAUTH_DATA_SCHEMA): that step writes its input back into entry.data via data_updates, so a name field there would persist the title as connection data on every key rotation. - Stops auto-creating four subentries. conversation/ai_task_data/stt/tts were all created up front carrying RECOMMENDED_* options, which pin OpenAI model names (gpt-4o-mini-tts, gpt-4o-mini-transcribe). Against a third-party endpoint those entities are dead on arrival -- the reported symptom was an install that "loads bits, it's all janky". - Drops _async_abort_entries_match, so several entries may point at one server (one per provider-side key, for instance). Upstream aborts with already_configured on matching url+key. Model discovery (new model_list.py, kept out of the vendored files like url_util.py): - async_fetch_model_ids asks the endpoint's GET /v1/models. It calls with_options(max_retries=0), because this runs while the user waits on a dialog and the SDK otherwise retries with backoff -- visible in the test run as repeated "Retrying request to /models" and a suite that went from 5s to minutes. - Returns [] on any OpenAIError, and model_field() then yields a plain text field instead of a dropdown, so an endpoint that serves /v1/responses but not /v1/models is still configurable by hand. The dropdown sets custom_value=True for the same reason (LiteLLM wildcard routes). - Wired into all four types: conversation and ai_task_data on the additional step, stt (which hardcoded OpenAI's three transcribe models) and tts on init. TTS had no model field at all -- it silently used gpt-4o-mini-tts. A new endpoint now starts with "recommended" unticked. Ticked, the flow ends on the first step and never shows a model field, which is why the model looked unpickable; reconfigure still keeps whatever was saved. The model field stays on the additional step rather than moving to init. The init position reads better, but 71 ported tests drive the model through the additional step, several from parametrized option dicts that no mechanical edit could relocate. Rewriting the only regression net against upstream to save one click was the wrong trade. Also: the setup description no longer says "your OpenAI API key", and instructions_url pointed at a README anchor (#generate-an-api-key) that does not exist -- now #configuration. strings.json re-verified against core's RE_URL: 0 violations. Local: 167 passed. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NxJYWgGJtCw13qUHZtG8ki
This commit is contained in:
1 parent
13c60c242a
commit
e7fcb1d7a1
10 files changed
+583
-98
No files matched your search
+28
-33
@@ -95,42 +95,30 @@ async def test_form(hass: HomeAssistant) -> None:
|
||||
"base_url": DEFAULT_BASE_URL,
|
||||
}
|
||||
assert result2["options"] == {}
|
||||
assert result2["subentries"] == [
|
||||
{
|
||||
"subentry_type": "conversation",
|
||||
"data": RECOMMENDED_CONVERSATION_OPTIONS,
|
||||
"title": DEFAULT_CONVERSATION_NAME,
|
||||
"unique_id": None,
|
||||
},
|
||||
{
|
||||
"subentry_type": "ai_task_data",
|
||||
"data": RECOMMENDED_AI_TASK_OPTIONS,
|
||||
"title": DEFAULT_AI_TASK_NAME,
|
||||
"unique_id": None,
|
||||
},
|
||||
{
|
||||
"subentry_type": "stt",
|
||||
"data": RECOMMENDED_STT_OPTIONS,
|
||||
"title": DEFAULT_STT_NAME,
|
||||
"unique_id": None,
|
||||
},
|
||||
{
|
||||
"subentry_type": "tts",
|
||||
"data": RECOMMENDED_TTS_OPTIONS,
|
||||
"title": DEFAULT_TTS_NAME,
|
||||
"unique_id": None,
|
||||
},
|
||||
]
|
||||
# Setup creates the provider only. Entities are added afterwards, one per
|
||||
# type and model, so nothing here is guessed on the user's behalf.
|
||||
assert list(result2["subentries"]) == []
|
||||
assert result2["title"] == "api.openai.com"
|
||||
assert result2["version"] == 2
|
||||
assert result2["minor_version"] == 8
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_duplicate_entry(hass: HomeAssistant) -> None:
|
||||
"""Test we abort on duplicate config entry."""
|
||||
async def test_duplicate_entry_is_allowed(hass: HomeAssistant) -> None:
|
||||
"""Several entries may point at one provider.
|
||||
|
||||
Upstream aborts with already_configured when url+key match an existing
|
||||
entry. This fork drops that check -- see async_step_user -- because one
|
||||
entry per provider-side key, or a second entry against the same server,
|
||||
are both legitimate here.
|
||||
"""
|
||||
# Pinned to the current version: the v1 -> v2 migration collapses multiple
|
||||
# entries, which would remove one and mask what this test is checking.
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={CONF_API_KEY: "bla", CONF_BASE_URL: DEFAULT_BASE_URL},
|
||||
version=2,
|
||||
minor_version=8,
|
||||
).add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -139,9 +127,15 @@ async def test_duplicate_entry(hass: HomeAssistant) -> None:
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert not result["errors"]
|
||||
|
||||
with patch(
|
||||
"custom_components.openai_compatible.config_flow.openai.resources.models.AsyncModels.list",
|
||||
new_callable=AsyncMock,
|
||||
with (
|
||||
patch(
|
||||
"custom_components.openai_compatible.config_flow.openai.resources.models.AsyncModels.list",
|
||||
new_callable=AsyncMock,
|
||||
),
|
||||
patch(
|
||||
"custom_components.openai_compatible.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
@@ -149,9 +143,10 @@ async def test_duplicate_entry(hass: HomeAssistant) -> None:
|
||||
CONF_API_KEY: "bla",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 2
|
||||
|
||||
|
||||
async def test_creating_conversation_subentry(
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
"""Model discovery: turn an endpoint's GET /v1/models into a picker."""
|
||||
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import openai
|
||||
import pytest
|
||||
|
||||
from homeassistant.helpers.selector import SelectSelector
|
||||
|
||||
from custom_components.openai_compatible.model_list import (
|
||||
async_fetch_model_ids,
|
||||
model_field,
|
||||
)
|
||||
|
||||
|
||||
def _client(model_ids: list[str] | None = None, *, error: Exception | None = None):
|
||||
"""Return a stub AsyncOpenAI whose models.list yields model_ids or raises."""
|
||||
client = MagicMock()
|
||||
# Production calls client.with_options(...).models.list(); returning the
|
||||
# same stub keeps the per-request options visible to assertions.
|
||||
client.with_options.return_value = client
|
||||
if error is not None:
|
||||
client.models.list = AsyncMock(side_effect=error)
|
||||
else:
|
||||
page = MagicMock()
|
||||
page.data = [MagicMock(id=model_id) for model_id in (model_ids or [])]
|
||||
client.models.list = AsyncMock(return_value=page)
|
||||
return client
|
||||
|
||||
|
||||
async def test_fetch_returns_sorted_unique_ids() -> None:
|
||||
"""A proxy can list the same alias twice; the picker should show it once."""
|
||||
client = _client(["gpt-4o-mini", "claude-sonnet", "gpt-4o-mini", "llama-3.3"])
|
||||
|
||||
assert await async_fetch_model_ids(client) == [
|
||||
"claude-sonnet",
|
||||
"gpt-4o-mini",
|
||||
"llama-3.3",
|
||||
]
|
||||
|
||||
|
||||
async def test_fetch_ignores_entries_without_an_id() -> None:
|
||||
"""Don't put a blank option in the dropdown."""
|
||||
page = MagicMock()
|
||||
page.data = [MagicMock(id="real-model"), MagicMock(id=None), MagicMock(id="")]
|
||||
client = MagicMock()
|
||||
client.with_options.return_value = client
|
||||
client.models.list = AsyncMock(return_value=page)
|
||||
|
||||
assert await async_fetch_model_ids(client) == ["real-model"]
|
||||
|
||||
|
||||
async def test_fetch_does_not_retry() -> None:
|
||||
"""This runs while the user waits on a dialog; retries would stall it."""
|
||||
client = _client(["gpt-4o-mini"])
|
||||
|
||||
await async_fetch_model_ids(client, timeout=3.0)
|
||||
|
||||
client.with_options.assert_called_once_with(max_retries=0, timeout=3.0)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"error",
|
||||
[
|
||||
openai.APIConnectionError(request=MagicMock()),
|
||||
openai.AuthenticationError(
|
||||
"nope", response=MagicMock(status_code=401), body=None
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_fetch_survives_an_endpoint_that_will_not_list(error: Exception) -> None:
|
||||
"""Model discovery is a convenience; it must never break the config flow."""
|
||||
assert await async_fetch_model_ids(_client(error=error)) == []
|
||||
|
||||
|
||||
async def test_field_is_a_dropdown_that_still_accepts_a_typed_name() -> None:
|
||||
"""A proxy may serve a model it does not advertise, so allow free text."""
|
||||
field = model_field(["claude-sonnet", "gpt-4o-mini"])
|
||||
|
||||
assert isinstance(field, SelectSelector)
|
||||
assert field.config["options"] == ["claude-sonnet", "gpt-4o-mini"]
|
||||
assert field.config["custom_value"] is True
|
||||
|
||||
|
||||
async def test_field_falls_back_to_free_text_when_no_models_are_known() -> None:
|
||||
"""An endpoint that won't list models must still be configurable by hand."""
|
||||
assert model_field([]) is str
|
||||
@@ -0,0 +1,152 @@
|
||||
"""Each endpoint type picks its model from what the provider advertises.
|
||||
|
||||
Upstream defaults every model field to an OpenAI name and offers no way to see
|
||||
what the endpoint actually serves, so configuring a proxy meant typing exact
|
||||
model ids from memory -- and for conversation/AI Task the field was skipped
|
||||
entirely unless you knew to untick "recommended" first.
|
||||
"""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.helpers.selector import SelectSelector
|
||||
|
||||
from pytest_homeassistant_custom_component.common import MockConfigEntry
|
||||
|
||||
from custom_components.openai_compatible.const import (
|
||||
CONF_CHAT_MODEL,
|
||||
CONF_RECOMMENDED,
|
||||
)
|
||||
|
||||
PROVIDER_MODELS = ["claude-sonnet-4-6", "gpt-4o-mini", "whisper-1"]
|
||||
DISCOVERY = "custom_components.openai_compatible.config_flow.async_fetch_model_ids"
|
||||
|
||||
|
||||
def _field(result: dict, key: str):
|
||||
"""Return the schema field for key in a shown form."""
|
||||
schema = result["data_schema"].schema
|
||||
return next(value for marker, value in schema.items() if marker == key)
|
||||
|
||||
|
||||
def _marker(result: dict, key: str):
|
||||
"""Return the schema marker (which carries the default) for key."""
|
||||
schema = result["data_schema"].schema
|
||||
return next(marker for marker in schema if marker == key)
|
||||
|
||||
|
||||
async def _model_step(
|
||||
hass: HomeAssistant,
|
||||
entry: MockConfigEntry,
|
||||
subentry_type: str,
|
||||
models: list[str],
|
||||
) -> dict:
|
||||
"""Return the form that asks for the model, for any endpoint type.
|
||||
|
||||
stt and tts are single-step flows that ask on init; conversation and
|
||||
ai_task_data ask on the step after it.
|
||||
"""
|
||||
with patch(DISCOVERY, return_value=models):
|
||||
result = await hass.config_entries.subentries.async_init(
|
||||
(entry.entry_id, subentry_type),
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
if subentry_type in ("stt", "tts"):
|
||||
return result
|
||||
|
||||
result = await hass.config_entries.subentries.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_NAME: "An endpoint", CONF_RECOMMENDED: False},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "additional"
|
||||
return result
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"subentry_type", ["conversation", "ai_task_data", "stt", "tts"]
|
||||
)
|
||||
async def test_every_type_offers_the_providers_models(
|
||||
hass: HomeAssistant,
|
||||
mock_init_component: None,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
subentry_type: str,
|
||||
) -> None:
|
||||
"""All four endpoint types get a dropdown, not a free-text box."""
|
||||
result = await _model_step(
|
||||
hass, mock_config_entry, subentry_type, PROVIDER_MODELS
|
||||
)
|
||||
|
||||
field = _field(result, CONF_CHAT_MODEL)
|
||||
assert isinstance(field, SelectSelector)
|
||||
assert field.config["options"] == PROVIDER_MODELS
|
||||
# A provider may route a model it does not advertise.
|
||||
assert field.config["custom_value"] is True
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"subentry_type", ["conversation", "ai_task_data", "stt", "tts"]
|
||||
)
|
||||
async def test_falls_back_to_free_text_when_provider_lists_nothing(
|
||||
hass: HomeAssistant,
|
||||
mock_init_component: None,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
subentry_type: str,
|
||||
) -> None:
|
||||
"""An endpoint without /v1/models must still be configurable by hand."""
|
||||
result = await _model_step(hass, mock_config_entry, subentry_type, [])
|
||||
|
||||
assert _field(result, CONF_CHAT_MODEL) is str
|
||||
|
||||
|
||||
async def test_new_endpoint_starts_with_recommended_unticked(
|
||||
hass: HomeAssistant,
|
||||
mock_init_component: None,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Ticked, the flow ends on the first step and never asks for a model."""
|
||||
with patch(DISCOVERY, return_value=PROVIDER_MODELS):
|
||||
result = await hass.config_entries.subentries.async_init(
|
||||
(mock_config_entry.entry_id, "conversation"),
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
)
|
||||
|
||||
assert _marker(result, CONF_RECOMMENDED).default() is False
|
||||
|
||||
|
||||
async def test_a_non_openai_model_name_survives_the_flow(
|
||||
hass: HomeAssistant,
|
||||
mock_init_component: None,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""The point of the fork: the stored model is the provider's, and none of
|
||||
upstream's OpenAI-specific model gates reject it."""
|
||||
with patch(DISCOVERY, return_value=PROVIDER_MODELS):
|
||||
result = await hass.config_entries.subentries.async_init(
|
||||
(mock_config_entry.entry_id, "conversation"),
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
)
|
||||
result = await hass.config_entries.subentries.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_NAME: "Sonnet via LiteLLM", CONF_RECOMMENDED: False},
|
||||
)
|
||||
result = await hass.config_entries.subentries.async_configure(
|
||||
result["flow_id"], {CONF_CHAT_MODEL: "claude-sonnet-4-6"}
|
||||
)
|
||||
# remaining steps take their defaults
|
||||
while result["type"] is FlowResultType.FORM:
|
||||
result = await hass.config_entries.subentries.async_configure(
|
||||
result["flow_id"], {}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Sonnet via LiteLLM"
|
||||
assert result["data"][CONF_CHAT_MODEL] == "claude-sonnet-4-6"
|
||||
@@ -0,0 +1,117 @@
|
||||
"""Setup creates a provider, not a pile of guessed endpoints.
|
||||
|
||||
One config entry per server/provider (LiteLLM, OpenAI, anything else): the
|
||||
flow asks for a URL and a key and stops there. Entities are added afterwards,
|
||||
one per type and model, by the subentry flows.
|
||||
"""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from homeassistant.const import CONF_NAME
|
||||
|
||||
from custom_components.openai_compatible.const import CONF_BASE_URL, DOMAIN
|
||||
|
||||
LITELLM = "http://192.0.2.10:4000/v1"
|
||||
|
||||
|
||||
async def _run_user_step(
|
||||
hass: HomeAssistant, base_url: str, api_key: str, name: str | None = None
|
||||
) -> dict:
|
||||
"""Drive the user step to completion and return the flow result."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
|
||||
with (
|
||||
patch(
|
||||
"custom_components.openai_compatible.config_flow.openai.resources.models.AsyncModels.list",
|
||||
new_callable=AsyncMock,
|
||||
),
|
||||
patch(
|
||||
"custom_components.openai_compatible.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
user_input = {CONF_BASE_URL: base_url, "api_key": api_key}
|
||||
if name is not None:
|
||||
user_input[CONF_NAME] = name
|
||||
done = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
return done
|
||||
|
||||
|
||||
async def test_setup_creates_no_subentries(hass: HomeAssistant) -> None:
|
||||
"""Auto-created stt/tts entities pointed at OpenAI-only model names were
|
||||
dead on arrival against a third-party endpoint."""
|
||||
done = await _run_user_step(hass, LITELLM, "sk-test")
|
||||
|
||||
assert done["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert list(done["subentries"]) == []
|
||||
|
||||
|
||||
async def test_entry_is_titled_after_the_endpoint(hass: HomeAssistant) -> None:
|
||||
"""The title said "ChatGPT" regardless of which provider it pointed at."""
|
||||
done = await _run_user_step(hass, LITELLM, "sk-test")
|
||||
|
||||
assert done["title"] == "192.0.2.10:4000"
|
||||
|
||||
|
||||
async def test_a_second_entry_for_the_same_provider_is_allowed(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Upstream aborts as already_configured on matching url+key; several
|
||||
entries against one server have to be possible here."""
|
||||
first = await _run_user_step(hass, LITELLM, "sk-test")
|
||||
assert first["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
second = await _run_user_step(hass, LITELLM, "sk-test")
|
||||
assert second["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 2
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("base_url", "expected"),
|
||||
[
|
||||
("http://192.0.2.10:4000/v1", "192.0.2.10:4000"),
|
||||
("https://api.openai.com/v1", "api.openai.com"),
|
||||
("https://openrouter.ai/api/v1", "openrouter.ai"),
|
||||
],
|
||||
)
|
||||
async def test_title_from_base_url(base_url: str, expected: str) -> None:
|
||||
"""The host distinguishes one provider entry from another."""
|
||||
from custom_components.openai_compatible.url_util import title_from_base_url
|
||||
|
||||
assert title_from_base_url(base_url) == expected
|
||||
|
||||
|
||||
async def test_name_becomes_the_entry_title(hass: HomeAssistant) -> None:
|
||||
"""With one entry per provider, the user names the provider."""
|
||||
done = await _run_user_step(hass, LITELLM, "sk-test", name="LiteLLM")
|
||||
|
||||
assert done["title"] == "LiteLLM"
|
||||
|
||||
|
||||
async def test_name_is_not_stored_as_connection_data(hass: HomeAssistant) -> None:
|
||||
"""The name is the entry title; it is not part of how we reach the API."""
|
||||
done = await _run_user_step(hass, LITELLM, "sk-test", name="LiteLLM")
|
||||
|
||||
assert done["data"] == {CONF_BASE_URL: LITELLM, "api_key": "sk-test"}
|
||||
assert CONF_NAME not in done["data"]
|
||||
|
||||
|
||||
async def test_blank_name_falls_back_to_the_endpoint_host(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Leaving it empty should not produce an entry titled ""."""
|
||||
done = await _run_user_step(hass, LITELLM, "sk-test", name=" ")
|
||||
|
||||
assert done["title"] == "192.0.2.10:4000"
|
||||
Reference in new issue
Block a user