diff --git a/homeassistant/components/openai_conversation/__init__.py b/homeassistant/components/openai_conversation/__init__.py index 71effe83884..a5b13ded375 100644 --- a/homeassistant/components/openai_conversation/__init__.py +++ b/homeassistant/components/openai_conversation/__init__.py @@ -19,7 +19,7 @@ from openai.types.responses import ( ) import voluptuous as vol -from homeassistant.config_entries import ConfigEntry +from homeassistant.config_entries import ConfigEntry, ConfigSubentry from homeassistant.const import CONF_API_KEY, Platform from homeassistant.core import ( HomeAssistant, @@ -32,7 +32,12 @@ from homeassistant.exceptions import ( HomeAssistantError, ServiceValidationError, ) -from homeassistant.helpers import config_validation as cv, selector +from homeassistant.helpers import ( + config_validation as cv, + device_registry as dr, + entity_registry as er, + selector, +) from homeassistant.helpers.httpx_client import get_async_client from homeassistant.helpers.typing import ConfigType @@ -73,6 +78,7 @@ def encode_file(file_path: str) -> tuple[str, str]: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up OpenAI Conversation.""" + await async_migrate_integration(hass) async def render_image(call: ServiceCall) -> ServiceResponse: """Render an image with dall-e.""" @@ -118,7 +124,21 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: translation_placeholders={"config_entry": entry_id}, ) - model: str = entry.options.get(CONF_CHAT_MODEL, RECOMMENDED_CHAT_MODEL) + # Get first conversation subentry for options + conversation_subentry = next( + ( + sub + for sub in entry.subentries.values() + if sub.subentry_type == "conversation" + ), + None, + ) + if not conversation_subentry: + raise ServiceValidationError("No conversation configuration found") + + model: str = conversation_subentry.data.get( + CONF_CHAT_MODEL, RECOMMENDED_CHAT_MODEL + ) client: openai.AsyncClient = entry.runtime_data content: ResponseInputMessageContentListParam = [ @@ -169,11 +189,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: model_args = { "model": model, "input": messages, - "max_output_tokens": entry.options.get( + "max_output_tokens": conversation_subentry.data.get( CONF_MAX_TOKENS, RECOMMENDED_MAX_TOKENS ), - "top_p": entry.options.get(CONF_TOP_P, RECOMMENDED_TOP_P), - "temperature": entry.options.get( + "top_p": conversation_subentry.data.get(CONF_TOP_P, RECOMMENDED_TOP_P), + "temperature": conversation_subentry.data.get( CONF_TEMPERATURE, RECOMMENDED_TEMPERATURE ), "user": call.context.user_id, @@ -182,7 +202,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: if model.startswith("o"): model_args["reasoning"] = { - "effort": entry.options.get( + "effort": conversation_subentry.data.get( CONF_REASONING_EFFORT, RECOMMENDED_REASONING_EFFORT ) } @@ -269,3 +289,68 @@ async def async_setup_entry(hass: HomeAssistant, entry: OpenAIConfigEntry) -> bo async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload OpenAI.""" return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) + + +async def async_migrate_integration(hass: HomeAssistant) -> None: + """Migrate integration entry structure.""" + + entries = hass.config_entries.async_entries(DOMAIN) + if not any(entry.version == 1 for entry in entries): + return + + api_keys_entries: dict[str, ConfigEntry] = {} + entity_registry = er.async_get(hass) + device_registry = dr.async_get(hass) + + for entry in entries: + use_existing = False + subentry = ConfigSubentry( + data=entry.options, + subentry_type="conversation", + title=entry.title, + unique_id=None, + ) + if entry.data[CONF_API_KEY] not in api_keys_entries: + use_existing = True + api_keys_entries[entry.data[CONF_API_KEY]] = entry + + parent_entry = api_keys_entries[entry.data[CONF_API_KEY]] + + hass.config_entries.async_add_subentry(parent_entry, subentry) + conversation_entity = entity_registry.async_get_entity_id( + "conversation", + DOMAIN, + entry.entry_id, + ) + if conversation_entity is not None: + entity_registry.async_update_entity( + conversation_entity, + config_entry_id=parent_entry.entry_id, + config_subentry_id=subentry.subentry_id, + new_unique_id=subentry.subentry_id, + ) + + device = device_registry.async_get_device( + identifiers={(DOMAIN, entry.entry_id)} + ) + if device is not None: + device_registry.async_update_device( + device.id, + new_identifiers={(DOMAIN, subentry.subentry_id)}, + add_config_subentry_id=subentry.subentry_id, + add_config_entry_id=parent_entry.entry_id, + ) + if parent_entry.entry_id != entry.entry_id: + device_registry.async_update_device( + device.id, + remove_config_entry_id=entry.entry_id, + ) + + if not use_existing: + await hass.config_entries.async_remove(entry.entry_id) + else: + hass.config_entries.async_update_entry( + entry, + options={}, + version=2, + ) diff --git a/homeassistant/components/openai_conversation/config_flow.py b/homeassistant/components/openai_conversation/config_flow.py index 60d81bf6745..a9a444cf3dd 100644 --- a/homeassistant/components/openai_conversation/config_flow.py +++ b/homeassistant/components/openai_conversation/config_flow.py @@ -13,17 +13,20 @@ from voluptuous_openapi import convert from homeassistant.components.zone import ENTITY_ID_HOME from homeassistant.config_entries import ( ConfigEntry, + ConfigEntryState, ConfigFlow, ConfigFlowResult, - OptionsFlow, + ConfigSubentryFlow, + SubentryFlowResult, ) from homeassistant.const import ( ATTR_LATITUDE, ATTR_LONGITUDE, CONF_API_KEY, CONF_LLM_HASS_API, + CONF_NAME, ) -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import llm from homeassistant.helpers.httpx_client import get_async_client from homeassistant.helpers.selector import ( @@ -52,6 +55,7 @@ from .const import ( CONF_WEB_SEARCH_REGION, CONF_WEB_SEARCH_TIMEZONE, CONF_WEB_SEARCH_USER_LOCATION, + DEFAULT_CONVERSATION_NAME, DOMAIN, RECOMMENDED_CHAT_MODEL, RECOMMENDED_MAX_TOKENS, @@ -94,7 +98,7 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> None: class OpenAIConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for OpenAI Conversation.""" - VERSION = 1 + VERSION = 2 async def async_step_user( self, user_input: dict[str, Any] | None = None @@ -107,6 +111,7 @@ class OpenAIConfigFlow(ConfigFlow, domain=DOMAIN): errors: dict[str, str] = {} + self._async_abort_entries_match(user_input) try: await validate_input(self.hass, user_input) except openai.APIConnectionError: @@ -120,32 +125,61 @@ class OpenAIConfigFlow(ConfigFlow, domain=DOMAIN): return self.async_create_entry( title="ChatGPT", data=user_input, - options=RECOMMENDED_OPTIONS, + subentries=[ + { + "subentry_type": "conversation", + "data": RECOMMENDED_OPTIONS, + "title": DEFAULT_CONVERSATION_NAME, + "unique_id": None, + } + ], ) return self.async_show_form( step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors ) - @staticmethod - def async_get_options_flow( - config_entry: ConfigEntry, - ) -> OptionsFlow: - """Create the options flow.""" - return OpenAIOptionsFlow(config_entry) + @classmethod + @callback + def async_get_supported_subentry_types( + cls, config_entry: ConfigEntry + ) -> dict[str, type[ConfigSubentryFlow]]: + """Return subentries supported by this integration.""" + return {"conversation": ConversationSubentryFlowHandler} -class OpenAIOptionsFlow(OptionsFlow): - """OpenAI config flow options handler.""" +class ConversationSubentryFlowHandler(ConfigSubentryFlow): + """Flow for managing conversation subentries.""" - def __init__(self, config_entry: ConfigEntry) -> None: - """Initialize options flow.""" - self.options = config_entry.options.copy() + last_rendered_recommended = False + options: dict[str, Any] + + @property + def _is_new(self) -> bool: + """Return if this is a new subentry.""" + return self.source == "user" + + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> SubentryFlowResult: + """Add a subentry.""" + self.options = RECOMMENDED_OPTIONS.copy() + return await self.async_step_init() + + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> SubentryFlowResult: + """Handle reconfiguration of a subentry.""" + self.options = self._get_reconfigure_subentry().data.copy() + return await self.async_step_init() async def async_step_init( self, user_input: dict[str, Any] | None = None - ) -> ConfigFlowResult: + ) -> SubentryFlowResult: """Manage initial options.""" + # abort if entry is not loaded + if self._get_entry().state != ConfigEntryState.LOADED: + return self.async_abort(reason="entry_not_loaded") options = self.options hass_apis: list[SelectOptionDict] = [ @@ -160,25 +194,47 @@ class OpenAIOptionsFlow(OptionsFlow): ): options[CONF_LLM_HASS_API] = [suggested_llm_apis] - step_schema: VolDictType = { - vol.Optional( - CONF_PROMPT, - description={"suggested_value": llm.DEFAULT_INSTRUCTIONS_PROMPT}, - ): TemplateSelector(), - vol.Optional(CONF_LLM_HASS_API): SelectSelector( - SelectSelectorConfig(options=hass_apis, multiple=True) - ), - vol.Required( - CONF_RECOMMENDED, default=options.get(CONF_RECOMMENDED, False) - ): bool, - } + step_schema: VolDictType = {} + + if self._is_new: + step_schema[vol.Required(CONF_NAME, default=DEFAULT_CONVERSATION_NAME)] = ( + str + ) + + step_schema.update( + { + vol.Optional( + CONF_PROMPT, + description={ + "suggested_value": options.get( + CONF_PROMPT, llm.DEFAULT_INSTRUCTIONS_PROMPT + ) + }, + ): TemplateSelector(), + vol.Optional(CONF_LLM_HASS_API): SelectSelector( + SelectSelectorConfig(options=hass_apis, multiple=True) + ), + vol.Required( + CONF_RECOMMENDED, default=options.get(CONF_RECOMMENDED, False) + ): bool, + } + ) if user_input is not None: if not user_input.get(CONF_LLM_HASS_API): user_input.pop(CONF_LLM_HASS_API, None) if user_input[CONF_RECOMMENDED]: - return self.async_create_entry(title="", data=user_input) + if self._is_new: + return self.async_create_entry( + title=user_input.pop(CONF_NAME), + data=user_input, + ) + return self.async_update_and_abort( + self._get_entry(), + self._get_reconfigure_subentry(), + data=user_input, + ) options.update(user_input) if CONF_LLM_HASS_API in options and CONF_LLM_HASS_API not in user_input: @@ -194,7 +250,7 @@ class OpenAIOptionsFlow(OptionsFlow): async def async_step_advanced( self, user_input: dict[str, Any] | None = None - ) -> ConfigFlowResult: + ) -> SubentryFlowResult: """Manage advanced options.""" options = self.options errors: dict[str, str] = {} @@ -236,7 +292,7 @@ class OpenAIOptionsFlow(OptionsFlow): async def async_step_model( self, user_input: dict[str, Any] | None = None - ) -> ConfigFlowResult: + ) -> SubentryFlowResult: """Manage model-specific options.""" options = self.options errors: dict[str, str] = {} @@ -303,7 +359,16 @@ class OpenAIOptionsFlow(OptionsFlow): } if not step_schema: - return self.async_create_entry(title="", data=options) + if self._is_new: + return self.async_create_entry( + title=options.pop(CONF_NAME, DEFAULT_CONVERSATION_NAME), + data=options, + ) + return self.async_update_and_abort( + self._get_entry(), + self._get_reconfigure_subentry(), + data=options, + ) if user_input is not None: if user_input.get(CONF_WEB_SEARCH): @@ -316,7 +381,16 @@ class OpenAIOptionsFlow(OptionsFlow): options.pop(CONF_WEB_SEARCH_TIMEZONE, None) options.update(user_input) - return self.async_create_entry(title="", data=options) + if self._is_new: + return self.async_create_entry( + title=options.pop(CONF_NAME, DEFAULT_CONVERSATION_NAME), + data=options, + ) + return self.async_update_and_abort( + self._get_entry(), + self._get_reconfigure_subentry(), + data=options, + ) return self.async_show_form( step_id="model", @@ -332,7 +406,7 @@ class OpenAIOptionsFlow(OptionsFlow): zone_home = self.hass.states.get(ENTITY_ID_HOME) if zone_home is not None: client = openai.AsyncOpenAI( - api_key=self.config_entry.data[CONF_API_KEY], + api_key=self._get_entry().data[CONF_API_KEY], http_client=get_async_client(self.hass), ) location_schema = vol.Schema( diff --git a/homeassistant/components/openai_conversation/const.py b/homeassistant/components/openai_conversation/const.py index f022b4840eb..f90c05eed79 100644 --- a/homeassistant/components/openai_conversation/const.py +++ b/homeassistant/components/openai_conversation/const.py @@ -5,6 +5,8 @@ import logging DOMAIN = "openai_conversation" LOGGER: logging.Logger = logging.getLogger(__package__) +DEFAULT_CONVERSATION_NAME = "OpenAI Conversation" + CONF_CHAT_MODEL = "chat_model" CONF_FILENAMES = "filenames" CONF_MAX_TOKENS = "max_tokens" diff --git a/homeassistant/components/openai_conversation/conversation.py b/homeassistant/components/openai_conversation/conversation.py index 8fea4613ce0..e63bbf32c35 100644 --- a/homeassistant/components/openai_conversation/conversation.py +++ b/homeassistant/components/openai_conversation/conversation.py @@ -34,7 +34,7 @@ from openai.types.responses.web_search_tool_param import UserLocation from voluptuous_openapi import convert from homeassistant.components import assist_pipeline, conversation -from homeassistant.config_entries import ConfigEntry +from homeassistant.config_entries import ConfigEntry, ConfigSubentry from homeassistant.const import CONF_LLM_HASS_API, MATCH_ALL from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -76,8 +76,14 @@ async def async_setup_entry( async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up conversation entities.""" - agent = OpenAIConversationEntity(config_entry) - async_add_entities([agent]) + for subentry in config_entry.subentries.values(): + if subentry.subentry_type != "conversation": + continue + + async_add_entities( + [OpenAIConversationEntity(config_entry, subentry)], + config_subentry_id=subentry.subentry_id, + ) def _format_tool( @@ -229,22 +235,22 @@ class OpenAIConversationEntity( ): """OpenAI conversation agent.""" - _attr_has_entity_name = True - _attr_name = None _attr_supports_streaming = True - def __init__(self, entry: OpenAIConfigEntry) -> None: + def __init__(self, entry: OpenAIConfigEntry, subentry: ConfigSubentry) -> None: """Initialize the agent.""" self.entry = entry - self._attr_unique_id = entry.entry_id + self.subentry = subentry + self._attr_name = subentry.title + self._attr_unique_id = subentry.subentry_id self._attr_device_info = dr.DeviceInfo( - identifiers={(DOMAIN, entry.entry_id)}, - name=entry.title, + identifiers={(DOMAIN, subentry.subentry_id)}, + name=subentry.title, manufacturer="OpenAI", - model="ChatGPT", + model=entry.data.get(CONF_CHAT_MODEL, RECOMMENDED_CHAT_MODEL), entry_type=dr.DeviceEntryType.SERVICE, ) - if self.entry.options.get(CONF_LLM_HASS_API): + if self.subentry.data.get(CONF_LLM_HASS_API): self._attr_supported_features = ( conversation.ConversationEntityFeature.CONTROL ) @@ -276,7 +282,7 @@ class OpenAIConversationEntity( chat_log: conversation.ChatLog, ) -> conversation.ConversationResult: """Process the user input and call the API.""" - options = self.entry.options + options = self.subentry.data try: await chat_log.async_provide_llm_data( @@ -304,7 +310,7 @@ class OpenAIConversationEntity( chat_log: conversation.ChatLog, ) -> None: """Generate an answer for the chat log.""" - options = self.entry.options + options = self.subentry.data tools: list[ToolParam] | None = None if chat_log.llm_api: diff --git a/homeassistant/components/openai_conversation/strings.json b/homeassistant/components/openai_conversation/strings.json index 351e82ec11f..ffbe84337b7 100644 --- a/homeassistant/components/openai_conversation/strings.json +++ b/homeassistant/components/openai_conversation/strings.json @@ -11,47 +11,63 @@ "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", "unknown": "[%key:common::config_flow::error::unknown%]" + }, + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_service%]" } }, - "options": { - "step": { - "init": { - "data": { - "prompt": "Instructions", - "llm_hass_api": "[%key:common::config_flow::data::llm_hass_api%]", - "recommended": "Recommended model settings" + "config_subentries": { + "conversation": { + "initiate_flow": { + "user": "Add conversation agent", + "reconfigure": "Reconfigure conversation agent" + }, + "entry_type": "Conversation agent", + + "step": { + "init": { + "data": { + "name": "[%key:common::config_flow::data::name%]", + "prompt": "Instructions", + "llm_hass_api": "[%key:common::config_flow::data::llm_hass_api%]", + "recommended": "Recommended model settings" + }, + "data_description": { + "prompt": "Instruct how the LLM should respond. This can be a template." + } }, - "data_description": { - "prompt": "Instruct how the LLM should respond. This can be a template." + "advanced": { + "title": "Advanced settings", + "data": { + "chat_model": "[%key:common::generic::model%]", + "max_tokens": "Maximum tokens to return in response", + "temperature": "Temperature", + "top_p": "Top P" + } + }, + "model": { + "title": "Model-specific options", + "data": { + "reasoning_effort": "Reasoning effort", + "web_search": "Enable web search", + "search_context_size": "Search context size", + "user_location": "Include home location" + }, + "data_description": { + "reasoning_effort": "How many reasoning tokens the model should generate before creating a response to the prompt", + "web_search": "Allow the model to search the web for the latest information before generating a response", + "search_context_size": "High level guidance for the amount of context window space to use for the search", + "user_location": "Refine search results based on geography" + } } }, - "advanced": { - "title": "Advanced settings", - "data": { - "chat_model": "[%key:common::generic::model%]", - "max_tokens": "Maximum tokens to return in response", - "temperature": "Temperature", - "top_p": "Top P" - } + "abort": { + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]", + "entry_not_loaded": "Cannot add things while the configuration is disabled." }, - "model": { - "title": "Model-specific options", - "data": { - "reasoning_effort": "Reasoning effort", - "web_search": "Enable web search", - "search_context_size": "Search context size", - "user_location": "Include home location" - }, - "data_description": { - "reasoning_effort": "How many reasoning tokens the model should generate before creating a response to the prompt", - "web_search": "Allow the model to search the web for the latest information before generating a response", - "search_context_size": "High level guidance for the amount of context window space to use for the search", - "user_location": "Refine search results based on geography" - } + "error": { + "model_not_supported": "This model is not supported, please select a different model" } - }, - "error": { - "model_not_supported": "This model is not supported, please select a different model" } }, "selector": { diff --git a/tests/components/openai_conversation/conftest.py b/tests/components/openai_conversation/conftest.py index 4639d0dc8e0..aa17c333a79 100644 --- a/tests/components/openai_conversation/conftest.py +++ b/tests/components/openai_conversation/conftest.py @@ -4,6 +4,7 @@ from unittest.mock import patch import pytest +from homeassistant.components.openai_conversation.const import DEFAULT_CONVERSATION_NAME from homeassistant.const import CONF_LLM_HASS_API from homeassistant.core import HomeAssistant from homeassistant.helpers import llm @@ -21,6 +22,15 @@ def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry: data={ "api_key": "bla", }, + version=2, + subentries_data=[ + { + "data": {}, + "subentry_type": "conversation", + "title": DEFAULT_CONVERSATION_NAME, + "unique_id": None, + } + ], ) entry.add_to_hass(hass) return entry @@ -31,8 +41,10 @@ def mock_config_entry_with_assist( hass: HomeAssistant, mock_config_entry: MockConfigEntry ) -> MockConfigEntry: """Mock a config entry with assist.""" - hass.config_entries.async_update_entry( - mock_config_entry, options={CONF_LLM_HASS_API: llm.LLM_API_ASSIST} + hass.config_entries.async_update_subentry( + mock_config_entry, + next(iter(mock_config_entry.subentries.values())), + data={CONF_LLM_HASS_API: llm.LLM_API_ASSIST}, ) return mock_config_entry diff --git a/tests/components/openai_conversation/snapshots/test_conversation.ambr b/tests/components/openai_conversation/snapshots/test_conversation.ambr index 0f874969aff..48ad0878b2f 100644 --- a/tests/components/openai_conversation/snapshots/test_conversation.ambr +++ b/tests/components/openai_conversation/snapshots/test_conversation.ambr @@ -6,7 +6,7 @@ 'role': 'user', }), dict({ - 'agent_id': 'conversation.openai', + 'agent_id': 'conversation.openai_conversation', 'content': None, 'role': 'assistant', 'tool_calls': list([ @@ -20,14 +20,14 @@ ]), }), dict({ - 'agent_id': 'conversation.openai', + 'agent_id': 'conversation.openai_conversation', 'role': 'tool_result', 'tool_call_id': 'call_call_1', 'tool_name': 'test_tool', 'tool_result': 'value1', }), dict({ - 'agent_id': 'conversation.openai', + 'agent_id': 'conversation.openai_conversation', 'content': None, 'role': 'assistant', 'tool_calls': list([ @@ -41,14 +41,14 @@ ]), }), dict({ - 'agent_id': 'conversation.openai', + 'agent_id': 'conversation.openai_conversation', 'role': 'tool_result', 'tool_call_id': 'call_call_2', 'tool_name': 'test_tool', 'tool_result': 'value2', }), dict({ - 'agent_id': 'conversation.openai', + 'agent_id': 'conversation.openai_conversation', 'content': 'Cool', 'role': 'assistant', 'tool_calls': None, @@ -62,7 +62,7 @@ 'role': 'user', }), dict({ - 'agent_id': 'conversation.openai', + 'agent_id': 'conversation.openai_conversation', 'content': None, 'role': 'assistant', 'tool_calls': list([ @@ -76,14 +76,14 @@ ]), }), dict({ - 'agent_id': 'conversation.openai', + 'agent_id': 'conversation.openai_conversation', 'role': 'tool_result', 'tool_call_id': 'call_call_1', 'tool_name': 'test_tool', 'tool_result': 'value1', }), dict({ - 'agent_id': 'conversation.openai', + 'agent_id': 'conversation.openai_conversation', 'content': 'Cool', 'role': 'assistant', 'tool_calls': None, diff --git a/tests/components/openai_conversation/test_config_flow.py b/tests/components/openai_conversation/test_config_flow.py index ad5bbffaed3..b77542fbab3 100644 --- a/tests/components/openai_conversation/test_config_flow.py +++ b/tests/components/openai_conversation/test_config_flow.py @@ -24,12 +24,13 @@ from homeassistant.components.openai_conversation.const import ( CONF_WEB_SEARCH_REGION, CONF_WEB_SEARCH_TIMEZONE, CONF_WEB_SEARCH_USER_LOCATION, + DEFAULT_CONVERSATION_NAME, DOMAIN, RECOMMENDED_CHAT_MODEL, RECOMMENDED_MAX_TOKENS, RECOMMENDED_TOP_P, ) -from homeassistant.const import CONF_LLM_HASS_API +from homeassistant.const import CONF_API_KEY, CONF_LLM_HASS_API from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -72,42 +73,132 @@ async def test_form(hass: HomeAssistant) -> None: assert result2["data"] == { "api_key": "bla", } - assert result2["options"] == RECOMMENDED_OPTIONS + assert result2["options"] == {} + assert result2["subentries"] == [ + { + "subentry_type": "conversation", + "data": RECOMMENDED_OPTIONS, + "title": DEFAULT_CONVERSATION_NAME, + "unique_id": None, + } + ] assert len(mock_setup_entry.mock_calls) == 1 -async def test_options_recommended( +async def test_duplicate_entry(hass: HomeAssistant) -> None: + """Test we abort on duplicate config entry.""" + MockConfigEntry( + domain=DOMAIN, + data={CONF_API_KEY: "bla"}, + ).add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert not result["errors"] + + with patch( + "homeassistant.components.openai_conversation.config_flow.openai.resources.models.AsyncModels.list", + ): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_API_KEY: "bla", + }, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +async def test_creating_conversation_subentry( + hass: HomeAssistant, + mock_init_component: None, + mock_config_entry: MockConfigEntry, +) -> None: + """Test creating a conversation subentry.""" + mock_config_entry.add_to_hass(hass) + + result = await hass.config_entries.subentries.async_init( + (mock_config_entry.entry_id, "conversation"), + context={"source": config_entries.SOURCE_USER}, + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "init" + assert not result["errors"] + + result2 = await hass.config_entries.subentries.async_configure( + result["flow_id"], + {"name": "My Custom Agent", **RECOMMENDED_OPTIONS}, + ) + await hass.async_block_till_done() + + assert result2["type"] is FlowResultType.CREATE_ENTRY + assert result2["title"] == "My Custom Agent" + + processed_options = RECOMMENDED_OPTIONS.copy() + processed_options[CONF_PROMPT] = processed_options[CONF_PROMPT].strip() + + assert result2["data"] == processed_options + + +async def test_creating_conversation_subentry_not_loaded( + hass: HomeAssistant, + mock_init_component, + mock_config_entry: MockConfigEntry, +) -> None: + """Test creating a conversation subentry when entry is not loaded.""" + await hass.config_entries.async_unload(mock_config_entry.entry_id) + with patch( + "homeassistant.components.openai_conversation.config_flow.openai.resources.models.AsyncModels.list", + return_value=[], + ): + result = await hass.config_entries.subentries.async_init( + (mock_config_entry.entry_id, "conversation"), + context={"source": config_entries.SOURCE_USER}, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "entry_not_loaded" + + +async def test_subentry_recommended( hass: HomeAssistant, mock_config_entry, mock_init_component ) -> None: - """Test the options flow with recommended settings.""" - options_flow = await hass.config_entries.options.async_init( - mock_config_entry.entry_id + """Test the subentry flow with recommended settings.""" + subentry = next(iter(mock_config_entry.subentries.values())) + subentry_flow = await mock_config_entry.start_subentry_reconfigure_flow( + hass, subentry.subentry_id ) - options = await hass.config_entries.options.async_configure( - options_flow["flow_id"], + options = await hass.config_entries.subentries.async_configure( + subentry_flow["flow_id"], { "prompt": "Speak like a pirate", "recommended": True, }, ) await hass.async_block_till_done() - assert options["type"] is FlowResultType.CREATE_ENTRY - assert options["data"]["prompt"] == "Speak like a pirate" + assert options["type"] is FlowResultType.ABORT + assert options["reason"] == "reconfigure_successful" + assert subentry.data["prompt"] == "Speak like a pirate" -async def test_options_unsupported_model( +async def test_subentry_unsupported_model( hass: HomeAssistant, mock_config_entry, mock_init_component ) -> None: - """Test the options form giving error about models not supported.""" - options_flow = await hass.config_entries.options.async_init( - mock_config_entry.entry_id + """Test the subentry form giving error about models not supported.""" + subentry = next(iter(mock_config_entry.subentries.values())) + subentry_flow = await mock_config_entry.start_subentry_reconfigure_flow( + hass, subentry.subentry_id ) - assert options_flow["type"] == FlowResultType.FORM - assert options_flow["step_id"] == "init" + assert subentry_flow["type"] == FlowResultType.FORM + assert subentry_flow["step_id"] == "init" # Configure initial step - options_flow = await hass.config_entries.options.async_configure( - options_flow["flow_id"], + subentry_flow = await hass.config_entries.subentries.async_configure( + subentry_flow["flow_id"], { CONF_RECOMMENDED: False, CONF_PROMPT: "Speak like a pirate", @@ -115,19 +206,19 @@ async def test_options_unsupported_model( }, ) await hass.async_block_till_done() - assert options_flow["type"] == FlowResultType.FORM - assert options_flow["step_id"] == "advanced" + assert subentry_flow["type"] == FlowResultType.FORM + assert subentry_flow["step_id"] == "advanced" # Configure advanced step - options_flow = await hass.config_entries.options.async_configure( - options_flow["flow_id"], + subentry_flow = await hass.config_entries.subentries.async_configure( + subentry_flow["flow_id"], { CONF_CHAT_MODEL: "o1-mini", }, ) await hass.async_block_till_done() - assert options_flow["type"] is FlowResultType.FORM - assert options_flow["errors"] == {"chat_model": "model_not_supported"} + assert subentry_flow["type"] is FlowResultType.FORM + assert subentry_flow["errors"] == {"chat_model": "model_not_supported"} @pytest.mark.parametrize( @@ -494,7 +585,7 @@ async def test_form_invalid_auth(hass: HomeAssistant, side_effect, error) -> Non ), ], ) -async def test_options_switching( +async def test_subentry_switching( hass: HomeAssistant, mock_config_entry, mock_init_component, @@ -502,16 +593,22 @@ async def test_options_switching( new_options, expected_options, ) -> None: - """Test the options form.""" - hass.config_entries.async_update_entry(mock_config_entry, options=current_options) - options = await hass.config_entries.options.async_init(mock_config_entry.entry_id) - assert options["step_id"] == "init" + """Test the subentry form.""" + subentry = next(iter(mock_config_entry.subentries.values())) + hass.config_entries.async_update_subentry( + mock_config_entry, subentry, data=current_options + ) + await hass.async_block_till_done() + subentry_flow = await mock_config_entry.start_subentry_reconfigure_flow( + hass, subentry.subentry_id + ) + assert subentry_flow["step_id"] == "init" for step_options in new_options: - assert options["type"] == FlowResultType.FORM + assert subentry_flow["type"] == FlowResultType.FORM # Test that current options are showed as suggested values: - for key in options["data_schema"].schema: + for key in subentry_flow["data_schema"].schema: if ( isinstance(key.description, dict) and "suggested_value" in key.description @@ -523,38 +620,42 @@ async def test_options_switching( assert key.description["suggested_value"] == current_option # Configure current step - options = await hass.config_entries.options.async_configure( - options["flow_id"], + subentry_flow = await hass.config_entries.subentries.async_configure( + subentry_flow["flow_id"], step_options, ) await hass.async_block_till_done() - assert options["type"] is FlowResultType.CREATE_ENTRY - assert options["data"] == expected_options + assert subentry_flow["type"] is FlowResultType.ABORT + assert subentry_flow["reason"] == "reconfigure_successful" + assert subentry.data == expected_options -async def test_options_web_search_user_location( +async def test_subentry_web_search_user_location( hass: HomeAssistant, mock_config_entry, mock_init_component ) -> None: """Test fetching user location.""" - options = await hass.config_entries.options.async_init(mock_config_entry.entry_id) - assert options["type"] == FlowResultType.FORM - assert options["step_id"] == "init" + subentry = next(iter(mock_config_entry.subentries.values())) + subentry_flow = await mock_config_entry.start_subentry_reconfigure_flow( + hass, subentry.subentry_id + ) + assert subentry_flow["type"] == FlowResultType.FORM + assert subentry_flow["step_id"] == "init" # Configure initial step - options = await hass.config_entries.options.async_configure( - options["flow_id"], + subentry_flow = await hass.config_entries.subentries.async_configure( + subentry_flow["flow_id"], { CONF_RECOMMENDED: False, CONF_PROMPT: "Speak like a pirate", }, ) - assert options["type"] == FlowResultType.FORM - assert options["step_id"] == "advanced" + assert subentry_flow["type"] == FlowResultType.FORM + assert subentry_flow["step_id"] == "advanced" # Configure advanced step - options = await hass.config_entries.options.async_configure( - options["flow_id"], + subentry_flow = await hass.config_entries.subentries.async_configure( + subentry_flow["flow_id"], { CONF_TEMPERATURE: 1.0, CONF_CHAT_MODEL: RECOMMENDED_CHAT_MODEL, @@ -563,8 +664,8 @@ async def test_options_web_search_user_location( }, ) await hass.async_block_till_done() - assert options["type"] == FlowResultType.FORM - assert options["step_id"] == "model" + assert subentry_flow["type"] == FlowResultType.FORM + assert subentry_flow["step_id"] == "model" hass.config.country = "US" hass.config.time_zone = "America/Los_Angeles" @@ -601,8 +702,8 @@ async def test_options_web_search_user_location( ) # Configure model step - options = await hass.config_entries.options.async_configure( - options["flow_id"], + subentry_flow = await hass.config_entries.subentries.async_configure( + subentry_flow["flow_id"], { CONF_WEB_SEARCH: True, CONF_WEB_SEARCH_CONTEXT_SIZE: "medium", @@ -614,8 +715,9 @@ async def test_options_web_search_user_location( mock_create.call_args.kwargs["input"][0]["content"] == "Where are the following" " coordinates located: (37.7749, -122.4194)?" ) - assert options["type"] is FlowResultType.CREATE_ENTRY - assert options["data"] == { + assert subentry_flow["type"] is FlowResultType.ABORT + assert subentry_flow["reason"] == "reconfigure_successful" + assert subentry.data == { CONF_RECOMMENDED: False, CONF_PROMPT: "Speak like a pirate", CONF_TEMPERATURE: 1.0, diff --git a/tests/components/openai_conversation/test_conversation.py b/tests/components/openai_conversation/test_conversation.py index 99559cb3b61..8621465bd14 100644 --- a/tests/components/openai_conversation/test_conversation.py +++ b/tests/components/openai_conversation/test_conversation.py @@ -153,20 +153,18 @@ async def test_entity( mock_init_component, ) -> None: """Test entity properties.""" - state = hass.states.get("conversation.openai") + state = hass.states.get("conversation.openai_conversation") assert state assert state.attributes["supported_features"] == 0 - hass.config_entries.async_update_entry( + hass.config_entries.async_update_subentry( mock_config_entry, - options={ - **mock_config_entry.options, - CONF_LLM_HASS_API: "assist", - }, + next(iter(mock_config_entry.subentries.values())), + data={CONF_LLM_HASS_API: "assist"}, ) await hass.config_entries.async_reload(mock_config_entry.entry_id) - state = hass.states.get("conversation.openai") + state = hass.states.get("conversation.openai_conversation") assert state assert ( state.attributes["supported_features"] @@ -261,7 +259,7 @@ async def test_incomplete_response( "Please tell me a big story", "mock-conversation-id", Context(), - agent_id="conversation.openai", + agent_id="conversation.openai_conversation", ) assert result.response.response_type == intent.IntentResponseType.ERROR, result @@ -285,7 +283,7 @@ async def test_incomplete_response( "please tell me a big story", "mock-conversation-id", Context(), - agent_id="conversation.openai", + agent_id="conversation.openai_conversation", ) assert result.response.response_type == intent.IntentResponseType.ERROR, result @@ -324,7 +322,7 @@ async def test_failed_response( "next natural number please", "mock-conversation-id", Context(), - agent_id="conversation.openai", + agent_id="conversation.openai_conversation", ) assert result.response.response_type == intent.IntentResponseType.ERROR, result @@ -583,7 +581,7 @@ async def test_function_call( "Please call the test function", mock_chat_log.conversation_id, Context(), - agent_id="conversation.openai", + agent_id="conversation.openai_conversation", ) assert mock_create_stream.call_args.kwargs["input"][2] == { @@ -630,7 +628,7 @@ async def test_function_call_without_reasoning( "Please call the test function", mock_chat_log.conversation_id, Context(), - agent_id="conversation.openai", + agent_id="conversation.openai_conversation", ) assert result.response.response_type == intent.IntentResponseType.ACTION_DONE @@ -686,7 +684,7 @@ async def test_function_call_invalid( "Please call the test function", "mock-conversation-id", Context(), - agent_id="conversation.openai", + agent_id="conversation.openai_conversation", ) @@ -720,7 +718,7 @@ async def test_assist_api_tools_conversion( ] await conversation.async_converse( - hass, "hello", None, Context(), agent_id="conversation.openai" + hass, "hello", None, Context(), agent_id="conversation.openai_conversation" ) tools = mock_create_stream.mock_calls[0][2]["tools"] @@ -735,10 +733,12 @@ async def test_web_search( mock_chat_log: MockChatLog, # noqa: F811 ) -> None: """Test web_search_tool.""" - hass.config_entries.async_update_entry( + subentry = next(iter(mock_config_entry.subentries.values())) + hass.config_entries.async_update_subentry( mock_config_entry, - options={ - **mock_config_entry.options, + subentry, + data={ + **subentry.data, CONF_WEB_SEARCH: True, CONF_WEB_SEARCH_CONTEXT_SIZE: "low", CONF_WEB_SEARCH_USER_LOCATION: True, @@ -764,7 +764,7 @@ async def test_web_search( "What's on the latest news?", mock_chat_log.conversation_id, Context(), - agent_id="conversation.openai", + agent_id="conversation.openai_conversation", ) assert mock_create_stream.mock_calls[0][2]["tools"] == [ diff --git a/tests/components/openai_conversation/test_init.py b/tests/components/openai_conversation/test_init.py index b4f816707e9..d209554e8d3 100644 --- a/tests/components/openai_conversation/test_init.py +++ b/tests/components/openai_conversation/test_init.py @@ -15,8 +15,10 @@ from openai.types.responses import Response, ResponseOutputMessage, ResponseOutp import pytest from homeassistant.components.openai_conversation import CONF_FILENAMES +from homeassistant.components.openai_conversation.const import DOMAIN from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError, ServiceValidationError +from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry @@ -536,3 +538,271 @@ async def test_generate_content_service_error( blocking=True, return_response=True, ) + + +async def test_migration_from_v1_to_v2( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, +) -> None: + """Test migration from version 1 to version 2.""" + # Create a v1 config entry with conversation options and an entity + OPTIONS = { + "recommended": True, + "llm_hass_api": ["assist"], + "prompt": "You are a helpful assistant", + "chat_model": "gpt-4o-mini", + } + mock_config_entry = MockConfigEntry( + domain=DOMAIN, + data={"api_key": "1234"}, + options=OPTIONS, + version=1, + title="ChatGPT", + ) + mock_config_entry.add_to_hass(hass) + + device = device_registry.async_get_or_create( + config_entry_id=mock_config_entry.entry_id, + identifiers={(DOMAIN, mock_config_entry.entry_id)}, + name=mock_config_entry.title, + manufacturer="OpenAI", + model="ChatGPT", + entry_type=dr.DeviceEntryType.SERVICE, + ) + entity = entity_registry.async_get_or_create( + "conversation", + DOMAIN, + mock_config_entry.entry_id, + config_entry=mock_config_entry, + device_id=device.id, + suggested_object_id="google_generative_ai_conversation", + ) + + # Run migration + with patch( + "homeassistant.components.openai_conversation.async_setup_entry", + return_value=True, + ): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert mock_config_entry.version == 2 + assert mock_config_entry.data == {"api_key": "1234"} + assert mock_config_entry.options == {} + + assert len(mock_config_entry.subentries) == 1 + + subentry = next(iter(mock_config_entry.subentries.values())) + assert subentry.unique_id is None + assert subentry.title == "ChatGPT" + assert subentry.subentry_type == "conversation" + assert subentry.data == OPTIONS + + migrated_entity = entity_registry.async_get(entity.entity_id) + assert migrated_entity is not None + assert migrated_entity.config_entry_id == mock_config_entry.entry_id + assert migrated_entity.config_subentry_id == subentry.subentry_id + assert migrated_entity.unique_id == subentry.subentry_id + + # Check device migration + assert not device_registry.async_get_device( + identifiers={(DOMAIN, mock_config_entry.entry_id)} + ) + assert ( + migrated_device := device_registry.async_get_device( + identifiers={(DOMAIN, subentry.subentry_id)} + ) + ) + assert migrated_device.identifiers == {(DOMAIN, subentry.subentry_id)} + assert migrated_device.id == device.id + + +async def test_migration_from_v1_to_v2_with_multiple_keys( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, +) -> None: + """Test migration from version 1 to version 2 with different API keys.""" + # Create two v1 config entries with different API keys + options = { + "recommended": True, + "llm_hass_api": ["assist"], + "prompt": "You are a helpful assistant", + "chat_model": "gpt-4o-mini", + } + mock_config_entry = MockConfigEntry( + domain=DOMAIN, + data={"api_key": "1234"}, + options=options, + version=1, + title="ChatGPT 1", + ) + mock_config_entry.add_to_hass(hass) + mock_config_entry_2 = MockConfigEntry( + domain=DOMAIN, + data={"api_key": "12345"}, + options=options, + version=1, + title="ChatGPT 2", + ) + mock_config_entry_2.add_to_hass(hass) + + device = device_registry.async_get_or_create( + config_entry_id=mock_config_entry.entry_id, + identifiers={(DOMAIN, mock_config_entry.entry_id)}, + name=mock_config_entry.title, + manufacturer="OpenAI", + model="ChatGPT 1", + entry_type=dr.DeviceEntryType.SERVICE, + ) + entity_registry.async_get_or_create( + "conversation", + DOMAIN, + mock_config_entry.entry_id, + config_entry=mock_config_entry, + device_id=device.id, + suggested_object_id="chatgpt_1", + ) + + device_2 = device_registry.async_get_or_create( + config_entry_id=mock_config_entry_2.entry_id, + identifiers={(DOMAIN, mock_config_entry_2.entry_id)}, + name=mock_config_entry_2.title, + manufacturer="OpenAI", + model="ChatGPT 2", + entry_type=dr.DeviceEntryType.SERVICE, + ) + entity_registry.async_get_or_create( + "conversation", + DOMAIN, + mock_config_entry_2.entry_id, + config_entry=mock_config_entry_2, + device_id=device_2.id, + suggested_object_id="chatgpt_2", + ) + + # Run migration + with patch( + "homeassistant.components.openai_conversation.async_setup_entry", + return_value=True, + ): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + await hass.async_block_till_done() + + entries = hass.config_entries.async_entries(DOMAIN) + assert len(entries) == 2 + + for idx, entry in enumerate(entries): + assert entry.version == 2 + assert not entry.options + assert len(entry.subentries) == 1 + subentry = list(entry.subentries.values())[0] + assert subentry.subentry_type == "conversation" + assert subentry.data == options + assert subentry.title == f"ChatGPT {idx + 1}" + + dev = device_registry.async_get_device( + identifiers={(DOMAIN, list(entry.subentries.values())[0].subentry_id)} + ) + assert dev is not None + + +async def test_migration_from_v1_to_v2_with_same_keys( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, +) -> None: + """Test migration from version 1 to version 2 with same API keys consolidates entries.""" + # Create two v1 config entries with the same API key + options = { + "recommended": True, + "llm_hass_api": ["assist"], + "prompt": "You are a helpful assistant", + "chat_model": "gpt-4o-mini", + } + mock_config_entry = MockConfigEntry( + domain=DOMAIN, + data={"api_key": "1234"}, + options=options, + version=1, + title="ChatGPT", + ) + mock_config_entry.add_to_hass(hass) + mock_config_entry_2 = MockConfigEntry( + domain=DOMAIN, + data={"api_key": "1234"}, # Same API key + options=options, + version=1, + title="ChatGPT 2", + ) + mock_config_entry_2.add_to_hass(hass) + + device = device_registry.async_get_or_create( + config_entry_id=mock_config_entry.entry_id, + identifiers={(DOMAIN, mock_config_entry.entry_id)}, + name=mock_config_entry.title, + manufacturer="OpenAI", + model="ChatGPT", + entry_type=dr.DeviceEntryType.SERVICE, + ) + entity_registry.async_get_or_create( + "conversation", + DOMAIN, + mock_config_entry.entry_id, + config_entry=mock_config_entry, + device_id=device.id, + suggested_object_id="chatgpt", + ) + + device_2 = device_registry.async_get_or_create( + config_entry_id=mock_config_entry_2.entry_id, + identifiers={(DOMAIN, mock_config_entry_2.entry_id)}, + name=mock_config_entry_2.title, + manufacturer="OpenAI", + model="ChatGPT", + entry_type=dr.DeviceEntryType.SERVICE, + ) + entity_registry.async_get_or_create( + "conversation", + DOMAIN, + mock_config_entry_2.entry_id, + config_entry=mock_config_entry_2, + device_id=device_2.id, + suggested_object_id="chatgpt_2", + ) + + # Run migration + with patch( + "homeassistant.components.openai_conversation.async_setup_entry", + return_value=True, + ): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + await hass.async_block_till_done() + + # Should have only one entry left (consolidated) + entries = hass.config_entries.async_entries(DOMAIN) + assert len(entries) == 1 + + entry = entries[0] + assert entry.version == 2 + assert not entry.options + assert len(entry.subentries) == 2 # Two subentries from the two original entries + + # Check both subentries exist with correct data + subentries = list(entry.subentries.values()) + titles = [sub.title for sub in subentries] + assert "ChatGPT" in titles + assert "ChatGPT 2" in titles + + for subentry in subentries: + assert subentry.subentry_type == "conversation" + assert subentry.data == options + + # Check devices were migrated correctly + dev = device_registry.async_get_device( + identifiers={(DOMAIN, subentry.subentry_id)} + ) + assert dev is not None