diff --git a/homeassistant/components/bond/utils.py b/homeassistant/components/bond/utils.py index ade8fd0b91d..60b9a7b492f 100644 --- a/homeassistant/components/bond/utils.py +++ b/homeassistant/components/bond/utils.py @@ -7,7 +7,7 @@ from typing import Any, cast from aiohttp import ClientResponseError from bond_async import Action, Bond, BondType -from homeassistant.util.async_ import gather_with_concurrency +from homeassistant.util.async_ import gather_with_limited_concurrency from .const import BRIDGE_MAKE @@ -163,7 +163,7 @@ class BondHub: ] ) - responses = await gather_with_concurrency(MAX_REQUESTS, *tasks) + responses = await gather_with_limited_concurrency(MAX_REQUESTS, *tasks) response_idx = 0 for device_id in setup_device_ids: self._devices.append( diff --git a/homeassistant/components/ping/device_tracker.py b/homeassistant/components/ping/device_tracker.py index f546bd6bacc..a25b3652b36 100644 --- a/homeassistant/components/ping/device_tracker.py +++ b/homeassistant/components/ping/device_tracker.py @@ -22,7 +22,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.event import async_track_point_in_utc_time from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.util import dt as dt_util -from homeassistant.util.async_ import gather_with_concurrency +from homeassistant.util.async_ import gather_with_limited_concurrency from homeassistant.util.process import kill_subprocess from .const import DOMAIN, ICMP_TIMEOUT, PING_ATTEMPTS_COUNT, PING_PRIVS, PING_TIMEOUT @@ -117,7 +117,7 @@ async def async_setup_scanner( async def async_update(now: datetime) -> None: """Update all the hosts on every interval time.""" - results = await gather_with_concurrency( + results = await gather_with_limited_concurrency( CONCURRENT_PING_LIMIT, *(hass.async_add_executor_job(host.update) for host in hosts), ) diff --git a/homeassistant/components/tile/__init__.py b/homeassistant/components/tile/__init__.py index 29754ffba4b..1e8cebdd5a6 100644 --- a/homeassistant/components/tile/__init__.py +++ b/homeassistant/components/tile/__init__.py @@ -16,7 +16,7 @@ from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import aiohttp_client from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_entries from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed -from homeassistant.util.async_ import gather_with_concurrency +from homeassistant.util.async_ import gather_with_limited_concurrency from .const import DOMAIN, LOGGER @@ -106,7 +106,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ) coordinator_init_tasks.append(coordinator.async_refresh()) - await gather_with_concurrency(DEFAULT_INIT_TASK_LIMIT, *coordinator_init_tasks) + await gather_with_limited_concurrency( + DEFAULT_INIT_TASK_LIMIT, *coordinator_init_tasks + ) hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = TileData(coordinators=coordinators, tiles=tiles) diff --git a/homeassistant/components/wemo/__init__.py b/homeassistant/components/wemo/__init__.py index a58169aa6e5..3f7cbe4cf45 100644 --- a/homeassistant/components/wemo/__init__.py +++ b/homeassistant/components/wemo/__init__.py @@ -16,7 +16,7 @@ from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant, callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.event import async_call_later from homeassistant.helpers.typing import ConfigType -from homeassistant.util.async_ import gather_with_concurrency +from homeassistant.util.async_ import gather_with_limited_concurrency from .const import DOMAIN from .models import WemoConfigEntryData, WemoData, async_wemo_data @@ -217,7 +217,7 @@ class WemoDispatcher: """Consider a platform as loaded and dispatch any backlog of discovered devices.""" self._dispatch_callbacks[platform] = dispatch - await gather_with_concurrency( + await gather_with_limited_concurrency( MAX_CONCURRENCY, *( dispatch(coordinator) @@ -289,7 +289,7 @@ class WemoDiscovery: if not self._static_config: return _LOGGER.debug("Adding statically configured WeMo devices") - for device in await gather_with_concurrency( + for device in await gather_with_limited_concurrency( MAX_CONCURRENCY, *( self._hass.async_add_executor_job(validate_static_config, host, port) diff --git a/homeassistant/helpers/discovery_flow.py b/homeassistant/helpers/discovery_flow.py index 306e8b51d63..c2c9a04b7c3 100644 --- a/homeassistant/helpers/discovery_flow.py +++ b/homeassistant/helpers/discovery_flow.py @@ -8,7 +8,7 @@ from homeassistant.const import EVENT_HOMEASSISTANT_STARTED from homeassistant.core import CoreState, Event, HomeAssistant, callback from homeassistant.data_entry_flow import FlowResult from homeassistant.loader import bind_hass -from homeassistant.util.async_ import gather_with_concurrency +from homeassistant.util.async_ import gather_with_limited_concurrency FLOW_INIT_LIMIT = 2 DISCOVERY_FLOW_DISPATCHER = "discovery_flow_dispatcher" @@ -93,7 +93,7 @@ class FlowDispatcher: for flow_key, flows in pending_flows.items() for flow_values in flows ] - await gather_with_concurrency( + await gather_with_limited_concurrency( FLOW_INIT_LIMIT, *[init_coro for init_coro in init_coros if init_coro is not None], ) diff --git a/homeassistant/util/async_.py b/homeassistant/util/async_.py index bc4cf68bb81..bcc7be62265 100644 --- a/homeassistant/util/async_.py +++ b/homeassistant/util/async_.py @@ -171,7 +171,7 @@ def protect_loop(func: Callable[_P, _R], strict: bool = True) -> Callable[_P, _R return protected_loop_func -async def gather_with_concurrency( +async def gather_with_limited_concurrency( limit: int, *tasks: Any, return_exceptions: bool = False ) -> Any: """Wrap asyncio.gather to limit the number of concurrent tasks. diff --git a/tests/util/test_async.py b/tests/util/test_async.py index 4945e95d2d7..60f86ee7af4 100644 --- a/tests/util/test_async.py +++ b/tests/util/test_async.py @@ -183,8 +183,8 @@ async def test_protect_loop_debugger_sleep(caplog: pytest.LogCaptureFixture) -> assert "Detected blocking call inside the event loop" not in caplog.text -async def test_gather_with_concurrency() -> None: - """Test gather_with_concurrency limits the number of running tasks.""" +async def test_gather_with_limited_concurrency() -> None: + """Test gather_with_limited_concurrency limits the number of running tasks.""" runs = 0 now_time = time.time() @@ -198,7 +198,7 @@ async def test_gather_with_concurrency() -> None: await asyncio.sleep(0.1) return runs - results = await hasync.gather_with_concurrency( + results = await hasync.gather_with_limited_concurrency( 2, *(_increment_runs_if_in_time() for i in range(4)) )