mirror of
https://github.com/esphome/aioesphomeapi.git
synced 2025-01-13 20:11:42 +01:00
Speed up timeout implementation in send_message_await_response_complex (#478)
This commit is contained in:
parent
5dd12169f1
commit
bbfa761aa0
@ -596,6 +596,12 @@ class APIConnection:
|
|||||||
for msg_type in msg_types:
|
for msg_type in msg_types:
|
||||||
self._message_handlers.setdefault(msg_type, []).append(on_message)
|
self._message_handlers.setdefault(msg_type, []).append(on_message)
|
||||||
|
|
||||||
|
def _handle_timeout(self, fut: asyncio.Future[None]) -> None:
|
||||||
|
"""Handle a timeout."""
|
||||||
|
if fut.done():
|
||||||
|
return
|
||||||
|
fut.set_exception(asyncio.TimeoutError())
|
||||||
|
|
||||||
async def send_message_await_response_complex(
|
async def send_message_await_response_complex(
|
||||||
self,
|
self,
|
||||||
send_msg: message.Message,
|
send_msg: message.Message,
|
||||||
@ -639,15 +645,15 @@ class APIConnection:
|
|||||||
# We must not await without a finally or
|
# We must not await without a finally or
|
||||||
# the message could fail to be removed if the
|
# the message could fail to be removed if the
|
||||||
# the await is cancelled
|
# the await is cancelled
|
||||||
|
timeout_handle = self._loop.call_later(timeout, self._handle_timeout, fut)
|
||||||
try:
|
try:
|
||||||
async with async_timeout.timeout(timeout):
|
await fut
|
||||||
await fut
|
|
||||||
except asyncio.TimeoutError as err:
|
except asyncio.TimeoutError as err:
|
||||||
raise TimeoutAPIError(
|
raise TimeoutAPIError(
|
||||||
f"Timeout waiting for response for {type(send_msg)} after {timeout}s"
|
f"Timeout waiting for response for {type(send_msg)} after {timeout}s"
|
||||||
) from err
|
) from err
|
||||||
finally:
|
finally:
|
||||||
|
timeout_handle.cancel()
|
||||||
for msg_type in msg_types:
|
for msg_type in msg_types:
|
||||||
with suppress(ValueError):
|
with suppress(ValueError):
|
||||||
self._message_handlers[msg_type].remove(on_message)
|
self._message_handlers[msg_type].remove(on_message)
|
||||||
|
Loading…
Reference in New Issue
Block a user