Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle already connected devices with no rssi value #25

Merged
merged 1 commit into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/bleak_retry_connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,14 @@ async def get_bluez_device(path: str, rssi: int | None = None) -> BLEDevice | No
):
continue
rssi = properties[path][defs.DEVICE_INTERFACE].get("RSSI")
if not rssi or rssi - RSSI_SWITCH_THRESHOLD < device_rssi:
continue
if rssi < rssi_to_beat:
if rssi_to_beat != UNREACHABLE_RSSI and (
not rssi
or rssi - RSSI_SWITCH_THRESHOLD < device_rssi
or rssi < rssi_to_beat
):
continue
best_path = path
rssi_to_beat = rssi
rssi_to_beat = rssi or UNREACHABLE_RSSI
_LOGGER.debug("Found device %s with better RSSI %s", path, rssi)

if best_path == device_path:
Expand Down
49 changes: 49 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,3 +899,52 @@ def __init__(self):
device = await get_device("FA:23:9D:AA:45:46")

assert device is None


@pytest.mark.asyncio
async def test_get_device_already_connected():
class FakeBluezManager:
def __init__(self):
self._properties = {
"/org/bluez/hci1/dev_BD_24_6F_85_AA_61": {
"org.freedesktop.DBus.Introspectable": {},
"org.bluez.Device1": {
"Address": "BD:24:6F:85:AA:61",
"AddressType": "public",
"Name": "Dream~BD246F85AA61",
"Alias": "Dream~BD246F85AA61",
"Appearance": 962,
"Icon": "input-mouse",
"Paired": False,
"Trusted": False,
"Blocked": False,
"LegacyPairing": False,
"Connected": True,
"UUIDs": [
"00001800-0000-1000-8000-00805f9b34fb",
"00001801-0000-1000-8000-00805f9b34fb",
"0000180a-0000-1000-8000-00805f9b34fb",
"0000ffd0-0000-1000-8000-00805f9b34fb",
"0000ffd5-0000-1000-8000-00805f9b34fb",
],
"Modalias": "usb:v045Ep0040d0300",
"Adapter": "/org/bluez/hci1",
"ManufacturerData": {20808: bytearray(b"364656")},
"ServicesResolved": True,
},
"org.freedesktop.DBus.Properties": {},
}
}

bleak_retry_connector.get_global_bluez_manager = AsyncMock(
return_value=FakeBluezManager()
)
bleak_retry_connector.defs = defs

with patch.object(bleak_retry_connector, "IS_LINUX", True), patch.object(
bleak_retry_connector, "CAN_CACHE_SERVICES", True
):
device = await get_device("BD:24:6F:85:AA:61")

assert device is not None
assert device.details["path"] == "/org/bluez/hci1/dev_BD_24_6F_85_AA_61"