Skip to content

Commit

Permalink
feat: add a fast cython init path for BluetoothServiceInfoBleak (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 2, 2024
1 parent 8d84f68 commit f532ed2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 36 deletions.
11 changes: 6 additions & 5 deletions src/habluetooth/base_scanner.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ cdef bint TYPE_CHECKING
cdef class BaseHaScanner:

cdef public str adapter
cdef public object connectable
cdef public bint connectable
cdef public str source
cdef public object connector
cdef public unsigned int _connecting
cdef public str name
cdef public bint scanning
cdef public object _last_detection
cdef public double _last_detection
cdef public object _start_time
cdef public object _cancel_watchdog
cdef public object _loop
Expand All @@ -45,19 +45,20 @@ cdef class BaseHaRemoteScanner(BaseHaScanner):
has_service_data=bint,
has_service_uuids=bint,
prev_details=dict,
service_info=BluetoothServiceInfoBleak,
prev_service_info=BluetoothServiceInfoBleak
)
cpdef void _async_on_advertisement(
self,
object address,
object rssi,
str address,
int rssi,
str local_name,
list service_uuids,
dict service_data,
dict manufacturer_data,
object tx_power,
dict details,
object advertisement_monotonic_time
double advertisement_monotonic_time
)

@cython.locals(now=float, timestamp=float, service_info=BluetoothServiceInfoBleak)
Expand Down
27 changes: 13 additions & 14 deletions src/habluetooth/base_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,20 +423,19 @@ def _async_on_advertisement(
# pylint: disable-next=protected-access
device._rssi = rssi # deprecated, will be removed in newer bleak

service_info = BluetoothServiceInfoBleak(
local_name or address,
address,
rssi,
manufacturer_data,
service_data,
service_uuids,
self.source,
device,
None,
self.connectable,
advertisement_monotonic_time,
tx_power,
)
service_info = BluetoothServiceInfoBleak.__new__(BluetoothServiceInfoBleak)
service_info.name = local_name or address
service_info.address = address
service_info.rssi = rssi
service_info.manufacturer_data = manufacturer_data
service_info.service_data = service_data
service_info.service_uuids = service_uuids
service_info.source = self.source
service_info.device = device
service_info._advertisement = None
service_info.connectable = self.connectable
service_info.time = advertisement_monotonic_time
service_info.tx_power = tx_power
self._previous_service_info[address] = service_info
self._manager.scanner_adv_received(service_info)

Expand Down
2 changes: 1 addition & 1 deletion src/habluetooth/models.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ cdef class BluetoothServiceInfoBleak(BluetoothServiceInfo):
"""BluetoothServiceInfo with bleak data."""

cdef public object device
cdef public bint connectable
cdef public object _advertisement
cdef public bint connectable
cdef public double time
cdef public object tx_power
2 changes: 2 additions & 0 deletions src/habluetooth/scanner.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cdef object BLEDevice

cdef bint TYPE_CHECKING

cdef object _NEW_SERVICE_INFO

cdef class HaScanner(BaseHaScanner):

Expand All @@ -21,6 +22,7 @@ cdef class HaScanner(BaseHaScanner):
cdef public object _start_future
cdef public object current_mode

@cython.locals(service_info=BluetoothServiceInfoBleak)
cpdef void _async_detection_callback(
self,
object device,
Expand Down
31 changes: 15 additions & 16 deletions src/habluetooth/scanner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# cython: profile=True
"""A local bleak scanner."""

from __future__ import annotations
Expand Down Expand Up @@ -259,22 +260,20 @@ def _async_detection_callback(
tx_power = advertisement_data.tx_power
if tx_power is not None and type(tx_power) is not int:
tx_power = int(tx_power)
self._manager.scanner_adv_received(
BluetoothServiceInfoBleak(
name,
address,
advertisement_data.rssi,
manufacturer_data,
service_data,
service_uuids,
self.source,
device,
advertisement_data,
True,
callback_time,
tx_power,
)
)
service_info = BluetoothServiceInfoBleak.__new__(BluetoothServiceInfoBleak)
service_info.name = name
service_info.address = address
service_info.rssi = advertisement_data.rssi
service_info.manufacturer_data = manufacturer_data
service_info.service_data = service_data
service_info.service_uuids = service_uuids
service_info.source = self.source
service_info.device = device
service_info._advertisement = advertisement_data
service_info.connectable = True
service_info.time = callback_time
service_info.tx_power = tx_power
self._manager.scanner_adv_received(service_info)

async def async_start(self) -> None:
"""Start bluetooth scanner."""
Expand Down

0 comments on commit f532ed2

Please sign in to comment.