diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 047d13fb..7c50391b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,6 +14,7 @@ Added ----- * Added new ``assigned_numbers`` module and ``AdvertisementDataType`` enum. +* Added new ``bluez`` kwarg to ``BleakScanner`` in BlueZ backend. Changed ------- @@ -24,6 +25,7 @@ Changed * ``BleakScanner()`` args ``detection_callback`` and ``service_uuids`` are no longer keyword-only. * ``BleakScanner()`` arg ``scanning_mode`` is no longer Windows-only and is no longer keyword-only. * All ``BleakScanner()`` instances in BlueZ backend now use common D-Bus object manager. +* Deprecated ``filters`` kwarg in ``BleakScanner`` in BlueZ backend. `0.14.3`_ (2022-04-29) diff --git a/bleak/backends/bluezdbus/scanner.py b/bleak/backends/bluezdbus/scanner.py index 80505b16..a7b12571 100644 --- a/bleak/backends/bluezdbus/scanner.py +++ b/bleak/backends/bluezdbus/scanner.py @@ -1,5 +1,7 @@ import logging +import sys from typing import Callable, Coroutine, Dict, List, Literal, Optional +from warnings import warn from dbus_next import Variant @@ -10,6 +12,32 @@ logger = logging.getLogger(__name__) +if sys.version_info >= (3, 8): + from typing import TypedDict + + class BlueZDiscoveryFilters(TypedDict, total=False): + UUIDs: List[str] + RSSI: int + Pathloss: int + Transport: str + DuplicateData: bool + Discoverable: bool + Pattern: str + + class BlueZArgs(TypedDict, total=False): + """ + :class:`BleakScanner` args that are specific to the BlueZ backend. + """ + + filters: BlueZDiscoveryFilters + """ + Filters to pass to the adapter SetDiscoveryFilter D-Bus method. + """ + +else: + BlueZDiscoveryFilters = Dict[str, object] + BlueZArgs = Dict[str, object] + class BleakScannerBlueZDBus(BaseBleakScanner): """The native Linux Bleak BLE Scanner. @@ -28,10 +56,10 @@ class BleakScannerBlueZDBus(BaseBleakScanner): also enables scanning while the screen is off on Android. scanning_mode: Set to ``"passive"`` to avoid the ``"active"`` scanning mode. + **bluez: + Dictionary of arguments specific to the BlueZ backend. **adapter (str): Bluetooth adapter to use for discovery. - **filters (dict): - A dict of filters to be applied on discovery. """ def __init__( @@ -39,6 +67,8 @@ def __init__( detection_callback: Optional[AdvertisementDataCallback] = None, service_uuids: Optional[List[str]] = None, scanning_mode: Literal["active", "passive"] = "active", + *, + bluez: BlueZArgs = {}, **kwargs, ): super(BleakScannerBlueZDBus, self).__init__(detection_callback, service_uuids) @@ -66,7 +96,19 @@ def __init__( if self._service_uuids: self._filters["UUIDs"] = Variant("as", self._service_uuids) - self.set_scanning_filter(**kwargs) + filters = kwargs.get("filters") + + if filters is None: + filters = bluez.get("filters") + else: + warn( + "the 'filters' kwarg is deprecated, use 'bluez' kwarg instead", + FutureWarning, + stacklevel=2, + ) + + if filters is not None: + self.set_scanning_filter(filters=filters) async def start(self): manager = await get_bluez_manager()