Skip to content

Commit d82631e

Browse files
committed
Switch to asyncio.timeout if possible
1 parent 90d4b72 commit d82631e

File tree

6 files changed

+54
-25
lines changed

6 files changed

+54
-25
lines changed

tests/api/test_request.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import sys
12
import asyncio
23
import logging
34

45
import pytest
5-
import async_timeout
6+
7+
if sys.version_info[:2] < (3, 11):
8+
from async_timeout import timeout as asyncio_timeout # pragma: no cover
9+
else:
10+
from asyncio import timeout as asyncio_timeout # pragma: no cover
611

712
import zigpy_znp.types as t
813
import zigpy_znp.config as conf
@@ -66,7 +71,7 @@ async def test_cleanup_timeout_external(connected_znp):
6671

6772
# This request will timeout because we didn't send anything back
6873
with pytest.raises(asyncio.TimeoutError):
69-
async with async_timeout.timeout(0.1):
74+
async with asyncio_timeout(0.1):
7075
await znp.request(c.UTIL.TimeAlive.Req())
7176

7277
# We should be cleaned up
@@ -80,7 +85,7 @@ async def test_callback_rsp_cleanup_timeout_external(connected_znp):
8085

8186
# This request will timeout because we didn't send anything back
8287
with pytest.raises(asyncio.TimeoutError):
83-
async with async_timeout.timeout(0.1):
88+
async with asyncio_timeout(0.1):
8489
await znp.request_callback_rsp(
8590
request=c.UTIL.TimeAlive.Req(),
8691
callback=c.SYS.ResetInd.Callback(partial=True),
@@ -262,7 +267,7 @@ async def test_znp_sreq_srsp(connected_znp):
262267

263268
# Each SREQ must have a corresponding SRSP, so this will fail
264269
with pytest.raises(asyncio.TimeoutError):
265-
async with async_timeout.timeout(0.5):
270+
async with asyncio_timeout(0.5):
266271
await znp.request(c.SYS.Ping.Req())
267272

268273
# This will work

tests/api/test_response.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import sys
12
import asyncio
23

34
import pytest
4-
import async_timeout
5+
6+
if sys.version_info[:2] < (3, 11):
7+
from async_timeout import timeout as asyncio_timeout # pragma: no cover
8+
else:
9+
from asyncio import timeout as asyncio_timeout # pragma: no cover
510

611
import zigpy_znp.types as t
712
import zigpy_znp.commands as c
@@ -61,7 +66,7 @@ async def send_soon(delay):
6166

6267
asyncio.create_task(send_soon(0.1))
6368

64-
async with async_timeout.timeout(0.5):
69+
async with asyncio_timeout(0.5):
6570
assert (await znp.wait_for_response(c.SYS.Ping.Rsp(partial=True))) == response
6671

6772
# The response was successfully received so we should have no outstanding listeners
@@ -71,7 +76,7 @@ async def send_soon(delay):
7176
asyncio.create_task(send_soon(0.6))
7277

7378
with pytest.raises(asyncio.TimeoutError):
74-
async with async_timeout.timeout(0.5):
79+
async with asyncio_timeout(0.5):
7580
assert (
7681
await znp.wait_for_response(c.SYS.Ping.Rsp(partial=True))
7782
) == response

zigpy_znp/api.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
import sys
45
import time
56
import typing
67
import asyncio
@@ -12,7 +13,12 @@
1213
from collections import Counter, defaultdict
1314

1415
import zigpy.state
15-
import async_timeout
16+
17+
if sys.version_info[:2] < (3, 11):
18+
from async_timeout import timeout as asyncio_timeout # pragma: no cover
19+
else:
20+
from asyncio import timeout as asyncio_timeout # pragma: no cover
21+
1622
import zigpy.zdo.types as zdo_t
1723
import zigpy.exceptions
1824
from zigpy.exceptions import NetworkNotFormed
@@ -296,7 +302,7 @@ async def start_network(self):
296302
)
297303

