Run coroutines as eager tasks in async_run_hass_job (#111683)

* Run coroutines as eager tasks in async_run_hass_job

Note that this does not change async_add_hass_job

Do not merge this. For test run only

* Phase out periodic tasks

* false by default or some tests will block forever, will need to fix each one manually

* kwarg works

* kwarg works

* kwarg works

* fixes

* fix more tests

* fix more tests

* fix lifx

* opensky

* pvpc_hourly_pricing

* adjust more

* adjust more

* smarttub

* adjust more

* adjust more

* adjust more

* adjust more

* adjust

* no eager executor

* zha

* qnap_qsw

* fix more

* fix fix

* docs

* its a wrapper now

* add more coverage

* coverage

* cover all combos

* more fixes

* more fixes

* more fixes

* remaining issues are legit bugs in tests

* make tplink test more predictable

* more fixes

* feedreader

* grind out some more

* make test race safe

* limit first scope to triggers

* one more

* Start tasks eagerly in for async_at_start(ed)

A few of these can avoid being scheduled on the loop
during startup

* fix cloud

* Revert "fix cloud"

This reverts commit 5eb3ce695d.

* fix test to do what start does

* flip flag

* flip flag

* Fix here_travel_time creating many refresh requests at startup

- Each entity would try to refresh the coordinator which
  created many tasks. Move the refresh to a single
  async_at_started

- The tests fired the EVENT_HOMEASSISTANT_START event
  but the code used async_at_started which only worked
  because the tests did not set CoreState to not_running

* fix azure

* remove kw

* remove kw

* rip

* cover

* more rips

* more rips

* more rips
This commit is contained in:
J. Nick Koston
2024-03-11 14:05:08 -10:00
committed by GitHub
parent 53c3e27ed9
commit 620433a79d
17 changed files with 40 additions and 49 deletions

View File

@@ -764,7 +764,6 @@ class HomeAssistant:
self,
hassjob: HassJob[..., Coroutine[Any, Any, _R]],
*args: Any,
eager_start: bool = False,
background: bool = False,
) -> asyncio.Future[_R] | None:
...
@@ -775,7 +774,6 @@ class HomeAssistant:
self,
hassjob: HassJob[..., Coroutine[Any, Any, _R] | _R],
*args: Any,
eager_start: bool = False,
background: bool = False,
) -> asyncio.Future[_R] | None:
...
@@ -785,14 +783,12 @@ class HomeAssistant:
self,
hassjob: HassJob[..., Coroutine[Any, Any, _R] | _R],
*args: Any,
eager_start: bool = False,
background: bool = False,
) -> asyncio.Future[_R] | None:
"""Run a HassJob from within the event loop.
This method must be run in the event loop.
If eager_start is True, coroutine functions will be scheduled eagerly.
If background is True, the task will created as a background task.
hassjob: HassJob
@@ -809,7 +805,7 @@ class HomeAssistant:
return None
return self.async_add_hass_job(
hassjob, *args, eager_start=eager_start, background=background
hassjob, *args, eager_start=True, background=background
)
@overload
@@ -847,7 +843,7 @@ class HomeAssistant:
args: parameters for method to call.
"""
if asyncio.iscoroutine(target):
return self.async_create_task(target)
return self.async_create_task(target, eager_start=True)
# This code path is performance sensitive and uses
# if TYPE_CHECKING to avoid the overhead of constructing
@@ -855,7 +851,7 @@ class HomeAssistant:
# https://github.com/home-assistant/core/pull/71960
if TYPE_CHECKING:
target = cast(Callable[..., Coroutine[Any, Any, _R] | _R], target)
return self.async_run_hass_job(HassJob(target), *args, eager_start=True)
return self.async_run_hass_job(HassJob(target), *args)
def block_till_done(self) -> None:
"""Block until all pending work is done."""
@@ -1369,7 +1365,7 @@ class EventBus:
continue
if run_immediately:
try:
self._hass.async_run_hass_job(job, event, eager_start=True)
self._hass.async_run_hass_job(job, event)
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Error running job: %s", job)
else: