11from __future__ import annotations
22
33import os
4+ import sys
45import time
56import typing
67import asyncio
1213from collections import Counter , defaultdict
1314
1415import 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+
1622import zigpy .zdo .types as zdo_t
1723import zigpy .exceptions
1824from 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 )
0 commit comments