From b073d87e0842e69e2b1198b37d2a6eddfc94f212 Mon Sep 17 00:00:00 2001 From: "David F. Mulcahey" Date: Mon, 5 Aug 2019 10:14:19 -0400 Subject: [PATCH] stagger device init to avoid flooding network (#25709) --- homeassistant/components/zha/core/gateway.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/zha/core/gateway.py b/homeassistant/components/zha/core/gateway.py index 5ac02293107..7eff3ebcf3b 100644 --- a/homeassistant/components/zha/core/gateway.py +++ b/homeassistant/components/zha/core/gateway.py @@ -126,10 +126,21 @@ class ZHAGateway: ) init_tasks = [] + semaphore = asyncio.Semaphore(2) + + async def init_with_semaphore(coro, semaphore): + """Don't flood the zigbee network during initialization.""" + async with semaphore: + await coro + for device in self.application_controller.devices.values(): if device.nwk == 0x0000: continue - init_tasks.append(self.async_device_initialized(device, False)) + init_tasks.append( + init_with_semaphore( + self.async_device_initialized(device, False), semaphore + ) + ) await asyncio.gather(*init_tasks) def device_joined(self, device):