From 3f0c0a72dbbcb2cdf0efa0bb9f7c48583a35bbdd Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 28 Apr 2024 08:13:51 -0500 Subject: [PATCH] Prevent setup retry from delaying shutdown (#116328) --- homeassistant/config_entries.py | 2 +- .../components/gardena_bluetooth/test_init.py | 2 +- .../specific_devices/test_ecobee3.py | 1 + .../homekit_controller/test_init.py | 6 +++-- tests/components/teslemetry/test_init.py | 2 +- tests/components/wiz/test_init.py | 4 ++-- tests/components/yeelight/test_init.py | 22 +++++++++---------- tests/components/zha/test_init.py | 3 ++- 8 files changed, 23 insertions(+), 19 deletions(-) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index 88230a78428..73e1d8debd6 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -698,7 +698,7 @@ class ConfigEntry: # Check again when we fire in case shutdown # has started so we do not block shutdown if not hass.is_stopping: - hass.async_create_task( + hass.async_create_background_task( self._async_setup_retry(hass), f"config entry retry {self.domain} {self.title}", eager_start=True, diff --git a/tests/components/gardena_bluetooth/test_init.py b/tests/components/gardena_bluetooth/test_init.py index 1f294c6169d..53688846c07 100644 --- a/tests/components/gardena_bluetooth/test_init.py +++ b/tests/components/gardena_bluetooth/test_init.py @@ -57,6 +57,6 @@ async def test_setup_retry( mock_client.read_char.side_effect = original_read_char async_fire_time_changed(hass, utcnow() + timedelta(seconds=10)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert mock_entry.state is ConfigEntryState.LOADED diff --git a/tests/components/homekit_controller/specific_devices/test_ecobee3.py b/tests/components/homekit_controller/specific_devices/test_ecobee3.py index 3f93ca1a896..059993e3bef 100644 --- a/tests/components/homekit_controller/specific_devices/test_ecobee3.py +++ b/tests/components/homekit_controller/specific_devices/test_ecobee3.py @@ -203,6 +203,7 @@ async def test_ecobee3_setup_connection_failure( # We just advance time by 5 minutes so that the retry happens, rather # than manually invoking async_setup_entry. await time_changed(hass, 5 * 60) + await hass.async_block_till_done(wait_background_tasks=True) climate = entity_registry.async_get("climate.homew") assert climate.unique_id == "00:00:00:00:00:00_1_16" diff --git a/tests/components/homekit_controller/test_init.py b/tests/components/homekit_controller/test_init.py index 59fdf555a50..9d2022f6b1c 100644 --- a/tests/components/homekit_controller/test_init.py +++ b/tests/components/homekit_controller/test_init.py @@ -160,7 +160,7 @@ async def test_offline_device_raises(hass: HomeAssistant, controller) -> None: is_connected = True async_fire_time_changed(hass, utcnow() + timedelta(seconds=10)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert config_entry.state is ConfigEntryState.LOADED assert hass.states.get("light.testdevice").state == STATE_OFF @@ -217,16 +217,18 @@ async def test_ble_device_only_checks_is_available( is_available = True async_fire_time_changed(hass, utcnow() + timedelta(seconds=10)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert config_entry.state is ConfigEntryState.LOADED assert hass.states.get("light.testdevice").state == STATE_OFF is_available = False async_fire_time_changed(hass, utcnow() + timedelta(hours=1)) + await hass.async_block_till_done(wait_background_tasks=True) assert hass.states.get("light.testdevice").state == STATE_UNAVAILABLE is_available = True async_fire_time_changed(hass, utcnow() + timedelta(hours=1)) + await hass.async_block_till_done(wait_background_tasks=True) assert hass.states.get("light.testdevice").state == STATE_OFF diff --git a/tests/components/teslemetry/test_init.py b/tests/components/teslemetry/test_init.py index fb405e2ee03..f21a421ed6e 100644 --- a/tests/components/teslemetry/test_init.py +++ b/tests/components/teslemetry/test_init.py @@ -74,7 +74,7 @@ async def test_vehicle_first_refresh( # Wait for the retry freezer.tick(timedelta(seconds=60)) async_fire_time_changed(hass) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) # Verify we have loaded assert entry.state is ConfigEntryState.LOADED diff --git a/tests/components/wiz/test_init.py b/tests/components/wiz/test_init.py index 3fa369c4d9d..c3438aed1b2 100644 --- a/tests/components/wiz/test_init.py +++ b/tests/components/wiz/test_init.py @@ -32,9 +32,9 @@ 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() + await hass.async_block_till_done(wait_background_tasks=True) async_fire_time_changed(hass, utcnow() + datetime.timedelta(minutes=15)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert entry.state is ConfigEntryState.LOADED diff --git a/tests/components/yeelight/test_init.py b/tests/components/yeelight/test_init.py index af442d1c8d0..0bff635fb6e 100644 --- a/tests/components/yeelight/test_init.py +++ b/tests/components/yeelight/test_init.py @@ -69,7 +69,7 @@ async def test_ip_changes_fallback_discovery(hass: HomeAssistant) -> None: assert config_entry.state is ConfigEntryState.SETUP_RETRY async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=2)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) # The discovery should update the ip address assert config_entry.data[CONF_HOST] == IP_ADDRESS @@ -78,7 +78,7 @@ async def test_ip_changes_fallback_discovery(hass: HomeAssistant) -> None: with patch(f"{MODULE}.AsyncBulb", return_value=mocked_bulb), _patch_discovery(): async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert config_entry.state is ConfigEntryState.LOADED binary_sensor_entity_id = ENTITY_BINARY_SENSOR_TEMPLATE.format( @@ -362,7 +362,7 @@ async def test_bulb_off_while_adding_in_ha(hass: HomeAssistant) -> None: _patch_discovery_interval(), ): async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=2)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert config_entry.state is ConfigEntryState.LOADED @@ -380,7 +380,7 @@ async def test_async_listen_error_late_discovery( await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert config_entry.state is ConfigEntryState.SETUP_RETRY await hass.async_block_till_done() @@ -388,7 +388,7 @@ async def test_async_listen_error_late_discovery( with _patch_discovery(), patch(f"{MODULE}.AsyncBulb", return_value=_mocked_bulb()): async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert config_entry.state is ConfigEntryState.LOADED assert config_entry.data[CONF_DETECTED_MODEL] == MODEL @@ -411,7 +411,7 @@ async def test_fail_to_fetch_initial_state( await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert config_entry.state is ConfigEntryState.SETUP_RETRY await hass.async_block_till_done() @@ -419,7 +419,7 @@ async def test_fail_to_fetch_initial_state( with _patch_discovery(), patch(f"{MODULE}.AsyncBulb", return_value=_mocked_bulb()): async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert config_entry.state is ConfigEntryState.LOADED @@ -502,7 +502,7 @@ async def test_async_setup_with_missing_id(hass: HomeAssistant) -> None: assert config_entry.state is ConfigEntryState.SETUP_RETRY assert config_entry.data[CONF_ID] == ID async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=2)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) with ( _patch_discovery(), @@ -511,7 +511,7 @@ async def test_async_setup_with_missing_id(hass: HomeAssistant) -> None: patch(f"{MODULE}.AsyncBulb", return_value=_mocked_bulb()), ): async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=4)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert config_entry.state is ConfigEntryState.LOADED @@ -535,7 +535,7 @@ async def test_async_setup_with_missing_unique_id(hass: HomeAssistant) -> None: assert config_entry.state is ConfigEntryState.SETUP_RETRY assert config_entry.unique_id == ID async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=2)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) with ( _patch_discovery(), @@ -544,7 +544,7 @@ async def test_async_setup_with_missing_unique_id(hass: HomeAssistant) -> None: patch(f"{MODULE}.AsyncBulb", return_value=_mocked_bulb()), ): async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=4)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert config_entry.state is ConfigEntryState.LOADED diff --git a/tests/components/zha/test_init.py b/tests/components/zha/test_init.py index 99d6a78924b..70ba88ee6e7 100644 --- a/tests/components/zha/test_init.py +++ b/tests/components/zha/test_init.py @@ -255,10 +255,11 @@ async def test_zha_retry_unique_ids( lambda hass, delay, action: async_call_later(hass, 0, action), ): await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) # Wait for the config entry setup to retry await asyncio.sleep(0.1) + await hass.async_block_till_done(wait_background_tasks=True) assert len(mock_connect.mock_calls) == 2