This commit is contained in:
J. Nick Koston
2023-12-05 12:01:41 -10:00
parent 62c4d0818c
commit 1008165b54
8 changed files with 24 additions and 82 deletions

View File

@@ -22,6 +22,8 @@ from bluetooth_adapters import (
get_adapters,
)
from habluetooth import (
BaseHaScanner,
BluetoothScannerDevice,
BluetoothScanningMode,
HaBluetoothConnector,
HaScanner,
@@ -66,11 +68,7 @@ from .api import (
async_set_fallback_availability_interval,
async_track_unavailable,
)
from .base_scanner import (
BaseHaScanner,
BluetoothScannerDevice,
HomeAssistantRemoteScanner,
)
from .base_scanner import HomeAssistantRemoteScanner
from .const import (
BLUETOOTH_DISCOVERY_COOLDOWN_SECONDS,
CONF_ADAPTER,

View File

@@ -8,8 +8,7 @@ from typing import Final
from bleak_retry_connector import BleakSlotManager
from bluetooth_adapters import BluetoothAdapters
from bluetooth_data_tools import monotonic_time_coarse
from habluetooth import BluetoothManager
from habluetooth import BluetoothManager, manager
from homeassistant import config_entries
from homeassistant.const import EVENT_LOGGING_CHANGED
@@ -35,7 +34,7 @@ from .models import BluetoothCallback, BluetoothChange, BluetoothServiceInfoBlea
from .storage import BluetoothStorage
from .util import async_load_history_from_system
MONOTONIC_TIME: Final = monotonic_time_coarse
MONOTONIC_TIME: Final = manager.MONOTONIC_TIME
_LOGGER = logging.getLogger(__name__)

View File

@@ -10,6 +10,7 @@ from unittest.mock import MagicMock
from bleak import BleakClient
from bleak.backends.scanner import AdvertisementData, BLEDevice
from bluetooth_adapters import DEFAULT_ADDRESS
from habluetooth import BaseHaScanner, BluetoothManager
from homeassistant.components.bluetooth import (
DOMAIN,
@@ -19,8 +20,6 @@ from homeassistant.components.bluetooth import (
async_get_advertisement_callback,
models,
)
from homeassistant.components.bluetooth.base_scanner import BaseHaScanner
from homeassistant.components.bluetooth.manager import BluetoothManager
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

View File

@@ -8,6 +8,7 @@ from bleak import BleakError
from bleak.backends.scanner import AdvertisementData, BLEDevice
from bluetooth_adapters import DEFAULT_ADDRESS
from habluetooth import scanner
from habluetooth.wrappers import HaBleakScannerWrapper
import pytest
from homeassistant.components import bluetooth
@@ -35,7 +36,6 @@ from homeassistant.components.bluetooth.match import (
SERVICE_DATA_UUID,
SERVICE_UUID,
)
from homeassistant.components.bluetooth.wrappers import HaBleakScannerWrapper
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant, callback

View File

@@ -7,6 +7,7 @@ from unittest.mock import patch
from bleak.backends.scanner import AdvertisementData, BLEDevice
from bluetooth_adapters import AdvertisementHistory
from habluetooth.manager import FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS
import pytest
from homeassistant.components import bluetooth
@@ -31,9 +32,6 @@ from homeassistant.components.bluetooth.const import (
SOURCE_LOCAL,
UNAVAILABLE_TRACK_SECONDS,
)
from homeassistant.components.bluetooth.manager import (
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util

View File

@@ -7,6 +7,7 @@ import bleak
from bleak import BleakError
from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData
from habluetooth.wrappers import HaBleakClientWrapper, HaBleakScannerWrapper
import pytest
from homeassistant.components.bluetooth import (
@@ -14,10 +15,6 @@ from homeassistant.components.bluetooth import (
HaBluetoothConnector,
HomeAssistantRemoteScanner,
)
from homeassistant.components.bluetooth.wrappers import (
HaBleakClientWrapper,
HaBleakScannerWrapper,
)
from homeassistant.core import HomeAssistant
from . import (

View File

@@ -2,17 +2,12 @@
from unittest.mock import patch
import bleak
import bleak_retry_connector
import pytest
from homeassistant.components.bluetooth.usage import (
from habluetooth.usage import (
install_multiple_bleak_catcher,
uninstall_multiple_bleak_catcher,
)
from homeassistant.components.bluetooth.wrappers import (
HaBleakClientWrapper,
HaBleakScannerWrapper,
)
from habluetooth.wrappers import HaBleakClientWrapper, HaBleakScannerWrapper
from homeassistant.core import HomeAssistant
from . import generate_ble_device
@@ -57,47 +52,3 @@ async def test_wrapping_bleak_client(
instance = bleak.BleakClient(MOCK_BLE_DEVICE)
assert not isinstance(instance, HaBleakClientWrapper)
async def test_bleak_client_reports_with_address(
hass: HomeAssistant, enable_bluetooth: None, caplog: pytest.LogCaptureFixture
) -> None:
"""Test we report when we pass an address to BleakClient."""
install_multiple_bleak_catcher()
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
async def test_bleak_retry_connector_client_reports_with_address(
hass: HomeAssistant, enable_bluetooth: None, caplog: pytest.LogCaptureFixture
) -> None:
"""Test we report when we pass an address to BleakClientWithServiceCache."""
install_multiple_bleak_catcher()
instance = bleak_retry_connector.BleakClientWithServiceCache("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_retry_connector.BleakClientWithServiceCache("00:00:00:00:00:00")
assert not isinstance(instance, HaBleakClientWrapper)
assert "BleakClient with an address instead of a BLEDevice" not in caplog.text

View File

@@ -8,6 +8,10 @@ import bleak
from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData
from bleak.exc import BleakError
from habluetooth.usage import (
install_multiple_bleak_catcher,
uninstall_multiple_bleak_catcher,
)
import pytest
from homeassistant.components.bluetooth import (
@@ -17,10 +21,6 @@ from homeassistant.components.bluetooth import (
HomeAssistantRemoteScanner,
async_get_advertisement_callback,
)
from homeassistant.components.bluetooth.usage import (
install_multiple_bleak_catcher,
uninstall_multiple_bleak_catcher,
)
from homeassistant.core import HomeAssistant
from . import _get_manager, generate_advertisement_data, generate_ble_device
@@ -133,7 +133,7 @@ def install_bleak_catcher_fixture():
def mock_platform_client_fixture():
"""Fixture that mocks the platform client."""
with patch(
"homeassistant.components.bluetooth.wrappers.get_platform_client_backend_type",
"habluetooth.wrappers.get_platform_client_backend_type",
return_value=FakeBleakClient,
):
yield
@@ -143,7 +143,7 @@ def mock_platform_client_fixture():
def mock_platform_client_that_fails_to_connect_fixture():
"""Fixture that mocks the platform client that fails to connect."""
with patch(
"homeassistant.components.bluetooth.wrappers.get_platform_client_backend_type",
"habluetooth.wrappers.get_platform_client_backend_type",
return_value=FakeBleakClientFailsToConnect,
):
yield
@@ -153,7 +153,7 @@ def mock_platform_client_that_fails_to_connect_fixture():
def mock_platform_client_that_raises_on_connect_fixture():
"""Fixture that mocks the platform client that fails to connect."""
with patch(
"homeassistant.components.bluetooth.wrappers.get_platform_client_backend_type",
"habluetooth.wrappers.get_platform_client_backend_type",
return_value=FakeBleakClientRaisesOnConnect,
):
yield
@@ -332,27 +332,27 @@ async def test_we_switch_adapters_on_failure(
return True
with patch(
"homeassistant.components.bluetooth.wrappers.get_platform_client_backend_type",
"habluetooth.wrappers.get_platform_client_backend_type",
return_value=FakeBleakClientFailsHCI0Only,
):
assert await client.connect() is False
with patch(
"homeassistant.components.bluetooth.wrappers.get_platform_client_backend_type",
"habluetooth.wrappers.get_platform_client_backend_type",
return_value=FakeBleakClientFailsHCI0Only,
):
assert await client.connect() is False
# After two tries we should switch to hci1
with patch(
"homeassistant.components.bluetooth.wrappers.get_platform_client_backend_type",
"habluetooth.wrappers.get_platform_client_backend_type",
return_value=FakeBleakClientFailsHCI0Only,
):
assert await client.connect() is True
# ..and we remember that hci1 works as long as the client doesn't change
with patch(
"homeassistant.components.bluetooth.wrappers.get_platform_client_backend_type",
"habluetooth.wrappers.get_platform_client_backend_type",
return_value=FakeBleakClientFailsHCI0Only,
):
assert await client.connect() is True
@@ -361,7 +361,7 @@ async def test_we_switch_adapters_on_failure(
client = bleak.BleakClient(ble_device)
with patch(
"homeassistant.components.bluetooth.wrappers.get_platform_client_backend_type",
"habluetooth.wrappers.get_platform_client_backend_type",
return_value=FakeBleakClientFailsHCI0Only,
):
assert await client.connect() is False