From 6abb8ae273cbc02264d63bfdf6bc66a6ff44c99a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 27 Feb 2024 12:43:32 -1000 Subject: [PATCH] Migrate setup to use eager tasks (#111619) --- homeassistant/setup.py | 11 +++++++---- tests/components/cloudflare/test_init.py | 1 + tests/components/hyperion/test_light.py | 2 ++ tests/components/rfxtrx/conftest.py | 1 + tests/components/roborock/test_init.py | 1 + tests/components/synology_dsm/test_init.py | 1 + tests/components/wiz/test_init.py | 1 + 7 files changed, 14 insertions(+), 4 deletions(-) diff --git a/homeassistant/setup.py b/homeassistant/setup.py index 0e3c78285e2..0cdbcec3ff3 100644 --- a/homeassistant/setup.py +++ b/homeassistant/setup.py @@ -29,6 +29,7 @@ from .helpers import translation from .helpers.issue_registry import IssueSeverity, async_create_issue from .helpers.typing import ConfigType, EventType from .util import ensure_unique_string +from .util.async_ import create_eager_task _LOGGER = logging.getLogger(__name__) @@ -192,7 +193,7 @@ async def _async_process_dependencies( dependencies_tasks = { dep: setup_futures.get(dep) - or hass.loop.create_task( + or create_eager_task( async_setup_component(hass, dep, config), name=f"setup {dep} as dependency of {integration.domain}", ) @@ -352,7 +353,7 @@ async def _async_setup_component( # noqa: C901 # loaded since we try to load them in bootstrap ahead of time. # If for some reason the background task in bootstrap was too slow # or the integration was added after bootstrap, we will load them here. - load_translations_task = asyncio.create_task( + load_translations_task = create_eager_task( translation.async_load_integrations(hass, integration_set) ) @@ -433,7 +434,7 @@ async def _async_setup_component( # noqa: C901 ): await asyncio.gather( *( - asyncio.create_task( + create_eager_task( entry.async_setup(hass, integration=integration), name=f"config entry setup {entry.title} {entry.domain} {entry.entry_id}", ) @@ -569,7 +570,9 @@ def _async_when_setup( _LOGGER.exception("Error handling when_setup callback for %s", component) if component in hass.config.components: - hass.async_create_task(when_setup(), f"when setup {component}") + hass.async_create_task( + when_setup(), f"when setup {component}", eager_start=True + ) return listeners: list[CALLBACK_TYPE] = [] diff --git a/tests/components/cloudflare/test_init.py b/tests/components/cloudflare/test_init.py index d1c9cb3c352..02001f83995 100644 --- a/tests/components/cloudflare/test_init.py +++ b/tests/components/cloudflare/test_init.py @@ -66,6 +66,7 @@ async def test_async_setup_raises_entry_auth_failed( instance.list_zones.side_effect = pycfdns.AuthenticationException() await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() assert entry.state is ConfigEntryState.SETUP_ERROR diff --git a/tests/components/hyperion/test_light.py b/tests/components/hyperion/test_light.py index 01cc1c7d9d2..4715441a5de 100644 --- a/tests/components/hyperion/test_light.py +++ b/tests/components/hyperion/test_light.py @@ -705,6 +705,7 @@ async def test_setup_entry_no_token_reauth(hass: HomeAssistant) -> None: "homeassistant.components.hyperion.client.HyperionClient", return_value=client ), patch.object(hass.config_entries.flow, "async_init") as mock_flow_init: assert not await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() assert client.async_client_disconnect.called mock_flow_init.assert_called_once_with( DOMAIN, @@ -734,6 +735,7 @@ async def test_setup_entry_bad_token_reauth(hass: HomeAssistant) -> None: "homeassistant.components.hyperion.client.HyperionClient", return_value=client ), patch.object(hass.config_entries.flow, "async_init") as mock_flow_init: assert not await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() assert client.async_client_disconnect.called mock_flow_init.assert_called_once_with( DOMAIN, diff --git a/tests/components/rfxtrx/conftest.py b/tests/components/rfxtrx/conftest.py index 6ca7bdd7250..8208c1138c0 100644 --- a/tests/components/rfxtrx/conftest.py +++ b/tests/components/rfxtrx/conftest.py @@ -60,6 +60,7 @@ async def setup_rfx_test_cfg( await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() await hass.async_start() + await hass.async_block_till_done() return mock_entry diff --git a/tests/components/roborock/test_init.py b/tests/components/roborock/test_init.py index e037d81786e..dd85861d53c 100644 --- a/tests/components/roborock/test_init.py +++ b/tests/components/roborock/test_init.py @@ -130,6 +130,7 @@ async def test_reauth_started( side_effect=RoborockInvalidCredentials(), ): await async_setup_component(hass, DOMAIN, {}) + await hass.async_block_till_done() assert mock_roborock_entry.state is ConfigEntryState.SETUP_ERROR flows = hass.config_entries.flow.async_progress() assert len(flows) == 1 diff --git a/tests/components/synology_dsm/test_init.py b/tests/components/synology_dsm/test_init.py index 8f66044a66b..7e86b28f0ee 100644 --- a/tests/components/synology_dsm/test_init.py +++ b/tests/components/synology_dsm/test_init.py @@ -69,4 +69,5 @@ async def test_reauth_triggered(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) assert not await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() mock_async_step_reauth.assert_called_once() diff --git a/tests/components/wiz/test_init.py b/tests/components/wiz/test_init.py index 96ae5e20ba3..41871381d3c 100644 --- a/tests/components/wiz/test_init.py +++ b/tests/components/wiz/test_init.py @@ -31,6 +31,7 @@ async def test_setup_retry(hass: HomeAssistant) -> None: bulb.getMac = AsyncMock(return_value=FAKE_MAC) with _patch_discovery(), _patch_wizlight(device=bulb): + await hass.async_block_till_done() async_fire_time_changed(hass, utcnow() + datetime.timedelta(minutes=15)) await hass.async_block_till_done() assert entry.state is config_entries.ConfigEntryState.LOADED