Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch asyncio.wait_for to asyncio_timeout #228

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions zigpy_deconz/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
import enum
import functools
import logging
import sys
from typing import Any, Callable

if sys.version_info[:2] < (3, 11):
from async_timeout import timeout as asyncio_timeout # pragma: no cover
else:
from asyncio import timeout as asyncio_timeout # pragma: no cover

from zigpy.config import CONF_DEVICE_PATH
import zigpy.exceptions
from zigpy.types import APSStatus, Bool, Channels
Expand Down Expand Up @@ -303,7 +309,8 @@ async def _command(self, cmd, *args):
fut = asyncio.Future()
self._awaiting[seq] = fut
try:
return await asyncio.wait_for(fut, timeout=COMMAND_TIMEOUT)
async with asyncio_timeout(COMMAND_TIMEOUT):
return await fut
except asyncio.TimeoutError:
LOGGER.warning(
"No response to '%s' command with seq id '0x%02x'", cmd, seq
Expand Down Expand Up @@ -390,7 +397,8 @@ async def probe(cls, device_config: dict[str, Any]) -> bool:
"""Probe port for the device presence."""
api = cls(None, device_config)
try:
await asyncio.wait_for(api._probe(), timeout=PROBE_TIMEOUT)
async with asyncio_timeout(PROBE_TIMEOUT):
await api._probe()
return True
except Exception as exc:
LOGGER.debug(
Expand Down
18 changes: 14 additions & 4 deletions zigpy_deconz/zigbee/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
import importlib.metadata
import logging
import re
import sys
from typing import Any

if sys.version_info[:2] < (3, 11):
from async_timeout import timeout as asyncio_timeout # pragma: no cover
else:
from asyncio import timeout as asyncio_timeout # pragma: no cover

import zigpy.application
import zigpy.config
import zigpy.device
Expand Down Expand Up @@ -140,7 +146,8 @@ async def change_loop():
await self._api.change_network_state(target_state)

try:
await asyncio.wait_for(change_loop(), timeout=timeout)
async with asyncio_timeout(timeout):
await change_loop()
except asyncio.TimeoutError:
if target_state != NetworkState.CONNECTED:
raise
Expand Down Expand Up @@ -450,7 +457,8 @@ async def send_packet(self, packet):
f"Failed to enqueue packet: {ex!r}", ex.status
)

status = await asyncio.wait_for(req.result, SEND_CONFIRM_TIMEOUT)
async with asyncio_timeout(SEND_CONFIRM_TIMEOUT):
status = await req.result

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

try:
await asyncio.wait_for(self.connect(), timeout=10)
await asyncio.wait_for(self.initialize(), timeout=10)
async with asyncio_timeout(10):
await self.connect()
async with asyncio_timeout(10):
await self.initialize()
break
except Exception as exc:
wait = 2 ** min(attempt, 5)
Expand Down
Loading