Limit parallel requests to Philips Hue (#29189)

* Limit parallel requests to Philips Hue

* Fix tests

* Remove loop

* Update homeassistant/components/hue/bridge.py

Co-Authored-By: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Bram Kragten 2019-12-01 22:24:16 +01:00 committed by Paulus Schoutsen
parent a9baa24fda
commit d91dd68b31
5 changed files with 25 additions and 6 deletions

View File

@ -33,6 +33,7 @@ class HueBridge:
self.available = True
self.authorized = False
self.api = None
self.parallel_updates_semaphore = None
@property
def host(self):
@ -78,9 +79,19 @@ class HueBridge:
DOMAIN, SERVICE_HUE_SCENE, self.hue_activate_scene, schema=SCENE_SCHEMA
)
self.parallel_updates_semaphore = asyncio.Semaphore(
3 if self.api.config.modelid == "BSB001" else 10
)
self.authorized = True
return True
async def async_request_call(self, coro):
"""Process request batched."""
async with self.parallel_updates_semaphore:
return await coro
async def async_reset(self):
"""Reset this bridge to default state.

View File

@ -202,7 +202,7 @@ async def async_update_items(
try:
start = monotonic()
with async_timeout.timeout(4):
await api.update()
await bridge.async_request_call(api.update())
except aiohue.Unauthorized:
await bridge.handle_unauthorized_error()
return
@ -434,9 +434,9 @@ class HueLight(Light):
command["effect"] = "none"
if self.is_group:
await self.light.set_action(**command)
await self.bridge.async_request_call(self.light.set_action(**command))
else:
await self.light.set_state(**command)
await self.bridge.async_request_call(self.light.set_state(**command))
async def async_turn_off(self, **kwargs):
"""Turn the specified or all lights off."""
@ -457,9 +457,9 @@ class HueLight(Light):
command["alert"] = "none"
if self.is_group:
await self.light.set_action(**command)
await self.bridge.async_request_call(self.light.set_action(**command))
else:
await self.light.set_state(**command)
await self.bridge.async_request_call(self.light.set_state(**command))
async def async_update(self):
"""Synchronize state with bridge."""

View File

@ -101,7 +101,7 @@ class SensorManager:
try:
start = monotonic()
with async_timeout.timeout(4):
await api.update()
await self.bridge.async_request_call(api.update())
except Unauthorized:
await self.bridge.handle_unauthorized_error()
return

View File

@ -204,6 +204,10 @@ def mock_bridge(hass):
return bridge.mock_group_responses.popleft()
return None
async def async_request_call(coro):
await coro
bridge.async_request_call = async_request_call
bridge.api.config.apiversion = "9.9.9"
bridge.api.lights = Lights({}, mock_request)
bridge.api.groups = Groups({}, mock_request)

View File

@ -277,6 +277,10 @@ def create_mock_bridge():
return bridge.mock_sensor_responses.popleft()
return None
async def async_request_call(coro):
await coro
bridge.async_request_call = async_request_call
bridge.api.config.apiversion = "9.9.9"
bridge.api.sensors = Sensors({}, mock_request)
return bridge