Skip to content

Commit

Permalink
feat: add cython implementation for manager (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 6, 2023
1 parent f28547c commit 266a602
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
49 changes: 49 additions & 0 deletions src/habluetooth/manager.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import cython

from .advertisement_tracker cimport AdvertisementTracker
from .base_scanner cimport BaseHaScanner

cdef object NO_RSSI_VALUE
cdef object RSSI_SWITCH_THRESHOLD
cdef object FILTER_UUIDS
cdef object BluetoothServiceInfoBleak
cdef object AdvertisementData
cdef object BLEDevice
cdef bint TYPE_CHECKING
cdef set APPLE_START_BYTES_WANTED

cdef unsigned int APPLE_MFR_ID

@cython.locals(uuids=set)
cdef _dispatch_bleak_callback(
object callback,
dict filters,
object device,
object advertisement_data
)

cdef class BluetoothManager:

cdef public object _cancel_unavailable_tracking
cdef public AdvertisementTracker _advertisement_tracker
cdef public dict _fallback_intervals
cdef public dict _intervals
cdef public dict _unavailable_callbacks
cdef public dict _connectable_unavailable_callbacks
cdef public list _bleak_callbacks
cdef public dict _all_history
cdef public dict _connectable_history
cdef public list _non_connectable_scanners
cdef public list _connectable_scanners
cdef public dict _adapters
cdef public dict _sources
cdef public object _bluetooth_adapters
cdef public object slot_manager
cdef public bint _debug
cdef public bint shutdown
cdef public object _loop

cdef bint _prefer_previous_adv_from_different_source(self, object old, object new)

@cython.locals(source=str, connectable=bint, scanner=BaseHaScanner, connectable_scanner=BaseHaScanner)
cpdef void scanner_adv_received(self, object service_info)
29 changes: 15 additions & 14 deletions src/habluetooth/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
from bluetooth_data_tools import monotonic_time_coarse
from home_assistant_bluetooth import BluetoothServiceInfoBleak

from habluetooth import TRACKER_BUFFERING_WOBBLE_SECONDS, AdvertisementTracker
from habluetooth import TRACKER_BUFFERING_WOBBLE_SECONDS

from .advertisement_tracker import AdvertisementTracker
from .base_scanner import BaseHaScanner, BluetoothScannerDevice
from .const import (
CALLBACK_TYPE,
Expand Down Expand Up @@ -496,17 +497,17 @@ def scanner_adv_received(self, service_info: BluetoothServiceInfoBleak) -> None:
# mark the service_info as connectable so that the callbacks
# will be called and the device can be discovered.
service_info = BluetoothServiceInfoBleak(
name=service_info.name,
address=service_info.address,
rssi=service_info.rssi,
manufacturer_data=service_info.manufacturer_data,
service_data=service_info.service_data,
service_uuids=service_info.service_uuids,
source=service_info.source,
device=service_info.device,
advertisement=service_info.advertisement,
connectable=True,
time=service_info.time,
service_info.name,
service_info.address,
service_info.rssi,
service_info.manufacturer_data,
service_info.service_data,
service_info.service_uuids,
service_info.source,
service_info.device,
service_info.advertisement,
True,
service_info.time,
)

if (connectable or old_connectable_service_info) and (
Expand All @@ -515,8 +516,8 @@ def scanner_adv_received(self, service_info: BluetoothServiceInfoBleak) -> None:
# Bleak callbacks must get a connectable device
device = service_info.device
advertisement_data = service_info.advertisement
for callback_filters in bleak_callbacks:
_dispatch_bleak_callback(*callback_filters, device, advertisement_data)
for callback, filters in bleak_callbacks:
_dispatch_bleak_callback(callback, filters, device, advertisement_data)

self._discover_service_info(service_info)

Expand Down

0 comments on commit 266a602

Please sign in to comment.