From dacfa4b4dcee0a20e2a9f456353ace68813bad9a Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 8 Aug 2023 10:32:35 +0200 Subject: [PATCH] Set up shopping list during onboarding (#97974) Co-authored-by: Simon Lamon <32477463+silamon@users.noreply.github.com> --- homeassistant/components/onboarding/views.py | 7 +++++- .../components/shopping_list/config_flow.py | 25 ++++++++++++++----- tests/components/onboarding/test_views.py | 23 +++++++++++++++++ .../shopping_list/test_config_flow.py | 19 +++++++++++--- 4 files changed, 63 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/onboarding/views.py b/homeassistant/components/onboarding/views.py index 5b47394e0e4..05467e96860 100644 --- a/homeassistant/components/onboarding/views.py +++ b/homeassistant/components/onboarding/views.py @@ -193,7 +193,12 @@ class CoreConfigOnboardingView(_BaseOnboardingView): await self._async_mark_done(hass) # Integrations to set up when finishing onboarding - onboard_integrations = ["google_translate", "met", "radio_browser"] + onboard_integrations = [ + "google_translate", + "met", + "radio_browser", + "shopping_list", + ] # pylint: disable-next=import-outside-toplevel from homeassistant.components import hassio diff --git a/homeassistant/components/shopping_list/config_flow.py b/homeassistant/components/shopping_list/config_flow.py index 23f66cecebb..0637dcea390 100644 --- a/homeassistant/components/shopping_list/config_flow.py +++ b/homeassistant/components/shopping_list/config_flow.py @@ -1,23 +1,36 @@ -"""Config flow to configure ShoppingList component.""" -from homeassistant import config_entries +"""Config flow to configure the shopping list integration.""" +from __future__ import annotations + +from typing import Any + +from homeassistant.config_entries import ConfigFlow +from homeassistant.data_entry_flow import FlowResult from .const import DOMAIN -class ShoppingListFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): - """Config flow for ShoppingList component.""" +class ShoppingListFlowHandler(ConfigFlow, domain=DOMAIN): + """Config flow for the shopping list integration.""" VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: """Handle a flow initialized by the user.""" # Check if already configured await self.async_set_unique_id(DOMAIN) self._abort_if_unique_id_configured() if user_input is not None: - return self.async_create_entry(title="Shopping List", data=user_input) + return self.async_create_entry(title="Shopping list", data={}) return self.async_show_form(step_id="user") async_step_import = async_step_user + + async def async_step_onboarding( + self, _: dict[str, Any] | None = None + ) -> FlowResult: + """Handle a flow initialized by onboarding.""" + return await self.async_step_user(user_input={}) diff --git a/tests/components/onboarding/test_views.py b/tests/components/onboarding/test_views.py index bbe50f9a810..c888381230c 100644 --- a/tests/components/onboarding/test_views.py +++ b/tests/components/onboarding/test_views.py @@ -117,6 +117,8 @@ def mock_default_integrations(): "homeassistant.components.met.async_setup_entry", return_value=True ), patch( "homeassistant.components.radio_browser.async_setup_entry", return_value=True + ), patch( + "homeassistant.components.shopping_list.async_setup_entry", return_value=True ): yield @@ -453,6 +455,27 @@ async def test_onboarding_core_sets_up_met( assert len(hass.config_entries.async_entries("met")) == 1 +async def test_onboarding_core_sets_up_shopping_list( + hass: HomeAssistant, + hass_storage: dict[str, Any], + hass_client: ClientSessionGenerator, + mock_default_integrations, +) -> None: + """Test finishing the core step set up the shopping list.""" + mock_storage(hass_storage, {"done": [const.STEP_USER]}) + + assert await async_setup_component(hass, "onboarding", {}) + await hass.async_block_till_done() + + client = await hass_client() + resp = await client.post("/api/onboarding/core_config") + + assert resp.status == 200 + + await hass.async_block_till_done() + assert len(hass.config_entries.async_entries("shopping_list")) == 1 + + async def test_onboarding_core_sets_up_google_translate( hass: HomeAssistant, hass_storage: dict[str, Any], diff --git a/tests/components/shopping_list/test_config_flow.py b/tests/components/shopping_list/test_config_flow.py index 4f4ea3f2188..34d74d18046 100644 --- a/tests/components/shopping_list/test_config_flow.py +++ b/tests/components/shopping_list/test_config_flow.py @@ -1,8 +1,8 @@ """Test config flow.""" -from homeassistant import data_entry_flow from homeassistant.components.shopping_list.const import DOMAIN from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType async def test_import(hass: HomeAssistant) -> None: @@ -11,7 +11,7 @@ async def test_import(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_IMPORT}, data={} ) - assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY + assert result["type"] == FlowResultType.CREATE_ENTRY async def test_user(hass: HomeAssistant) -> None: @@ -21,7 +21,7 @@ async def test_user(hass: HomeAssistant) -> None: DOMAIN, context={"source": SOURCE_USER} ) - assert result["type"] == data_entry_flow.FlowResultType.FORM + assert result["type"] == FlowResultType.FORM assert result["step_id"] == "user" @@ -32,5 +32,16 @@ async def test_user_confirm(hass: HomeAssistant) -> None: DOMAIN, context={"source": SOURCE_USER}, data={} ) - assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY + assert result["type"] == FlowResultType.CREATE_ENTRY assert result["result"].data == {} + + +async def test_onboarding_flow(hass: HomeAssistant) -> None: + """Test the onboarding configuration flow.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": "onboarding"} + ) + + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["title"] == "Shopping list" + assert result["data"] == {}