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