Skip to content

Commit

Permalink
fix: ensure timeouts work with py3.11 (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 7, 2023
1 parent 99e3877 commit 4951aef
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Sphinx = {version = "^5.0", optional = true}
sphinx-rtd-theme = {version = "^1.0", optional = true}
myst-parser = {version = "^0.18", optional = true}
bleak = ">=0.21.0"
async-timeout = ">=4.0.1"
async-timeout = {version = ">=3.0.0", python = "<3.11"}
dbus-fast = {version = ">=1.14.0", markers = "platform_system == \"Linux\""}
bluetooth-adapters = {version = ">=0.15.2", markers = "platform_system == \"Linux\""}

Expand Down
4 changes: 2 additions & 2 deletions src/bleak_retry_connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from collections.abc import Awaitable, Callable
from typing import Any, ParamSpec, TypeVar

import async_timeout
from bleak import BleakClient, BleakScanner
from bleak.backends.device import BLEDevice
from bleak.backends.service import BleakGATTServiceCollection
Expand All @@ -26,6 +25,7 @@
wait_for_disconnect,
)
from .const import IS_LINUX, NO_RSSI_VALUE, RSSI_SWITCH_THRESHOLD
from .util import asyncio_timeout

DISCONNECT_TIMEOUT = 5

Expand Down Expand Up @@ -346,7 +346,7 @@ def _raise_if_needed(name: str, description: str, exc: Exception) -> None:
)

try:
async with async_timeout.timeout(BLEAK_SAFETY_TIMEOUT):
async with asyncio_timeout(BLEAK_SAFETY_TIMEOUT):
await client.connect(
timeout=BLEAK_TIMEOUT,
dangerous_use_bleak_cache=use_services_cache
Expand Down
6 changes: 3 additions & 3 deletions src/bleak_retry_connector/bluez.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from collections.abc import Generator
from typing import Any

import async_timeout
from bleak.backends.device import BLEDevice
from bleak.exc import BleakError

from .const import IS_LINUX, NO_RSSI_VALUE, RSSI_SWITCH_THRESHOLD
from .util import asyncio_timeout

DISCONNECT_TIMEOUT = 5
DBUS_CONNECT_TIMEOUT = 8.5
Expand Down Expand Up @@ -48,7 +48,7 @@ async def get_global_bluez_manager_with_timeout() -> "BlueZManager" | None:
return None

try:
async with async_timeout.timeout(DBUS_CONNECT_TIMEOUT):
async with asyncio_timeout(DBUS_CONNECT_TIMEOUT):
return await get_global_bluez_manager()
except FileNotFoundError as ex:
setattr(get_global_bluez_manager_with_timeout, "_has_dbus_socket", False)
Expand Down Expand Up @@ -304,7 +304,7 @@ async def wait_for_disconnect(device: BLEDevice, min_wait_time: float) -> None:
start = time.monotonic() if min_wait_time else 0
try:
manager = await get_global_bluez_manager()
async with async_timeout.timeout(DISCONNECT_TIMEOUT):
async with asyncio_timeout(DISCONNECT_TIMEOUT):
await manager._wait_condition(device.details["path"], "Connected", False)
end = time.monotonic() if min_wait_time else 0
waited = end - start
Expand Down
5 changes: 3 additions & 2 deletions src/bleak_retry_connector/dbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import contextlib

import async_timeout
from bleak.backends.bluezdbus import defs
from bleak.backends.device import BLEDevice
from dbus_fast.aio.message_bus import MessageBus
from dbus_fast.constants import BusType
from dbus_fast.message import Message

from .util import asyncio_timeout

DISCONNECT_TIMEOUT = 5


Expand All @@ -24,7 +25,7 @@ async def disconnect_devices(devices: list[BLEDevice]) -> None:
bus = await MessageBus(bus_type=BusType.SYSTEM, negotiate_unix_fd=True).connect()
for device in valid_devices:
with contextlib.suppress(Exception):
async with async_timeout.timeout(DISCONNECT_TIMEOUT):
async with asyncio_timeout(DISCONNECT_TIMEOUT):
await bus.call(
Message(
destination=defs.BLUEZ_SERVICE,
Expand Down
8 changes: 8 additions & 0 deletions src/bleak_retry_connector/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

import sys

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

0 comments on commit 4951aef

Please sign in to comment.