Move HA user agent routing to options

This commit is contained in:
2026-05-06 14:39:46 +02:00
parent 2a02c18b86
commit 1624bb9ff7
7 changed files with 155 additions and 19 deletions
+49 -4
View File
@@ -46,6 +46,7 @@ from .const import (
CONF_CONTEXT_MAX_CHARS,
CONF_CONTEXT_STRATEGY,
CONF_ENABLE_TOOL_CALLS,
CONF_FALLBACK_AGENT_ID,
CONF_INCLUDE_EXPOSED_CONTEXT,
CONF_WAKE_WORD,
CONF_WAKE_WORD_ENABLED,
@@ -54,6 +55,7 @@ from .const import (
CONF_BROWSER_VOICE_LANGUAGE,
CONF_VOICE_PROVIDER,
CONF_THINKING_TIMEOUT,
CONF_USER_AGENT_MAP,
BROWSER_VOICE_LANGUAGES,
CONTEXT_STRATEGY_CLEAR,
CONTEXT_STRATEGY_TRUNCATE,
@@ -75,6 +77,13 @@ from .const import (
DOMAIN,
OPENCLAW_CONFIG_REL_PATH,
)
from .routing import (
DEFAULT_CHAT_AGENT_ID,
DEFAULT_HA_USER_AGENT_MAP,
normalize_optional_text,
parse_user_agent_map,
serialize_user_agent_map,
)
_LOGGER = logging.getLogger(__name__)
@@ -456,11 +465,35 @@ class OpenClawOptionsFlow(OptionsFlowWithReload):
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Manage integration options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
errors: dict[str, str] = {}
options = self._config_entry.options
if user_input is not None:
try:
user_agent_map = parse_user_agent_map(
user_input.get(CONF_USER_AGENT_MAP)
)
except ValueError:
errors[CONF_USER_AGENT_MAP] = "invalid_user_agent_map"
else:
fallback_agent_id = normalize_optional_text(
user_input.get(CONF_FALLBACK_AGENT_ID)
) or DEFAULT_CHAT_AGENT_ID
new_options = dict(self._config_entry.options)
new_options.update(user_input)
new_options[CONF_FALLBACK_AGENT_ID] = fallback_agent_id
new_options[CONF_USER_AGENT_MAP] = serialize_user_agent_map(
user_agent_map
)
return self.async_create_entry(title="", data=new_options)
options = dict(self._config_entry.options)
if user_input is not None and errors:
options.update(user_input)
selected_provider = options.get(CONF_VOICE_PROVIDER, DEFAULT_VOICE_PROVIDER)
user_agent_map_default = options.get(
CONF_USER_AGENT_MAP,
serialize_user_agent_map(DEFAULT_HA_USER_AGENT_MAP),
)
schema: dict[Any, Any] = {
vol.Optional(
@@ -470,6 +503,14 @@ class OpenClawOptionsFlow(OptionsFlowWithReload):
self._config_entry.data.get(CONF_AGENT_ID, DEFAULT_AGENT_ID),
),
): str,
vol.Optional(
CONF_FALLBACK_AGENT_ID,
default=options.get(CONF_FALLBACK_AGENT_ID, DEFAULT_CHAT_AGENT_ID),
): str,
vol.Optional(
CONF_USER_AGENT_MAP,
default=user_agent_map_default,
): str,
vol.Optional(
CONF_VOICE_AGENT_ID,
default=options.get(
@@ -557,4 +598,8 @@ class OpenClawOptionsFlow(OptionsFlowWithReload):
)
] = vol.In(BROWSER_VOICE_LANGUAGES)
return self.async_show_form(step_id="init", data_schema=vol.Schema(schema))
return self.async_show_form(
step_id="init",
data_schema=vol.Schema(schema),
errors=errors,
)