Skip to content

Commit

Permalink
feat: small speed ups to base_scanner (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 7, 2023
1 parent d3e2a84 commit e1ff7e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
14 changes: 11 additions & 3 deletions src/habluetooth/base_scanner.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ cdef object NO_RSSI_VALUE
cdef object BluetoothServiceInfoBleak
cdef object AdvertisementData
cdef object BLEDevice
cdef bint TYPE_CHECKING

cdef class BaseHaScanner:

Expand All @@ -27,25 +28,32 @@ cdef class BaseHaRemoteScanner(BaseHaScanner):
cdef public dict _discovered_device_advertisement_datas
cdef public dict _discovered_device_timestamps
cdef public dict _details
cdef public object _expire_seconds
cdef public float _expire_seconds
cdef public object _cancel_track

@cython.locals(
prev_service_uuids=list,
prev_service_data=dict,
prev_manufacturer_data=dict,
prev_name=str,
prev_discovery=tuple
prev_discovery=tuple,
has_manufacturer_data=bint,
has_service_data=bint,
has_service_uuids=bint,
prev_details=dict
)
cpdef void _async_on_advertisement(
self,
object address,
object rssi,
object local_name,
str local_name,
list service_uuids,
dict service_data,
dict manufacturer_data,
object tx_power,
dict details,
object advertisement_monotonic_time
)

@cython.locals(now=float, timestamp=float)
cpdef void _async_expire_devices(self)
18 changes: 11 additions & 7 deletions src/habluetooth/base_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,23 +314,27 @@ def _async_on_advertisement(
prev_service_data = prev_advertisement.service_data
prev_manufacturer_data = prev_advertisement.manufacturer_data
prev_name = prev_device.name
prev_details = prev_device.details

if prev_name and (not local_name or len(prev_name) > len(local_name)):
local_name = prev_name

if service_uuids and service_uuids != prev_service_uuids:
has_service_uuids = bool(service_uuids)
if has_service_uuids and service_uuids != prev_service_uuids:
service_uuids = list({*service_uuids, *prev_service_uuids})
elif not service_uuids:
elif not has_service_uuids:
service_uuids = prev_service_uuids

if service_data and service_data != prev_service_data:
has_service_data = bool(service_data)
if has_service_data and service_data != prev_service_data:
service_data = {**prev_service_data, **service_data}
elif not service_data:
elif not has_service_data:
service_data = prev_service_data

if manufacturer_data and manufacturer_data != prev_manufacturer_data:
has_manufacturer_data = bool(manufacturer_data)
if has_manufacturer_data and manufacturer_data != prev_manufacturer_data:
manufacturer_data = {**prev_manufacturer_data, **manufacturer_data}
elif not manufacturer_data:
elif not has_manufacturer_data:
manufacturer_data = prev_manufacturer_data
#
# Bleak updates the BLEDevice via create_or_update_device.
Expand All @@ -342,7 +346,7 @@ def _async_on_advertisement(
#
device = prev_device
device.name = local_name
device.details = {**self._details, **details}
prev_details.update(details)
# pylint: disable-next=protected-access
device._rssi = rssi # deprecated, will be removed in newer bleak

Expand Down

0 comments on commit e1ff7e9

Please sign in to comment.