summaryrefslogtreecommitdiff
path: root/pkgs/development/python-modules/pyzerproc/bleak-compat.patch
blob: 9054a5f86b79164c9124a733875cf5caf0bc57fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
diff --git a/pyzerproc/discovery.py b/pyzerproc/discovery.py
index e383996..1810bbe 100644
--- a/pyzerproc/discovery.py
+++ b/pyzerproc/discovery.py
@@ -4,6 +4,9 @@ import logging
 from .light import Light
 from .exceptions import ZerprocException
 
+from bleak import BLEDevice, BleakScanner, AdvertisementData
+from bleak.exc import BleakError
+
 _LOGGER = logging.getLogger(__name__)
 
 EXPECTED_SERVICES = [
@@ -13,27 +16,25 @@ EXPECTED_SERVICES = [
 ]
 
 
-def is_valid_device(device):
+def is_valid_device(device: BLEDevice, advertisement_data: AdvertisementData):
     """Returns true if the given device is a Zerproc light."""
     for service in EXPECTED_SERVICES:
-        if service not in device.metadata['uuids']:
+        if service not in advertisement_data.service_uuids:
             return False
     return True
 
 
 async def discover(timeout=10):
     """Returns nearby discovered lights."""
-    import bleak
-
     _LOGGER.info("Starting scan for local devices")
 
     lights = []
     try:
-        devices = await bleak.BleakScanner.discover(timeout=timeout)
-    except bleak.exc.BleakError as ex:
+        devices = await BleakScanner.discover(timeout=timeout, return_adv=True)
+    except BleakError as ex:
         raise ZerprocException() from ex
-    for device in devices:
-        if is_valid_device(device):
+    for device, advertisement_data in devices:
+        if is_valid_device(device, advertisement_data):
             lights.append(Light(device.address, device.name))
 
     _LOGGER.info("Scan complete")
diff --git a/tests/test_discovery.py b/tests/test_discovery.py
index 7a1442d..6b271b8 100644
--- a/tests/test_discovery.py
+++ b/tests/test_discovery.py
@@ -16,7 +16,6 @@ async def test_discover_devices(scanner, client_class):
                 'AA:BB:CC:11:22:33',
                 'LEDBlue-CC112233',
                 {},
-                0,
                 uuids=[
                     "0000ffe0-0000-1000-8000-00805f9b34fb",
                     "0000ffe5-0000-1000-8000-00805f9b34fb",
@@ -27,7 +26,6 @@ async def test_discover_devices(scanner, client_class):
                 'AA:BB:CC:44:55:66',
                 'LEDBlue-CC445566',
                 {},
-                0,
                 uuids=[
                     "0000ffe0-0000-1000-8000-00805f9b34fb",
                     "0000ffe5-0000-1000-8000-00805f9b34fb",
@@ -38,7 +36,6 @@ async def test_discover_devices(scanner, client_class):
                 'DD:EE:FF:11:22:33',
                 'Other',
                 {},
-                0,
                 uuids=[
                     "0000fe9f-0000-1000-8000-00805f9b34fb",
                 ],