8000 Speed up bond setup with gather by bdraco · Pull Request #48454 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Speed up bond setup with gather #48454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions homeassistant/components/bond/utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
"""Reusable utilities for the Bond component."""
from __future__ import annotations

import asyncio
import logging
from typing import Any, cast

from aiohttp import ClientResponseError
from bond_api import Action, Bond

from homeassistant.util.async_ import gather_with_concurrency

from .const import BRIDGE_MAKE

MAX_REQUESTS = 6

_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -114,15 +117,25 @@ async def setup(self, max_devices: int | None = None) -> None:
# Fetch all available devices using Bond API.
device_ids = await self.bond.devices()
self._devices = []
setup_device_ids = []
tasks = []
for idx, device_id in enumerate(device_ids):
if max_devices is not None and idx >= max_devices:
break

device, props = await asyncio.gather(
self.bond.device(device_id), self.bond.device_properties(device_id)
setup_device_ids.append(device_id)
tasks.extend(
[self.bond.device(device_id), self.bond.device_properties(device_id)]
)

self._devices.append(BondDevice(device_id, device, props))
responses = await gather_with_concurrency(MAX_REQUESTS, *tasks)
response_idx = 0
for device_id in setup_device_ids:
self._devices.append(
BondDevice(
device_id, responses[response_idx], responses[response_idx + 1]
)
)
response_idx += 2

_LOGGER.debug("Discovered Bond devices: %s", self._devices)
try:
Expand Down
0