diff --git a/homeassistant/components/google_generative_ai_conversation/config_flow.py b/homeassistant/components/google_generative_ai_conversation/config_flow.py index 2782122feae..4a1a6448ce7 100644 --- a/homeassistant/components/google_generative_ai_conversation/config_flow.py +++ b/homeassistant/components/google_generative_ai_conversation/config_flow.py @@ -14,6 +14,7 @@ import voluptuous as vol from homeassistant.config_entries import ( SOURCE_REAUTH, ConfigEntry, + ConfigEntryState, ConfigFlow, ConfigFlowResult, ConfigSubentryFlow, @@ -192,6 +193,10 @@ class ConversationSubentryFlowHandler(ConfigSubentryFlow): self, user_input: dict[str, Any] | None = None ) -> SubentryFlowResult: """Set conversation options.""" + # abort if entry is not loaded + if self._get_entry().state != ConfigEntryState.LOADED: + return self.async_abort(reason="entry_not_loaded") + errors: dict[str, str] = {} if user_input is None: diff --git a/homeassistant/components/google_generative_ai_conversation/strings.json b/homeassistant/components/google_generative_ai_conversation/strings.json index df3011d399a..e81ee7e9648 100644 --- a/homeassistant/components/google_generative_ai_conversation/strings.json +++ b/homeassistant/components/google_generative_ai_conversation/strings.json @@ -54,6 +54,7 @@ } }, "abort": { + "entry_not_loaded": "Cannot add things while the configuration is disabled.", "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]" }, "error": { diff --git a/tests/components/google_generative_ai_conversation/test_config_flow.py b/tests/components/google_generative_ai_conversation/test_config_flow.py index 7202526f405..6d02fbb7686 100644 --- a/tests/components/google_generative_ai_conversation/test_config_flow.py +++ b/tests/components/google_generative_ai_conversation/test_config_flow.py @@ -129,8 +129,6 @@ async def test_creating_conversation_subentry( mock_config_entry: MockConfigEntry, ) -> None: """Test creating a conversation subentry.""" - mock_config_entry.add_to_hass(hass) - with patch( "google.genai.models.AsyncModels.list", return_value=get_models_pager(), @@ -163,6 +161,26 @@ async def test_creating_conversation_subentry( assert result2["data"] == processed_options +async def test_creating_conversation_subentry_not_loaded( + hass: HomeAssistant, + mock_init_component: None, + mock_config_entry: MockConfigEntry, +) -> None: + """Test creating a conversation subentry.""" + await hass.config_entries.async_unload(mock_config_entry.entry_id) + with patch( + "google.genai.models.AsyncModels.list", + return_value=get_models_pager(), + ): + 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" + + def will_options_be_rendered_again(current_options, new_options) -> bool: """Determine if options will be rendered again.""" return current_options.get(CONF_RECOMMENDED) != new_options.get(CONF_RECOMMENDED)