|
| 1 | +import asyncio |
1 | 2 | import dataclasses |
2 | 3 | from collections.abc import AsyncGenerator |
3 | 4 |
|
|
11 | 12 |
|
12 | 13 | from bleak import BleakAdapter, BleakClient |
13 | 14 | from bleak.backends.device import BLEDevice |
| 15 | +from bleak.exc import BleakBluetoothNotAvailableError, BleakBluetoothNotAvailableReason |
14 | 16 | from tests.integration.conftest import ( |
15 | 17 | configure_and_power_on_bumble_peripheral, |
16 | 18 | create_bumble_peripheral, |
@@ -99,3 +101,29 @@ async def test_get_connected_devices_filters_by_service_uuid( |
99 | 101 |
|
100 | 102 | not_connected = await adapter.get_connected_devices([OTHER_SERVICE_UUID]) |
101 | 103 | assert not_connected == [] |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.asyncio(loop_scope="module") |
| 107 | +@pytest.mark.usefixtures("hci_transport") |
| 108 | +async def test_get_raises_when_powered_off() -> None: |
| 109 | + """``BleakAdapter.get()`` raises ``BleakBluetoothNotAvailableError`` with |
| 110 | + reason ``POWERED_OFF`` when the local Bluetooth adapter is not powered on. |
| 111 | +
|
| 112 | + This test power-cycles the BlueZ adapter via ``bluetoothctl``, so it must |
| 113 | + be the last test in the module - it leaves the adapter back in |
| 114 | + ``POWERED_ON`` but subsequent tests would race the recovery. |
| 115 | + """ |
| 116 | + proc = await asyncio.create_subprocess_exec("bluetoothctl", "power", "off") |
| 117 | + await proc.wait() |
| 118 | + # Allow the BlueZ PropertiesChanged signal to propagate to the manager's |
| 119 | + # cached properties before calling get(). |
| 120 | + await asyncio.sleep(1.0) |
| 121 | + |
| 122 | + try: |
| 123 | + with pytest.raises(BleakBluetoothNotAvailableError) as exc_info: |
| 124 | + await BleakAdapter.get() |
| 125 | + assert exc_info.value.reason == BleakBluetoothNotAvailableReason.POWERED_OFF |
| 126 | + finally: |
| 127 | + proc = await asyncio.create_subprocess_exec("bluetoothctl", "power", "on") |
| 128 | + await proc.wait() |
| 129 | + await asyncio.sleep(1.0) |
0 commit comments