Skip to content

Commit

Permalink
Fix slow power state change (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jul 23, 2023
1 parent c302593 commit e01be49
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
8 changes: 4 additions & 4 deletions flux_led/aiodevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ async def _async_send_state_query(self) -> None:
async def _async_wait_state_change(
self, futures: List["asyncio.Future[Any]"], state: bool, timeout: float
) -> bool:
done, _ = await asyncio.wait(futures, timeout=timeout)
if done and self.is_on == state:
return True
return False
done, _ = await asyncio.wait(
futures, timeout=timeout, return_when=asyncio.FIRST_COMPLETED
)
return bool(done and self.is_on == state)

async def _async_set_power_state(
self, state: bool, accept_any_power_state_response: bool
Expand Down
26 changes: 14 additions & 12 deletions flux_led/aioprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ def connection_made(self, transport: BaseTransport) -> None:
def write(self, data: bytes) -> None:
"""Write data to the client."""
assert self.transport is not None
_LOGGER.debug(
"%s => %s (%d)",
self.peername,
" ".join(f"0x{x:02X}" for x in data),
len(data),
)
if _LOGGER.isEnabledFor(logging.DEBUG):
_LOGGER.debug(
"%s => %s (%d)",
self.peername,
" ".join(f"0x{x:02X}" for x in data),
len(data),
)
self.transport.write(data)

def close(self) -> None:
Expand All @@ -48,10 +49,11 @@ def close(self) -> None:

def data_received(self, data: bytes) -> None:
"""Process new data from the socket."""
_LOGGER.debug(
"%s <= %s (%d)",
self.peername,
" ".join(f"0x{x:02X}" for x in data),
len(data),
)
if _LOGGER.isEnabledFor(logging.DEBUG):
_LOGGER.debug(
"%s <= %s (%d)",
self.peername,
" ".join(f"0x{x:02X}" for x in data),
len(data),
)
self._data_receive_callback(data)
1 change: 1 addition & 0 deletions tests_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ async def _wait_for_connection():
transport, protocol = await future
await asyncio.sleep(0)
await asyncio.sleep(0)
await asyncio.sleep(0)
return transport, protocol

async def _mock_create_connection(func, ip, port):
Expand Down

0 comments on commit e01be49

Please sign in to comment.