Skip to content

Commit

Permalink
Ensure bulbs are still reported offline if an update is deferred (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 11, 2021
1 parent dc7a86b commit 2ad0ebe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions flux_led/aiodevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,13 @@ async def async_update(self) -> None:
# then no need to poll except for the interval
# to make sure the device is still responding
if self._protocol.state_push_updates:
self._async_raise_if_offline()
return
elif self._protocol.power_push_updates:
# If the device pushes power updates
# then no need to poll except for the interval
# to make sure the device is still responding
self._async_raise_if_offline()
return
self._last_update_time = now
if self._updates_without_response == MAX_UPDATES_WITHOUT_RESPONSE:
Expand All @@ -208,6 +210,11 @@ async def async_update(self) -> None:
await self._async_send_state_query()
self._updates_without_response += 1

def _async_raise_if_offline(self) -> None:
"""Raise RuntimeError if the bulb is offline."""
if not self.available:
raise RuntimeError("Bulb not responding, too soon to retry")

async def async_set_levels(
self,
r: Optional[int] = None,
Expand Down
19 changes: 19 additions & 0 deletions tests_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,25 @@ def _updated_callback(*args, **kwargs):
await asyncio.sleep(0)
assert len(transport.mock_calls) == 0

transport.reset_mock()
for _ in range(4):
light._last_update_time = aiodevice.NEVER_TIME
await light.async_update()
await asyncio.sleep(0)
assert len(transport.mock_calls) == 4

light._last_update_time = aiodevice.NEVER_TIME
for _ in range(4):
# First failure should keep the device in
# a failure state until we get to an update
# time
with pytest.raises(RuntimeError):
await light.async_update()

# Should not raise now that bulb has recovered
light._last_update_time = aiodevice.NEVER_TIME
await light.async_update()


@pytest.mark.asyncio
async def test_async_scanner(mock_discovery_aio_protocol):
Expand Down

0 comments on commit 2ad0ebe

Please sign in to comment.