Handle default RSSI values from bleak in bluetooth (#78908)

pull/56587/head
J. Nick Koston 2022-09-21 11:03:50 -10:00 committed by GitHub
parent 420285f7ef
commit 488b04fc8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 1 deletions

View File

@ -65,6 +65,7 @@ APPLE_START_BYTES_WANTED: Final = {
} }
RSSI_SWITCH_THRESHOLD = 6 RSSI_SWITCH_THRESHOLD = 6
NO_RSSI_VALUE = -1000
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -88,7 +89,7 @@ def _prefer_previous_adv(
STALE_ADVERTISEMENT_SECONDS, STALE_ADVERTISEMENT_SECONDS,
) )
return False return False
if new.device.rssi - RSSI_SWITCH_THRESHOLD > old.device.rssi: if new.device.rssi - RSSI_SWITCH_THRESHOLD > (old.device.rssi or NO_RSSI_VALUE):
# If new advertisement is RSSI_SWITCH_THRESHOLD more, the new one is preferred # If new advertisement is RSSI_SWITCH_THRESHOLD more, the new one is preferred
if new.source != old.source: if new.source != old.source:
_LOGGER.debug( _LOGGER.debug(

View File

@ -119,6 +119,62 @@ async def test_switching_adapters_based_on_rssi(hass, enable_bluetooth):
) )
async def test_switching_adapters_based_on_zero_rssi(hass, enable_bluetooth):
"""Test switching adapters based on zero rssi."""
address = "44:44:33:11:23:45"
switchbot_device_no_rssi = BLEDevice(address, "wohand_poor_signal", rssi=0)
switchbot_adv_no_rssi = AdvertisementData(
local_name="wohand_no_rssi", service_uuids=[]
)
inject_advertisement_with_source(
hass, switchbot_device_no_rssi, switchbot_adv_no_rssi, "hci0"
)
assert (
bluetooth.async_ble_device_from_address(hass, address)
is switchbot_device_no_rssi
)
switchbot_device_good_signal = BLEDevice(address, "wohand_good_signal", rssi=-60)
switchbot_adv_good_signal = AdvertisementData(
local_name="wohand_good_signal", service_uuids=[]
)
inject_advertisement_with_source(
hass, switchbot_device_good_signal, switchbot_adv_good_signal, "hci1"
)
assert (
bluetooth.async_ble_device_from_address(hass, address)
is switchbot_device_good_signal
)
inject_advertisement_with_source(
hass, switchbot_device_good_signal, switchbot_adv_no_rssi, "hci0"
)
assert (
bluetooth.async_ble_device_from_address(hass, address)
is switchbot_device_good_signal
)
# We should not switch adapters unless the signal hits the threshold
switchbot_device_similar_signal = BLEDevice(
address, "wohand_similar_signal", rssi=-62
)
switchbot_adv_similar_signal = AdvertisementData(
local_name="wohand_similar_signal", service_uuids=[]
)
inject_advertisement_with_source(
hass, switchbot_device_similar_signal, switchbot_adv_similar_signal, "hci0"
)
assert (
bluetooth.async_ble_device_from_address(hass, address)
is switchbot_device_good_signal
)
async def test_switching_adapters_based_on_stale(hass, enable_bluetooth): async def test_switching_adapters_based_on_stale(hass, enable_bluetooth):
"""Test switching adapters based on the previous advertisement being stale.""" """Test switching adapters based on the previous advertisement being stale."""