Prevent bluetooth scanner from being shutdown by BleakClient not using BLEDevice (#76945)

This commit is contained in:
J. Nick Koston 2022-08-17 11:42:12 -10:00 committed by GitHub
parent 3bcc274dfa
commit 7bf13167d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 95 additions and 4 deletions

View file

@ -1,14 +1,27 @@
"""Tests for the Bluetooth integration."""
import bleak
from unittest.mock import patch
from homeassistant.components.bluetooth.models import HaBleakScannerWrapper
import bleak
from bleak.backends.device import BLEDevice
import pytest
from homeassistant.components.bluetooth.models import (
HaBleakClientWrapper,
HaBleakScannerWrapper,
)
from homeassistant.components.bluetooth.usage import (
install_multiple_bleak_catcher,
uninstall_multiple_bleak_catcher,
)
from . import _get_manager
MOCK_BLE_DEVICE = BLEDevice(
"00:00:00:00:00:00", "any", delegate="", details={"path": "/dev/hci0/device"}
)
async def test_multiple_bleak_scanner_instances(hass):
"""Test creating multiple BleakScanners without an integration."""
@ -23,3 +36,46 @@ async def test_multiple_bleak_scanner_instances(hass):
instance = bleak.BleakScanner()
assert not isinstance(instance, HaBleakScannerWrapper)
async def test_wrapping_bleak_client(hass, enable_bluetooth):
"""Test we wrap BleakClient."""
install_multiple_bleak_catcher()
instance = bleak.BleakClient(MOCK_BLE_DEVICE)
assert isinstance(instance, HaBleakClientWrapper)
uninstall_multiple_bleak_catcher()
instance = bleak.BleakClient(MOCK_BLE_DEVICE)
assert not isinstance(instance, HaBleakClientWrapper)
async def test_bleak_client_reports_with_address(hass, enable_bluetooth, caplog):
"""Test we report when we pass an address to BleakClient."""
install_multiple_bleak_catcher()
with pytest.raises(bleak.BleakError):
instance = bleak.BleakClient("00:00:00:00:00:00")
with patch.object(
_get_manager(),
"async_ble_device_from_address",
return_value=MOCK_BLE_DEVICE,
):
instance = bleak.BleakClient("00:00:00:00:00:00")
assert "BleakClient with an address instead of a BLEDevice" in caplog.text
assert isinstance(instance, HaBleakClientWrapper)
uninstall_multiple_bleak_catcher()
caplog.clear()
instance = bleak.BleakClient("00:00:00:00:00:00")
assert not isinstance(instance, HaBleakClientWrapper)
assert "BleakClient with an address instead of a BLEDevice" not in caplog.text