Skip to content
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

Limit parallel requests to Philips Hue #29189

Merged
merged 4 commits into from
Dec 1, 2019
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions homeassistant/components/hue/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self, hass, config_entry, allow_unreachable, allow_groups):
self.available = True
self.authorized = False
self.api = None
self.parallel_updates_semaphore = None

@property
def host(self):
Expand Down Expand Up @@ -77,9 +78,19 @@ async def async_setup(self, tries=0):
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.

Expand Down
10 changes: 5 additions & 5 deletions homeassistant/components/hue/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -434,9 +434,9 @@ async def async_turn_on(self, **kwargs):
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."""
Expand All @@ -457,9 +457,9 @@ async def async_turn_off(self, **kwargs):
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."""
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/hue/sensor_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async def async_update_items(self):
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
Expand Down
4 changes: 4 additions & 0 deletions tests/components/hue/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ async def mock_request(method, path, **kwargs):
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)
Expand Down
4 changes: 4 additions & 0 deletions tests/components/hue/test_sensor_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ async def mock_request(method, path, **kwargs):
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
Expand Down