diff --git a/tests/common.py b/tests/common.py index 3ed0f9e2008..88450d34564 100644 --- a/tests/common.py +++ b/tests/common.py @@ -353,13 +353,13 @@ async def async_test_home_assistant( hass.set_state(CoreState.running) - @callback - def clear_instance(event): + async def clear_instance(event): """Clear global instance.""" + await asyncio.sleep(0) # Give aiohttp one loop iteration to close INSTANCES.remove(hass) hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_CLOSE, clear_instance, run_immediately=False + EVENT_HOMEASSISTANT_CLOSE, clear_instance, run_immediately=True ) yield hass diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index ba02e61f0a7..439ca76d545 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -2506,6 +2506,7 @@ async def test_recursive_automation_starting_script( async def async_automation_triggered(event): """Listen to automation_triggered event from the automation integration.""" automation_triggered.append(event) + await asyncio.sleep(0) # Yield to allow other tasks to run hass.states.async_set("sensor.test", str(len(automation_triggered))) hass.services.async_register("test", "script_done", async_service_handler) @@ -2513,7 +2514,7 @@ async def test_recursive_automation_starting_script( "test", "automation_started", async_service_handler ) hass.bus.async_listen( - "automation_triggered", async_automation_triggered, run_immediately=False + "automation_triggered", async_automation_triggered, run_immediately=True ) hass.bus.async_fire("trigger_automation")