Fix bluetooth tests (#150978)

This commit is contained in:
Erik Montnemery
2025-08-21 09:38:42 +02:00
committed by GitHub
parent 88d853cfbd
commit 210ec28f0a

View File

@@ -1846,19 +1846,30 @@ def mock_bleak_scanner_start() -> Generator[MagicMock]:
# Late imports to avoid loading bleak unless we need it
from habluetooth import scanner as bluetooth_scanner # noqa: PLC0415
from habluetooth import ( # noqa: PLC0415
manager as bluetooth_manager,
scanner as bluetooth_scanner,
)
# 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.
# pylint: disable-next=c-extension-no-member
bluetooth_scanner.OriginalBleakScanner.stop = AsyncMock() # type: ignore[assignment]
# Mock BlueZ management controller
mock_mgmt_bluetooth_ctl = Mock()
mock_mgmt_bluetooth_ctl.setup = AsyncMock(side_effect=OSError("Mocked error"))
with (
patch.object(
bluetooth_scanner.OriginalBleakScanner, # pylint: disable=c-extension-no-member
"start",
) as mock_bleak_scanner_start,
patch.object(bluetooth_scanner, "HaScanner"),
patch.object(
bluetooth_manager, "MGMTBluetoothCtl", return_value=mock_mgmt_bluetooth_ctl
),
):
yield mock_bleak_scanner_start