Skip to content

Commit

Permalink
Drop async_timeout dep (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Jan 1, 2024
1 parent 0cf7a53 commit 8abf4a7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
async_timeout>=4.0.1
bleak>=0.17.0
bleak-retry-connector>=2.9.0
cryptography>=38.0.3
Expand Down
1 change: 0 additions & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pytest-asyncio
pytest-cov
async_timeout>=4.0.1
bleak>=0.17.0
bleak-retry-connector>=3.4.0
cryptography>=38.0.3
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
name="PySwitchbot",
packages=["switchbot", "switchbot.devices", "switchbot.adv_parsers"],
install_requires=[
"async_timeout>=4.0.1",
"bleak>=0.19.0",
"bleak-retry-connector>=3.4.0",
"cryptography>=39.0.0",
Expand Down
25 changes: 21 additions & 4 deletions switchbot/devices/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from typing import Any, Callable, TypeVar, cast
from uuid import UUID

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


def _handle_timeout(fut: asyncio.Future[None]) -> None:
"""Handle a timeout."""
if not fut.done():
fut.set_exception(asyncio.TimeoutError)


class SwitchbotBaseDevice:
"""Base Representation of a Switchbot Device."""

Expand Down Expand Up @@ -451,16 +456,28 @@ async def _execute_command_locked(self, key: str, command: bytes) -> bytes:
assert self._client is not None
assert self._read_char is not None
assert self._write_char is not None
self._notify_future = asyncio.Future()
self._notify_future = self.loop.create_future()
client = self._client

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

async with async_timeout.timeout(5):
timeout = 5
timeout_handle = self.loop.call_at(
self.loop.time() + timeout, _handle_timeout, self._notify_future
)
timeout_expired = False
try:
notify_msg = await self._notify_future
except asyncio.TimeoutError:
timeout_expired = True
raise
finally:
if not timeout_expired:
timeout_handle.cancel()
self._notify_future = None

_LOGGER.debug("%s: Notification received: %s", self.name, notify_msg.hex())
self._notify_future = None

if notify_msg == b"\x07":
_LOGGER.error("Password required")
Expand Down

0 comments on commit 8abf4a7

Please sign in to comment.