Browse Source

Drop async_timeout dep (#225)

J. Nick Koston 3 months ago
parent
commit
8abf4a7b0d
4 changed files with 21 additions and 7 deletions
  1. 0 1
      requirements.txt
  2. 0 1
      requirements_dev.txt
  3. 0 1
      setup.py
  4. 21 4
      switchbot/devices/device.py

+ 0 - 1
requirements.txt

@@ -1,4 +1,3 @@
-async_timeout>=4.0.1
 bleak>=0.17.0
 bleak-retry-connector>=2.9.0
 cryptography>=38.0.3

+ 0 - 1
requirements_dev.txt

@@ -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

+ 0 - 1
setup.py

@@ -4,7 +4,6 @@ setup(
     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",

+ 21 - 4
switchbot/devices/device.py

@@ -10,7 +10,6 @@ from enum import Enum
 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
@@ -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."""
 
@@ -451,16 +456,28 @@ class SwitchbotBaseDevice:
         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")