From e01be49e840ffdce6ea48ca1f3693a5c3ec0d269 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 23 Jul 2023 12:36:41 -0500 Subject: [PATCH] Fix slow power state change (#393) --- flux_led/aiodevice.py | 8 ++++---- flux_led/aioprotocol.py | 26 ++++++++++++++------------ tests_aio.py | 1 + 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/flux_led/aiodevice.py b/flux_led/aiodevice.py index c46d921d..b63f570b 100644 --- a/flux_led/aiodevice.py +++ b/flux_led/aiodevice.py @@ -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 diff --git a/flux_led/aioprotocol.py b/flux_led/aioprotocol.py index a0f1ff6d..70725fb5 100644 --- a/flux_led/aioprotocol.py +++ b/flux_led/aioprotocol.py @@ -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: @@ -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) diff --git a/tests_aio.py b/tests_aio.py index 90b59fa8..1f918cb8 100644 --- a/tests_aio.py +++ b/tests_aio.py @@ -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):