From ca0536c3aa687dbc6614ecc4cb4c2b9e7202346f Mon Sep 17 00:00:00 2001 From: Ruslan Sayfutdinov Date: Mon, 17 May 2021 18:53:27 +0100 Subject: [PATCH 1/3] Define sync hass.create_task function --- homeassistant/components/zeroconf/__init__.py | 8 ++++---- homeassistant/core.py | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/zeroconf/__init__.py b/homeassistant/components/zeroconf/__init__.py index 484d4404a66a6a..fe47604eaa2ab6 100644 --- a/homeassistant/components/zeroconf/__init__.py +++ b/homeassistant/components/zeroconf/__init__.py @@ -368,10 +368,10 @@ def service_update( ): continue - hass.add_job( + hass.create_task( hass.config_entries.flow.async_init( matcher["domain"], context={"source": DOMAIN}, data=info - ) # type: ignore + ) ) _LOGGER.debug("Starting Zeroconf browser") @@ -404,12 +404,12 @@ def handle_homekit( ): continue - hass.add_job( + hass.create_task( hass.config_entries.flow.async_init( homekit_models[test_model], context={"source": config_entries.SOURCE_HOMEKIT}, data=info, - ) # type: ignore + ) ) return True diff --git a/homeassistant/core.py b/homeassistant/core.py index 067afb23c8a415..b1610faad6ed60 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -373,6 +373,13 @@ def async_add_hass_job(self, hassjob: HassJob, *args: Any) -> asyncio.Future | N return task + def create_task(self, target: Coroutine) -> None: + """Add task to the executor pool. + + target: target to call. + """ + self.loop.call_soon_threadsafe(self.async_create_task, target) + @callback def async_create_task(self, target: Coroutine) -> asyncio.tasks.Task: """Create a task from within the eventloop. From 6b4ef6de7ca41cd4e2aa7659a4ea6cc160fe4961 Mon Sep 17 00:00:00 2001 From: Ruslan Sayfutdinov Date: Mon, 17 May 2021 19:14:48 +0100 Subject: [PATCH 2/3] Revert zeroconf change --- homeassistant/components/zeroconf/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/zeroconf/__init__.py b/homeassistant/components/zeroconf/__init__.py index fe47604eaa2ab6..484d4404a66a6a 100644 --- a/homeassistant/components/zeroconf/__init__.py +++ b/homeassistant/components/zeroconf/__init__.py @@ -368,10 +368,10 @@ def service_update( ): continue - hass.create_task( + hass.add_job( hass.config_entries.flow.async_init( matcher["domain"], context={"source": DOMAIN}, data=info - ) + ) # type: ignore ) _LOGGER.debug("Starting Zeroconf browser") @@ -404,12 +404,12 @@ def handle_homekit( ): continue - hass.create_task( + hass.add_job( hass.config_entries.flow.async_init( homekit_models[test_model], context={"source": config_entries.SOURCE_HOMEKIT}, data=info, - ) + ) # type: ignore ) return True From 9180ba0e51130715db9ef0ec83f4bb3dd53a94ae Mon Sep 17 00:00:00 2001 From: Ruslan Sayfutdinov Date: Mon, 17 May 2021 19:42:10 +0100 Subject: [PATCH 3/3] Add test --- tests/test_core.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/test_core.py b/tests/test_core.py index 0a205cedad1048..39c5b310537c39 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -233,6 +233,29 @@ async def wait_finish_callback(): assert len(call_count) == 2 +async def test_async_create_task_pending_tasks_coro(hass): + """Add a coro to pending tasks.""" + call_count = [] + + async def test_coro(): + """Test Coro.""" + call_count.append("call") + + for _ in range(2): + hass.create_task(test_coro()) + + async def wait_finish_callback(): + """Wait until all stuff is scheduled.""" + await asyncio.sleep(0) + await asyncio.sleep(0) + + await wait_finish_callback() + + assert len(hass._pending_tasks) == 2 + await hass.async_block_till_done() + assert len(call_count) == 2 + + async def test_async_add_job_pending_tasks_executor(hass): """Run an executor in pending tasks.""" call_count = []