Skip to content

Commit 37fc839

Browse files
authored
feat: reduce overhead to convert non-connectable BluetoothServiceInfoBleak to connectable (#135)
1 parent 7ab2795 commit 37fc839

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

src/habluetooth/manager.py

+1-14
Original file line numberDiff line numberDiff line change
@@ -605,20 +605,7 @@ def scanner_adv_received(self, service_info: BluetoothServiceInfoBleak) -> None:
605605
# route any connection attempts to the connectable path, we
606606
# mark the service_info as connectable so that the callbacks
607607
# will be called and the device can be discovered.
608-
service_info = BluetoothServiceInfoBleak(
609-
service_info.name,
610-
address,
611-
service_info.rssi,
612-
service_info.manufacturer_data,
613-
service_info.service_data,
614-
service_info.service_uuids,
615-
service_info.source,
616-
service_info.device,
617-
service_info._advertisement,
618-
True,
619-
service_info.time,
620-
service_info.tx_power,
621-
)
608+
service_info = service_info._as_connectable()
622609

623610
if (connectable or old_connectable_service_info is not None) and (
624611
bleak_callbacks := self._bleak_callbacks

src/habluetooth/models.pxd

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import cython
12

23
cdef object BLEDevice
34
cdef object AdvertisementData
@@ -29,3 +30,6 @@ cdef class BluetoothServiceInfoBleak(BluetoothServiceInfo):
2930
cdef public bint connectable
3031
cdef public double time
3132
cdef public object tx_power
33+
34+
@cython.locals(new_obj=BluetoothServiceInfoBleak)
35+
cpdef BluetoothServiceInfoBleak _as_connectable(self)

src/habluetooth/models.py

+17
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,20 @@ def from_device_and_advertisement_data(
316316
time,
317317
advertisement_data.tx_power,
318318
)
319+
320+
def _as_connectable(self) -> BluetoothServiceInfoBleak:
321+
"""Return a connectable version of this object."""
322+
new_obj = BluetoothServiceInfoBleak.__new__(BluetoothServiceInfoBleak)
323+
new_obj.name = self.name
324+
new_obj.address = self.address
325+
new_obj.rssi = self.rssi
326+
new_obj.manufacturer_data = self.manufacturer_data
327+
new_obj.service_data = self.service_data
328+
new_obj.service_uuids = self.service_uuids
329+
new_obj.source = self.source
330+
new_obj.device = self.device
331+
new_obj._advertisement = self._advertisement
332+
new_obj.connectable = True
333+
new_obj.time = self.time
334+
new_obj.tx_power = self.tx_power
335+
return new_obj

tests/test_models.py

+37
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,40 @@ class pyobjc_int(int):
238238
"time": now,
239239
"tx_power": 1,
240240
}
241+
242+
243+
def test_as_connectable():
244+
switchbot_device = BLEDevice("44:44:33:11:23:45", "wohand", {}, -127)
245+
switchbot_adv = generate_advertisement_data(
246+
local_name="wohand", service_uuids=["cba20d00-224d-11e6-9fb8-0002a5d5c51b"]
247+
)
248+
now = time.monotonic()
249+
service_info = BluetoothServiceInfoBleak(
250+
name="wohand",
251+
address="44:44:33:11:23:45",
252+
rssi=-127,
253+
manufacturer_data=switchbot_adv.manufacturer_data,
254+
service_data=switchbot_adv.service_data,
255+
service_uuids=switchbot_adv.service_uuids,
256+
source=SOURCE_LOCAL,
257+
device=switchbot_device,
258+
advertisement=switchbot_adv,
259+
connectable=False,
260+
time=now,
261+
tx_power=1,
262+
)
263+
connectable_service_info = service_info._as_connectable()
264+
assert connectable_service_info.connectable is True
265+
assert service_info.connectable is False
266+
assert connectable_service_info is not service_info
267+
assert service_info.name == connectable_service_info.name
268+
assert service_info.address == connectable_service_info.address
269+
assert service_info.rssi == connectable_service_info.rssi
270+
assert service_info.manufacturer_data == connectable_service_info.manufacturer_data
271+
assert service_info.service_data == connectable_service_info.service_data
272+
assert service_info.service_uuids == connectable_service_info.service_uuids
273+
assert service_info.source == connectable_service_info.source
274+
assert service_info.device == connectable_service_info.device
275+
assert service_info.advertisement == connectable_service_info.advertisement
276+
assert service_info.time == connectable_service_info.time
277+
assert service_info.tx_power == connectable_service_info.tx_power

0 commit comments

Comments
 (0)