From 879e5bc9612d2dbc1645c3d20baa62851641643d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 19 Mar 2024 10:38:27 -1000 Subject: [PATCH] Only wait for import flows in setup of there is a config flow (#113780) --- homeassistant/setup.py | 16 ++++++++++------ tests/components/cloud/test_http_api.py | 3 +++ tests/test_bootstrap.py | 1 + 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/homeassistant/setup.py b/homeassistant/setup.py index f8a123ab797..522e7bd94ac 100644 --- a/homeassistant/setup.py +++ b/homeassistant/setup.py @@ -427,15 +427,19 @@ async def _async_setup_component( # noqa: C901 ) return False - # Flush out async_setup calling create_task. Fragile but covered by test. + if load_translations_task: + await load_translations_task + + if integration.platforms_exists(("config_flow",)): + # If the integration has a config_flow, flush out async_setup calling create_task + # with an asyncio.sleep(0) so we can wait for import flows. + # Fragile but covered by test. await asyncio.sleep(0) await hass.config_entries.flow.async_wait_import_flow_initialized(domain) - if load_translations_task: - await load_translations_task - # Add to components before the entry.async_setup - # call to avoid a deadlock when forwarding platforms - hass.config.components.add(domain) + # Add to components before the entry.async_setup + # call to avoid a deadlock when forwarding platforms + hass.config.components.add(domain) if entries := hass.config_entries.async_entries( domain, include_ignore=False, include_disabled=False diff --git a/tests/components/cloud/test_http_api.py b/tests/components/cloud/test_http_api.py index f7b88fab6c3..269b7b5d0c5 100644 --- a/tests/components/cloud/test_http_api.py +++ b/tests/components/cloud/test_http_api.py @@ -926,6 +926,8 @@ async def test_websocket_update_preferences_alexa_report_state( client = await hass_ws_client(hass) with patch( + "homeassistant.components.cloud.alexa_config.CloudAlexaConfig.async_sync_entities" + ), patch( ( "homeassistant.components.cloud.alexa_config.CloudAlexaConfig" ".async_get_access_token" @@ -941,6 +943,7 @@ async def test_websocket_update_preferences_alexa_report_state( response = await client.receive_json() set_authorized_mock.assert_called_once_with(True) + await hass.async_block_till_done() assert response["success"] diff --git a/tests/test_bootstrap.py b/tests/test_bootstrap.py index 4dc322c5a29..fee058c4b52 100644 --- a/tests/test_bootstrap.py +++ b/tests/test_bootstrap.py @@ -1121,6 +1121,7 @@ async def test_bootstrap_dependencies( # We patch the _import platform method to avoid loading the platform module # to avoid depending on non core components in the tests. mqtt_integration._import_platform = Mock() + mqtt_integration.platforms_exists = Mock(return_value=True) integrations = { "mqtt": {