2022-07-08 18:55:31 -05:00
|
|
|
"""Tests for the Bluetooth integration."""
|
2024-03-08 14:50:25 +01:00
|
|
|
|
2022-08-17 11:42:12 -10:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2022-07-08 18:55:31 -05:00
|
|
|
import bleak
|
2023-12-11 10:42:00 -10:00
|
|
|
from habluetooth.usage import (
|
2022-07-22 13:19:53 -05:00
|
|
|
install_multiple_bleak_catcher,
|
|
|
|
uninstall_multiple_bleak_catcher,
|
|
|
|
)
|
2023-12-11 10:42:00 -10:00
|
|
|
from habluetooth.wrappers import HaBleakClientWrapper, HaBleakScannerWrapper
|
2024-06-04 16:26:07 +02:00
|
|
|
import pytest
|
2023-12-11 10:42:00 -10:00
|
|
|
|
2023-02-08 18:08:43 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-07-08 18:55:31 -05:00
|
|
|
|
2023-07-09 10:06:26 -10:00
|
|
|
from . import generate_ble_device
|
2022-08-17 11:42:12 -10:00
|
|
|
|
2023-03-20 01:06:15 -10:00
|
|
|
MOCK_BLE_DEVICE = generate_ble_device(
|
|
|
|
"00:00:00:00:00:00",
|
|
|
|
"any",
|
|
|
|
delegate="",
|
|
|
|
details={"path": "/dev/hci0/device"},
|
|
|
|
rssi=-127,
|
2022-08-17 11:42:12 -10:00
|
|
|
)
|
|
|
|
|
2022-07-08 18:55:31 -05:00
|
|
|
|
2023-02-08 18:08:43 +01:00
|
|
|
async def test_multiple_bleak_scanner_instances(hass: HomeAssistant) -> None:
|
2022-07-22 13:19:53 -05:00
|
|
|
"""Test creating multiple BleakScanners without an integration."""
|
|
|
|
install_multiple_bleak_catcher()
|
2022-07-08 18:55:31 -05:00
|
|
|
|
|
|
|
instance = bleak.BleakScanner()
|
|
|
|
|
|
|
|
assert isinstance(instance, HaBleakScannerWrapper)
|
2022-07-22 13:19:53 -05:00
|
|
|
|
|
|
|
uninstall_multiple_bleak_catcher()
|
|
|
|
|
2022-11-02 14:57:59 +01:00
|
|
|
with patch("bleak.get_platform_scanner_backend_type"):
|
|
|
|
instance = bleak.BleakScanner()
|
2022-07-22 13:19:53 -05:00
|
|
|
|
|
|
|
assert not isinstance(instance, HaBleakScannerWrapper)
|
2022-08-17 11:42:12 -10:00
|
|
|
|
|
|
|
|
2024-06-04 16:26:07 +02:00
|
|
|
@pytest.mark.usefixtures("enable_bluetooth")
|
|
|
|
async def test_wrapping_bleak_client(hass: HomeAssistant) -> None:
|
2022-08-17 11:42:12 -10:00
|
|
|
"""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)
|