|
6 | 6 | import importlib.metadata |
7 | 7 | import logging |
8 | 8 | import re |
| 9 | +import sys |
9 | 10 | from typing import Any |
10 | 11 |
|
| 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 | + |
11 | 17 | import zigpy.application |
12 | 18 | import zigpy.config |
13 | 19 | import zigpy.device |
@@ -140,7 +146,8 @@ async def change_loop(): |
140 | 146 | await self._api.change_network_state(target_state) |
141 | 147 |
|
142 | 148 | try: |
143 | | - await asyncio.wait_for(change_loop(), timeout=timeout) |
| 149 | + async with asyncio_timeout(timeout): |
| 150 | + await change_loop() |
144 | 151 | except asyncio.TimeoutError: |
145 | 152 | if target_state != NetworkState.CONNECTED: |
146 | 153 | raise |
@@ -450,7 +457,8 @@ async def send_packet(self, packet): |
450 | 457 | f"Failed to enqueue packet: {ex!r}", ex.status |
451 | 458 | ) |
452 | 459 |
|
453 | | - status = await asyncio.wait_for(req.result, SEND_CONFIRM_TIMEOUT) |
| 460 | + async with asyncio_timeout(SEND_CONFIRM_TIMEOUT): |
| 461 | + status = await req.result |
454 | 462 |
|
455 | 463 | if status != TXStatus.SUCCESS: |
456 | 464 | raise zigpy.exceptions.DeliveryError( |
@@ -550,8 +558,10 @@ async def _reconnect_loop(self) -> None: |
550 | 558 | LOGGER.debug("Reconnecting, attempt %s", attempt) |
551 | 559 |
|
552 | 560 | 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() |
555 | 565 | break |
556 | 566 | except Exception as exc: |
557 | 567 | wait = 2 ** min(attempt, 5) |
|
0 commit comments