Skip to content

Commit dc4a1d3

Browse files
bdracopuddly
andauthored
Switch asyncio.wait_for to asyncio_timeout (#228)
* Switch asyncio.wait_for to asyncio_timeout see zigpy/zigpy#1187 * Directly import from the `async_timeout` package --------- Co-authored-by: puddly <[email protected]>
1 parent 09e952b commit dc4a1d3

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

zigpy_deconz/api.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
import enum
88
import functools
99
import logging
10+
import sys
1011
from typing import Any, Callable
1112

13+
if sys.version_info[:2] < (3, 11):
14+
from async_timeout import timeout as asyncio_timeout # pragma: no cover
15+
else:
16+
from asyncio import timeout as asyncio_timeout # pragma: no cover
17+
1218
from zigpy.config import CONF_DEVICE_PATH
1319
import zigpy.exceptions
1420
from zigpy.types import APSStatus, Bool, Channels
@@ -303,7 +309,8 @@ async def _command(self, cmd, *args):
303309
fut = asyncio.Future()
304310
self._awaiting[seq] = fut
305311
try:
306-
return await asyncio.wait_for(fut, timeout=COMMAND_TIMEOUT)
312+
async with asyncio_timeout(COMMAND_TIMEOUT):
313+
return await fut
307314
except asyncio.TimeoutError:
308315
LOGGER.warning(
309316
"No response to '%s' command with seq id '0x%02x'", cmd, seq
@@ -390,7 +397,8 @@ async def probe(cls, device_config: dict[str, Any]) -> bool:
390397
"""Probe port for the device presence."""
391398
api = cls(None, device_config)
392399
try:
393-
await asyncio.wait_for(api._probe(), timeout=PROBE_TIMEOUT)
400+
async with asyncio_timeout(PROBE_TIMEOUT):
401+
await api._probe()
394402
return True
395403
except Exception as exc:
396404
LOGGER.debug(

zigpy_deconz/zigbee/application.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
import importlib.metadata
77
import logging
88
import re
9+
import sys
910
from typing import Any
1011

12+
if sys.version_info[:2] < (3, 11):
13+
from async_timeout import timeout as asyncio_timeout # pragma: no cover
14+
else:
15+
from asyncio import timeout as asyncio_timeout # pragma: no cover
16+
1117
import zigpy.application
1218
import zigpy.config
1319
import zigpy.device
@@ -140,7 +146,8 @@ async def change_loop():
140146
await self._api.change_network_state(target_state)
141147

142148
try:
143-
await asyncio.wait_for(change_loop(), timeout=timeout)
149+
async with asyncio_timeout(timeout):
150+
await change_loop()
144151
except asyncio.TimeoutError:
145152
if target_state != NetworkState.CONNECTED:
146153
raise
@@ -450,7 +457,8 @@ async def send_packet(self, packet):
450457
f"Failed to enqueue packet: {ex!r}", ex.status
451458
)
452459

453-
status = await asyncio.wait_for(req.result, SEND_CONFIRM_TIMEOUT)
460+
async with asyncio_timeout(SEND_CONFIRM_TIMEOUT):
461+
status = await req.result
454462

455463
if status != TXStatus.SUCCESS:
456464
raise zigpy.exceptions.DeliveryError(
@@ -550,8 +558,10 @@ async def _reconnect_loop(self) -> None:
550558
LOGGER.debug("Reconnecting, attempt %s", attempt)
551559

552560
try:
553-
await asyncio.wait_for(self.connect(), timeout=10)
554-
await asyncio.wait_for(self.initialize(), timeout=10)
561+
async with asyncio_timeout(10):
562+
await self.connect()
563+
async with asyncio_timeout(10):
564+
await self.initialize()
555565
break
556566
except Exception as exc:
557567
wait = 2 ** min(attempt, 5)

0 commit comments

Comments
 (0)