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

@@ -123,9 +123,7 @@ async def test_async_run_hass_job_eager_start_coro_suspends(
async def job_that_suspends():
await asyncio.sleep(0)
task = hass.async_run_hass_job(
ha.HassJob(ha.callback(job_that_suspends)), eager_start=True
)
task = hass.async_run_hass_job(ha.HassJob(ha.callback(job_that_suspends)))
assert not task.done()
assert task in hass._tasks
await task
@@ -169,7 +167,7 @@ async def test_async_add_hass_job_eager_background(hass: HomeAssistant) -> None:
await asyncio.sleep(0)
task = hass.async_add_hass_job(
ha.HassJob(ha.callback(job_that_suspends)), eager_start=True, background=True
ha.HassJob(ha.callback(job_that_suspends)), background=True
)
assert not task.done()
assert task in hass._background_tasks
@@ -184,7 +182,7 @@ async def test_async_run_hass_job_eager_background(hass: HomeAssistant) -> None:
await asyncio.sleep(0)
task = hass.async_run_hass_job(
ha.HassJob(ha.callback(job_that_suspends)), eager_start=True, background=True
ha.HassJob(ha.callback(job_that_suspends)), background=True
)
assert not task.done()
assert task in hass._background_tasks
@@ -200,7 +198,6 @@ async def test_async_run_hass_job_background_synchronous(hass: HomeAssistant) ->
task = hass.async_run_hass_job(
ha.HassJob(ha.callback(job_that_does_not_suspends)),
eager_start=True,
background=True,
)
assert task.done()
@@ -217,7 +214,6 @@ async def test_async_run_hass_job_synchronous(hass: HomeAssistant) -> None:
task = hass.async_run_hass_job(
ha.HassJob(ha.callback(job_that_does_not_suspends)),
eager_start=True,
background=False,
)
assert task.done()
@@ -393,9 +389,7 @@ async def test_async_run_eager_hass_job_calls_callback() -> None:
asyncio.get_running_loop() # ensure we are in the event loop
calls.append(1)
ha.HomeAssistant.async_run_hass_job(
hass, ha.HassJob(ha.callback(job)), eager_start=True
)
ha.HomeAssistant.async_run_hass_job(hass, ha.HassJob(ha.callback(job)))
assert len(calls) == 1
@@ -406,7 +400,7 @@ async def test_async_run_eager_hass_job_calls_coro_function() -> None:
async def job():
pass
ha.HomeAssistant.async_run_hass_job(hass, ha.HassJob(job), eager_start=True)
ha.HomeAssistant.async_run_hass_job(hass, ha.HassJob(job))
assert len(hass.async_add_hass_job.mock_calls) == 1
@@ -2145,6 +2139,20 @@ async def test_async_run_job_starts_tasks_eagerly(hass: HomeAssistant) -> None:
await task
async def test_async_run_job_starts_coro_eagerly(hass: HomeAssistant) -> None:
"""Test async_run_job starts coros eagerly."""
runs = []
async def _test():
runs.append(True)
task = hass.async_run_job(_test())
# No call to hass.async_block_till_done to ensure the task is run eagerly
assert len(runs) == 1
assert task.done()
await task
def test_valid_entity_id() -> None:
"""Test valid entity ID."""
for invalid in [