Skip to content

Commit 8abf4a7

Browse files
authored
Drop async_timeout dep (#225)
1 parent 0cf7a53 commit 8abf4a7

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
async_timeout>=4.0.1
21
bleak>=0.17.0
32
bleak-retry-connector>=2.9.0
43
cryptography>=38.0.3

requirements_dev.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
pytest-asyncio
22
pytest-cov
3-
async_timeout>=4.0.1
43
bleak>=0.17.0
54
bleak-retry-connector>=3.4.0
65
cryptography>=38.0.3

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
name="PySwitchbot",
55
packages=["switchbot", "switchbot.devices", "switchbot.adv_parsers"],
66
install_requires=[
7-
"async_timeout>=4.0.1",
87
"bleak>=0.19.0",
98
"bleak-retry-connector>=3.4.0",
109
"cryptography>=39.0.0",

switchbot/devices/device.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from typing import Any, Callable, TypeVar, cast
1111
from uuid import UUID
1212

13-
import async_timeout
1413
from bleak.backends.device import BLEDevice
1514
from bleak.backends.service import BleakGATTCharacteristic, BleakGATTServiceCollection
1615
from bleak.exc import BleakDBusError
@@ -109,6 +108,12 @@ def _merge_data(old_data: dict[str, Any], new_data: dict[str, Any]) -> dict[str,
109108
return merged
110109

111110

111+
def _handle_timeout(fut: asyncio.Future[None]) -> None:
112+
"""Handle a timeout."""
113+
if not fut.done():
114+
fut.set_exception(asyncio.TimeoutError)
115+
116+
112117
class SwitchbotBaseDevice:
113118
"""Base Representation of a Switchbot Device."""
114119

@@ -451,16 +456,28 @@ async def _execute_command_locked(self, key: str, command: bytes) -> bytes:
451456
assert self._client is not None
452457
assert self._read_char is not None
453458
assert self._write_char is not None
454-
self._notify_future = asyncio.Future()
459+
self._notify_future = self.loop.create_future()
455460
client = self._client
456461

457462
_LOGGER.debug("%s: Sending command: %s", self.name, key)
458463
await client.write_gatt_char(self._write_char, command, False)
459464

460-
async with async_timeout.timeout(5):
465+
timeout = 5
466+
timeout_handle = self.loop.call_at(
467+
self.loop.time() + timeout, _handle_timeout, self._notify_future
468+
)
469+
timeout_expired = False
470+
try:
461471
notify_msg = await self._notify_future
472+
except asyncio.TimeoutError:
473+
timeout_expired = True
474+
raise
475+
finally:
476+
if not timeout_expired:
477+
timeout_handle.cancel()
478+
self._notify_future = None
479+
462480
_LOGGER.debug("%s: Notification received: %s", self.name, notify_msg.hex())
463-
self._notify_future = None
464481

465482
if notify_msg == b"\x07":
466483
_LOGGER.error("Password required")

0 commit comments

Comments
 (0)