Skip to content

Commit

Permalink
backends/device: deprecate BLEDevice.rssi and .metadata
Browse files Browse the repository at this point in the history
This make a clean separation between the BLEDevice which is the OS
handle for a "known" BLE device and the advertising data for that
device. There are now alternatives in place for getting advertising
data paired with the BLEDevice so we can deprecate these attributes.

Fixes #1025.
  • Loading branch information
dlech committed Oct 3, 2022
1 parent 516f0e1 commit 807bc31
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Changed
-------
* Changed ``AdvertisementData`` to a named tuple.
* A faster ``unpack_variants`` is now provided by dbus-fast
* Deprecated ``BLEDevice.rssi`` and ``BLEDevice.metadata``. Fixes #1025.

Fixed
-----
Expand Down
64 changes: 47 additions & 17 deletions bleak/backends/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,61 @@
"""


from typing import Any, Optional
from warnings import warn


class BLEDevice:
"""A simple wrapper class representing a BLE server detected during
a `discover` call.
- When using Windows backend, `details` attribute is a
``Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisement`` object, unless
it is created with the Windows.Devices.Enumeration discovery method, then is is a
``Windows.Devices.Enumeration.DeviceInformation``.
- When using Linux backend, ``details`` attribute is a
dict with keys ``path`` which has the string path to the DBus device object and ``props``
which houses the properties dictionary of the D-Bus Device.
- When using macOS backend, ``details`` attribute will be a CBPeripheral object.
"""
A simple wrapper class representing a BLE server detected during scanning.
"""

def __init__(self, address, name=None, details=None, rssi=0, **kwargs):
def __init__(
self, address: str, name: Optional[str], details: Any, rssi: int, **kwargs
):
#: The Bluetooth address of the device on this machine.
self.address = address
#: The advertised name of the device.
#: The operating system name of the device (not necessarily the local name
# from the advertising data), suitable for display to the user.
self.name = name
#: The OS native details required for connecting to the device.
self.details = details
#: RSSI, if available
self.rssi = rssi
#: Device specific details. Contains a ``uuids`` key which is a list of service UUIDs and a ``manufacturer_data`` field with a bytes-object from the advertised data.
self.metadata = kwargs

# for backwards compatibility
self._rssi = rssi
self._metadata = kwargs

@property
def rssi(self) -> int:
"""
Gets the RSSI of the last received advertisement.
.. deprecated:: 0.19.0
Use :class:`AdvertisementData` from detection callback or
:attr:`BleakScanner.discovered_devices_and_advertisement_data` instead.
"""
warn(
"BLEDevice.rssi is deprecated and will be removed in a future version of Bleak, use AdvertisementData.rssi instead",
FutureWarning,
stacklevel=2,
)
return self._rssi

@property
def metadata(self) -> dict:
"""
Gets additional advertisement data for the device.
.. deprecated:: 0.19.0
Use :class:`AdvertisementData` from detection callback or
:attr:`BleakScanner.discovered_devices_and_advertisement_data` instead.
"""
warn(
"BLEDevice.metadata is deprecated and will be removed in a future version of Bleak, use AdvertisementData instead",
FutureWarning,
stacklevel=2,
)
return self._metadata

def __str__(self):
return f"{self.address}: {self.name}"
Expand Down
3 changes: 2 additions & 1 deletion bleak/backends/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ def create_or_update_device(
try:
device, _ = self.seen_devices[address]

device.metadata = metadata
device._rssi = adv.rssi
device._metadata = metadata
except KeyError:
device = BLEDevice(
address,
Expand Down
2 changes: 0 additions & 2 deletions examples/kivy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ async def example(self):
if len(scanned_devices) == 0:
raise bleak.exc.BleakError("no devices found")

scanned_devices.sort(key=lambda device: -device.rssi)

for device in scanned_devices:
self.line(f"{device.name} ({device.address})")

Expand Down

0 comments on commit 807bc31

Please sign in to comment.