Add mock_bluetooth fixture (#75075)
parent
20432ccc76
commit
a3c1926da5
|
@ -1,25 +1 @@
|
|||
"""Tests for the bluetooth component."""
|
||||
|
||||
import threading
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.common import INSTANCES
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def verify_cleanup():
|
||||
"""Verify that the test has cleaned up resources correctly."""
|
||||
threads_before = frozenset(threading.enumerate())
|
||||
|
||||
yield
|
||||
|
||||
if len(INSTANCES) >= 2:
|
||||
count = len(INSTANCES)
|
||||
for inst in INSTANCES:
|
||||
inst.stop()
|
||||
pytest.exit(f"Detected non stopped instances ({count}), aborting test run")
|
||||
|
||||
threads = frozenset(threading.enumerate()) - threads_before
|
||||
for thread in threads:
|
||||
assert isinstance(thread, threading._DummyThread)
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
"""Tests for the Bluetooth integration."""
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import bleak
|
||||
from bleak import BleakError
|
||||
from bleak.backends.scanner import AdvertisementData, BLEDevice
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import bluetooth
|
||||
from homeassistant.components.bluetooth import (
|
||||
|
@ -16,25 +14,6 @@ from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT
|
|||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def mock_bleak_scanner_start():
|
||||
"""Fixture to mock starting the bleak scanner."""
|
||||
scanner = bleak.BleakScanner
|
||||
models.HA_BLEAK_SCANNER = None
|
||||
|
||||
with patch("homeassistant.components.bluetooth.HaBleakScanner.stop"), patch(
|
||||
"homeassistant.components.bluetooth.HaBleakScanner.start",
|
||||
) as mock_bleak_scanner_start:
|
||||
yield mock_bleak_scanner_start
|
||||
|
||||
# We need to drop the stop method from the object since we patched
|
||||
# out start and this fixture will expire before the stop method is called
|
||||
# when EVENT_HOMEASSISTANT_STOP is fired.
|
||||
if models.HA_BLEAK_SCANNER:
|
||||
models.HA_BLEAK_SCANNER.stop = AsyncMock()
|
||||
bleak.BleakScanner = scanner
|
||||
|
||||
|
||||
async def test_setup_and_stop(hass, mock_bleak_scanner_start):
|
||||
"""Test we and setup and stop the scanner."""
|
||||
mock_bt = [
|
||||
|
|
|
@ -167,7 +167,8 @@ def verify_cleanup():
|
|||
pytest.exit(f"Detected non stopped instances ({count}), aborting test run")
|
||||
|
||||
threads = frozenset(threading.enumerate()) - threads_before
|
||||
assert not threads
|
||||
for thread in threads:
|
||||
assert isinstance(thread, threading._DummyThread)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
|
@ -855,3 +856,36 @@ def mock_integration_frame():
|
|||
],
|
||||
):
|
||||
yield correct_frame
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_bleak_scanner_start")
|
||||
def mock_bleak_scanner_start():
|
||||
"""Fixture to mock starting the bleak scanner."""
|
||||
|
||||
# Late imports to avoid loading bleak unless we need it
|
||||
|
||||
import bleak # pylint: disable=import-outside-toplevel
|
||||
|
||||
from homeassistant.components.bluetooth import ( # pylint: disable=import-outside-toplevel
|
||||
models as bluetooth_models,
|
||||
)
|
||||
|
||||
scanner = bleak.BleakScanner
|
||||
bluetooth_models.HA_BLEAK_SCANNER = None
|
||||
|
||||
with patch("homeassistant.components.bluetooth.HaBleakScanner.stop"), patch(
|
||||
"homeassistant.components.bluetooth.HaBleakScanner.start",
|
||||
) as mock_bleak_scanner_start:
|
||||
yield mock_bleak_scanner_start
|
||||
|
||||
# We need to drop the stop method from the object since we patched
|
||||
# out start and this fixture will expire before the stop method is called
|
||||
# when EVENT_HOMEASSISTANT_STOP is fired.
|
||||
if bluetooth_models.HA_BLEAK_SCANNER:
|
||||
bluetooth_models.HA_BLEAK_SCANNER.stop = AsyncMock()
|
||||
bleak.BleakScanner = scanner
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_bluetooth")
|
||||
def mock_bluetooth(mock_bleak_scanner_start):
|
||||
"""Mock out bluetooth from starting."""
|
||||
|
|
Loading…
Reference in New Issue