Browse Source

Use faster async_timeout in place of asyncio.wait_for (#59)

J. Nick Koston 1 year ago
parent
commit
2174f2389c
2 changed files with 4 additions and 2 deletions
  1. 1 1
      setup.py
  2. 3 1
      switchbot/__init__.py

+ 1 - 1
setup.py

@@ -3,7 +3,7 @@ from setuptools import setup
 setup(
     name = 'PySwitchbot',
     packages = ['switchbot'],
-    install_requires=['bleak', 'bleak-retry-connector>=1.1.1'],
+    install_requires=['async_timeout>=4.0.1', 'bleak', 'bleak-retry-connector>=1.1.1'],
     version = '0.17.1',
     description = 'A library to communicate with Switchbot',
     author='Daniel Hjelseth Hoyer',

+ 3 - 1
switchbot/__init__.py

@@ -8,6 +8,7 @@ from dataclasses import dataclass
 from typing import Any
 from uuid import UUID
 
+import async_timeout
 import bleak
 from bleak.backends.device import BLEDevice
 from bleak.backends.scanner import AdvertisementData
@@ -400,7 +401,8 @@ class SwitchbotDevice:
             _LOGGER.debug("%s: Sending command, %s", self.name, key)
             await client.write_gatt_char(_sb_uuid(comms_type="tx"), command, False)
 
-            notify_msg = await asyncio.wait_for(future, timeout=5)
+            async with async_timeout.timeout(5):
+                notify_msg = await future
             _LOGGER.info("%s: Notification received: %s", self.name, notify_msg)
 
             _LOGGER.debug("%s: UnSubscribe to notifications", self.name)