298304
# Both versions still end with this callback
299-
async with async_timeout.timeout(STARTUP_TIMEOUT):
305+
async with asyncio_timeout(STARTUP_TIMEOUT):
300306
await started_as_coordinator
301307
except asyncio.TimeoutError as e:
302308
raise zigpy.exceptions.FormationFailure(
@@ -666,7 +672,7 @@ async def ping_task():
666672

667673
# First, just try pinging
668674
try:
669-
async with async_timeout.timeout(CONNECT_PING_TIMEOUT):
675+
async with asyncio_timeout(CONNECT_PING_TIMEOUT):
670676
return await self.request(c.SYS.Ping.Req())
671677
except asyncio.TimeoutError:
672678
pass
@@ -682,7 +688,7 @@ async def ping_task():
682688
# At this point we have nothing left to try
683689
while True:
684690
try:
685-
async with async_timeout.timeout(2 * CONNECT_PING_TIMEOUT):
691+
async with asyncio_timeout(2 * CONNECT_PING_TIMEOUT):
686692
return await self.request(c.SYS.Ping.Req())
687693
except asyncio.TimeoutError:
688694
pass
@@ -691,7 +697,7 @@ async def ping_task():
691697
ping_task = asyncio.create_task(ping_task())
692698

693699
try:
694-
async with async_timeout.timeout(CONNECT_PROBE_TIMEOUT):
700+
async with asyncio_timeout(CONNECT_PROBE_TIMEOUT):
695701
result = await responses.get()
696702
except Exception:
697703
ping_task.cancel()
@@ -706,7 +712,7 @@ async def ping_task():
706712
LOGGER.debug("Giving ping task %0.2fs to finish", CONNECT_PING_TIMEOUT)
707713

708714
try:
709-
async with async_timeout.timeout(CONNECT_PING_TIMEOUT):
715+
async with asyncio_timeout(CONNECT_PING_TIMEOUT):
710716
result = await ping_task # type:ignore[misc]
711717
except asyncio.TimeoutError:
712718
ping_task.cancel()
@@ -1066,7 +1072,7 @@ async def request(
10661072
self._uart.send(frame)
10671073

10681074
# We should get a SRSP in a reasonable amount of time
1069-
async with async_timeout.timeout(
1075+
async with asyncio_timeout(
10701076
timeout or self._znp_config[conf.CONF_SREQ_TIMEOUT]
10711077
):
10721078
# We lock until either a sync response is seen or an error occurs
@@ -1108,7 +1114,7 @@ async def request_callback_rsp(
11081114
# Typical request/response/callbacks are not backgrounded
11091115
if not background:
11101116
try:
1111-
async with async_timeout.timeout(timeout):
1117+
async with asyncio_timeout(timeout):
11121118
await self.request(request, timeout=timeout, **response_params)
11131119

11141120
return await callback_rsp
@@ -1119,7 +1125,7 @@ async def request_callback_rsp(
11191125
start_time = time.monotonic()
11201126

11211127
try:
1122-
async with async_timeout.timeout(timeout):
1128+
async with asyncio_timeout(timeout):
11231129
request_rsp = await self.request(request, **response_params)
11241130
except Exception:
11251131
# If the SREQ/SRSP pair fails, we must cancel the AREQ listener
@@ -1131,7 +1137,7 @@ async def request_callback_rsp(
11311137
# the timeout
11321138
async def callback_catcher(timeout):
11331139
try:
1134-
async with async_timeout.timeout(timeout):
1140+
async with asyncio_timeout(timeout):
11351141
await callback_rsp
11361142
finally:
11371143
self.remove_listener(listener)

zigpy_znp/tools/flash_read.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
import asyncio
33
import logging
44

5-
import async_timeout
5+
if sys.version_info[:2] < (3, 11):
6+
from async_timeout import timeout as asyncio_timeout # pragma: no cover
7+
else:
8+
from asyncio import timeout as asyncio_timeout # pragma: no cover
69

710
import zigpy_znp.commands as c
811
from zigpy_znp.api import ZNP
@@ -14,7 +17,7 @@
1417

1518
async def read_firmware(znp: ZNP) -> bytearray:
1619
try:
17-
async with async_timeout.timeout(5):
20+
async with asyncio_timeout(5):
1821
handshake_rsp = await znp.request_callback_rsp(
1922
request=c.UBL.HandshakeReq.Req(),
2023
callback=c.UBL.HandshakeRsp.Callback(partial=True),

zigpy_znp/tools/flash_write.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import asyncio
55
import logging
66

7-
import async_timeout
7+
if sys.version_info[:2] < (3, 11):
8+
from async_timeout import timeout as asyncio_timeout # pragma: no cover
9+
else:
10+
from asyncio import timeout as asyncio_timeout # pragma: no cover
11+
812

913
import zigpy_znp.types as t
1014
import zigpy_znp.commands as c
@@ -70,7 +74,7 @@ async def write_firmware(znp: ZNP, firmware: bytes, reset_nvram: bool):
7074
)
7175

7276
try:
73-
async with async_timeout.timeout(5):
77+
async with asyncio_timeout(5):
7478
handshake_rsp = await znp.request_callback_rsp(
7579
request=c.UBL.HandshakeReq.Req(),
7680
callback=c.UBL.HandshakeRsp.Callback(partial=True),

zigpy_znp/zigbee/application.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
import sys
45
import asyncio
56
import logging
67

@@ -11,7 +12,12 @@
1112
import zigpy.types
1213
import zigpy.config
1314
import zigpy.device
14-
import async_timeout
15+
16+
if sys.version_info[:2] < (3, 11):
17+
from async_timeout import timeout as asyncio_timeout # pragma: no cover
18+
else:
19+
from asyncio import timeout as asyncio_timeout # pragma: no cover
20+
1521
import zigpy.profiles
1622
import zigpy.zdo.types as zdo_t
1723
import zigpy.application
@@ -64,7 +70,7 @@ def __init__(self, config: conf.ConfigType):
6470
@classmethod
6571
async def probe(cls, device_config):
6672
try:
67-
async with async_timeout.timeout(PROBE_TIMEOUT):
73+
async with asyncio_timeout(PROBE_TIMEOUT):
6874
return await super().probe(device_config)
6975
except asyncio.TimeoutError:
7076
return False
@@ -609,7 +615,7 @@ async def _set_led_mode(self, *, led: t.uint8_t, mode: c.util.LEDMode) -> None:
609615

610616
# XXX: If Z-Stack is not compiled with HAL_LED, it will just not respond at all
611617
try:
612-
async with async_timeout.timeout(0.5):
618+
async with asyncio_timeout(0.5):
613619
await self._znp.request(
614620
c.UTIL.LEDControl.Req(LED=led, Mode=mode),
615621
RspStatus=t.Status.SUCCESS,
@@ -813,7 +819,7 @@ async def _send_request_raw(
813819
# Broadcasts and ZDO requests will not receive a confirmation
814820
await self._znp.request(request=request, RspStatus=t.Status.SUCCESS)
815821
else:
816-
async with async_timeout.timeout(
822+
async with asyncio_timeout(
817823
EXTENDED_DATA_CONFIRM_TIMEOUT
818824
if extended_timeout
819825
else DATA_CONFIRM_TIMEOUT

0 commit comments

Comments
 (0)