Skip to content

Commit c995728

Browse files
committed
Version 0.7.0 release
1 parent b9a1559 commit c995728

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"nrf-connect.toolchain.path": "${nrf-connect.toolchain:2.1.0}"
3+
}

cometblue_lite/discovery.py

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""Discover switchbot devices."""
2+
3+
from __future__ import annotations
4+
5+
import asyncio
6+
import logging
7+
8+
import bleak
9+
from bleak.backends.device import BLEDevice
10+
from bleak.backends.scanner import AdvertisementData
11+
12+
from cometblue import *
13+
14+
_LOGGER = logging.getLogger(__name__)
15+
CONNECT_LOCK = asyncio.Lock()
16+
17+
DEFAULT_SCAN_TIMEOUT = 5
18+
DEFAULT_RETRY_COUNT = 3
19+
DEFAULT_RETRY_TIMEOUT = 1
20+
21+
class GetSwitchbotDevices:
22+
"""Scan for all Switchbot devices and return by type."""
23+
24+
def __init__(self, interface: int = 0) -> None:
25+
"""Get switchbot devices class constructor."""
26+
self._interface = f"hci{interface}"
27+
self._adv_data: dict[str, AdvertisementData] = {}
28+
29+
def detection_callback(
30+
self,
31+
device: BLEDevice,
32+
advertisement_data: AdvertisementData,
33+
) -> None:
34+
"""Callback for device detection."""
35+
discovery = advertisement_data #parse_advertisement_data(device, advertisement_data)
36+
#print(discovery.local_name)
37+
if discovery and device.name == "Comet Blue":
38+
print(device.metadata)
39+
print(device.address)
40+
print(discovery.service_data)
41+
print(discovery.service_uuids)
42+
print(discovery.manufacturer_data)
43+
44+
#self._adv_data[discovery.address] = discovery
45+
46+
async def discover(
47+
self, retry: int = DEFAULT_RETRY_COUNT, scan_timeout: int = DEFAULT_SCAN_TIMEOUT
48+
) -> dict:
49+
"""Find switchbot devices and their advertisement data."""
50+
51+
devices = None
52+
devices = bleak.BleakScanner(
53+
# TODO: Find new UUIDs to filter on. For example, see
54+
# https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/4ad138bb09f0fbbfa41b152ca327a78c1d0b6ba9/devicetypes/meter.md
55+
adapter=self._interface,
56+
)
57+
devices.register_detection_callback(self.detection_callback)
58+
59+
async with CONNECT_LOCK:
60+
await devices.start()
61+
await asyncio.sleep(scan_timeout)
62+
await devices.stop()
63+
64+
if devices is None:
65+
if retry < 1:
66+
_LOGGER.error(
67+
"Scanning for Switchbot devices failed. Stop trying", exc_info=True
68+
)
69+
return self._adv_data
70+
71+
_LOGGER.warning(
72+
"Error scanning for Switchbot devices. Retrying (remaining: %d)",
73+
retry,
74+
)
75+
await asyncio.sleep(DEFAULT_RETRY_TIMEOUT)
76+
return await self.discover(retry - 1, scan_timeout)
77+
78+
return self._adv_data
79+
80+
async def _get_devices_by_model(
81+
self,
82+
model: str,
83+
) -> dict:
84+
"""Get switchbot devices by type."""
85+
if not self._adv_data:
86+
await self.discover()
87+
88+
return {
89+
address: adv
90+
for address, adv in self._adv_data.items()
91+
if adv.data.get("model") == model
92+
}
93+
94+
95+
async def get_device_data(
96+
self, address: str
97+
) -> dict[str, AdvertisementData] | None:
98+
"""Return data for specific device."""
99+
if not self._adv_data:
100+
await self.discover()
101+
102+
return {
103+
device: adv
104+
for device, adv in self._adv_data.items()
105+
# MacOS uses UUIDs instead of MAC addresses
106+
if adv.data.get("address") == address
107+
}

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name='cometblue_lite',
6-
version='0.5.3',
6+
version='0.7.0',
77
packages=['cometblue_lite'],
88
python_requires='>=3.4',
99
install_requires=['bleak_retry_connector>=1.8.0', 'bleak>=0.15.1'],

0 commit comments

Comments
 (0)