From 1772e5257c616b6837fa13fb709110a4ffbcddd0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 6 Mar 2024 15:37:41 -1000 Subject: [PATCH] Move analytics setup to later stage to avoid delaying frontend startup (#112535) Move analytics setup to stage 1 to avoid delaying frontend startup analytics was only needed in the frontend startup phase for onboarding. Its very unlikely the user will be able to complete the onboarding steps and get to the analytics screen before analytics is done loading so we can delay loading it until stage 1. To be absolutely sure that it is ready, the core_config step in onboarding will wait to proceed if it is some how still being setup --- homeassistant/bootstrap.py | 1 + .../components/onboarding/manifest.json | 2 +- homeassistant/components/onboarding/views.py | 26 +++++++++++++------ tests/components/onboarding/test_views.py | 22 ++++++++++++++++ 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 0800586eb85..68f1266317a 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -141,6 +141,7 @@ DEFAULT_INTEGRATIONS = { # These integrations are set up unless recovery mode is activated. # # Integrations providing core functionality: + "analytics", # Needed for onboarding "application_credentials", "backup", "frontend", diff --git a/homeassistant/components/onboarding/manifest.json b/homeassistant/components/onboarding/manifest.json index 466292eb2e0..918d845993a 100644 --- a/homeassistant/components/onboarding/manifest.json +++ b/homeassistant/components/onboarding/manifest.json @@ -3,7 +3,7 @@ "name": "Home Assistant Onboarding", "after_dependencies": ["hassio"], "codeowners": ["@home-assistant/core"], - "dependencies": ["analytics", "auth", "http", "person"], + "dependencies": ["auth", "http", "person"], "documentation": "https://www.home-assistant.io/integrations/onboarding", "integration_type": "system", "quality_scale": "internal" diff --git a/homeassistant/components/onboarding/views.py b/homeassistant/components/onboarding/views.py index e1edfa82a62..9bef34150a2 100644 --- a/homeassistant/components/onboarding/views.py +++ b/homeassistant/components/onboarding/views.py @@ -2,6 +2,7 @@ from __future__ import annotations import asyncio +from collections.abc import Coroutine from http import HTTPStatus from typing import TYPE_CHECKING, Any, cast @@ -20,6 +21,8 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import area_registry as ar from homeassistant.helpers.system_info import async_get_system_info from homeassistant.helpers.translation import async_get_translations +from homeassistant.setup import async_setup_component +from homeassistant.util.async_ import create_eager_task if TYPE_CHECKING: from . import OnboadingStorage @@ -215,15 +218,22 @@ class CoreConfigOnboardingView(_BaseOnboardingView): ): onboard_integrations.append("rpi_power") - # Set up integrations after onboarding - await asyncio.gather( - *( - hass.config_entries.flow.async_init( - domain, context={"source": "onboarding"} - ) - for domain in onboard_integrations + coros: list[Coroutine[Any, Any, Any]] = [ + hass.config_entries.flow.async_init( + domain, context={"source": "onboarding"} ) - ) + for domain in onboard_integrations + ] + + if "analytics" not in hass.config.components: + # If by some chance that analytics has not finished + # setting up, wait for it here so its ready for the + # next step. + coros.append(async_setup_component(hass, "analytics", {})) + + # Set up integrations after onboarding and ensure + # analytics is ready for the next step. + await asyncio.gather(*(create_eager_task(coro) for coro in coros)) return self.json({}) diff --git a/tests/components/onboarding/test_views.py b/tests/components/onboarding/test_views.py index b23f693b230..af9e6f9ebf0 100644 --- a/tests/components/onboarding/test_views.py +++ b/tests/components/onboarding/test_views.py @@ -567,6 +567,28 @@ async def test_onboarding_core_no_rpi_power( assert not rpi_power_state +async def test_onboarding_core_ensures_analytics_loaded( + hass: HomeAssistant, + hass_storage: dict[str, Any], + hass_client: ClientSessionGenerator, + mock_default_integrations, +) -> None: + """Test finishing the core step ensures analytics is ready.""" + mock_storage(hass_storage, {"done": [const.STEP_USER]}) + assert "analytics" not in hass.config.components + + 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 "analytics" in hass.config.components + + async def test_onboarding_analytics( hass: HomeAssistant, hass_storage: dict[str, Any],