Skip to content

Commit

Permalink
Use eager task creation to add entities to entity platform (#111553)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Feb 27, 2024
1 parent 8da2c53 commit a6f4f6e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
5 changes: 4 additions & 1 deletion homeassistant/helpers/entity_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ def _async_schedule_add_entities(
task = self.hass.async_create_task(
self.async_add_entities(new_entities, update_before_add=update_before_add),
f"EntityPlatform async_add_entities {self.domain}.{self.platform_name}",
eager_start=True,
)

if not self._setup_complete:
Expand All @@ -491,6 +492,7 @@ def _async_schedule_add_entities_for_entry(
self.hass,
self.async_add_entities(new_entities, update_before_add=update_before_add),
f"EntityPlatform async_add_entities_for_entry {self.domain}.{self.platform_name}",
eager_start=True,
)

if not self._setup_complete:
Expand Down Expand Up @@ -526,9 +528,10 @@ async def _async_add_and_update_entities(
event loop and will finish faster if we run them concurrently.
"""
results: list[BaseException | None] | None = None
tasks = [create_eager_task(coro) for coro in coros]
try:
async with self.hass.timeout.async_timeout(timeout, self.domain):
results = await asyncio.gather(*coros, return_exceptions=True)
results = await asyncio.gather(*tasks, return_exceptions=True)
except TimeoutError:
self.logger.warning(
"Timed out adding entities for domain %s with platform %s after %ds",
Expand Down
7 changes: 5 additions & 2 deletions tests/components/honeywell/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,18 @@ async def test_remove_stale_device(
(DOMAIN, 7654321) in device.identifiers for device in device_entries_other
)

assert await config_entry.async_unload(hass)
assert await hass.config_entries.async_unload(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state == ConfigEntryState.NOT_LOADED
assert config_entry.state is ConfigEntryState.NOT_LOADED

del location.devices_by_id[another_device.deviceid]

await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
await hass.async_block_till_done()

assert config_entry.state is ConfigEntryState.LOADED

assert (
hass.states.async_entity_ids_count() == 3
) # 1 climate entities; 2 sensor entities
Expand Down
2 changes: 1 addition & 1 deletion tests/components/netatmo/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async def fake_post(*args, **kwargs):

await hass.async_block_till_done()

assert fake_post_hits == 10
assert fake_post_hits >= 8
mock_impl.assert_called_once()
mock_webhook.assert_called_once()

Expand Down

0 comments on commit a6f4f6e

Please sign in to comment.