-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for recovering failed adapters after reboot (#40)
- Loading branch information
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,3 +49,6 @@ | |
|
||
|
||
UNAVAILABLE_TRACK_SECONDS: Final = 60 * 5 | ||
|
||
|
||
FAILED_ADAPTER_MAC = "00:00:00:00:00:00" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
"""Tests for the manager.""" | ||
|
||
from typing import Any | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from bleak_retry_connector import BleakSlotManager | ||
from bluetooth_adapters import BluetoothAdapters | ||
from bluetooth_adapters.systems.linux import LinuxAdapters | ||
|
||
from habluetooth import ( | ||
BluetoothManager, | ||
set_manager, | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.skipif("platform.system() == 'Windows'") | ||
async def test_async_recover_failed_adapters() -> None: | ||
"""Return the BluetoothManager instance.""" | ||
attempt = 0 | ||
|
||
class MockLinuxAdapters(LinuxAdapters): | ||
@property | ||
def adapters(self) -> dict[str, Any]: | ||
nonlocal attempt | ||
attempt += 1 | ||
|
||
if attempt == 1: | ||
return { | ||
"hci0": { | ||
"address": "00:00:00:00:00:01", | ||
"hw_version": "usb:v1D6Bp0246d053F", | ||
"passive_scan": False, | ||
"sw_version": "homeassistant", | ||
"manufacturer": "ACME", | ||
"product": "Bluetooth Adapter 5.0", | ||
"product_id": "aa01", | ||
"vendor_id": "cc01", | ||
}, | ||
"hci1": { | ||
"address": "00:00:00:00:00:00", | ||
"hw_version": "usb:v1D6Bp0246d053F", | ||
"passive_scan": False, | ||
"sw_version": "homeassistant", | ||
"manufacturer": "ACME", | ||
"product": "Bluetooth Adapter 5.0", | ||
"product_id": "aa01", | ||
"vendor_id": "cc01", | ||
}, | ||
"hci2": { | ||
"address": "00:00:00:00:00:00", | ||
"hw_version": "usb:v1D6Bp0246d053F", | ||
"passive_scan": False, | ||
"sw_version": "homeassistant", | ||
"manufacturer": "ACME", | ||
"product": "Bluetooth Adapter 5.0", | ||
"product_id": "aa01", | ||
"vendor_id": "cc01", | ||
}, | ||
} | ||
|
||
return { | ||
"hci0": { | ||
"address": "00:00:00:00:00:01", | ||
"hw_version": "usb:v1D6Bp0246d053F", | ||
"passive_scan": False, | ||
"sw_version": "homeassistant", | ||
"manufacturer": "ACME", | ||
"product": "Bluetooth Adapter 5.0", | ||
"product_id": "aa01", | ||
"vendor_id": "cc01", | ||
}, | ||
"hci1": { | ||
"address": "00:00:00:00:00:02", | ||
"hw_version": "usb:v1D6Bp0246d053F", | ||
"passive_scan": False, | ||
"sw_version": "homeassistant", | ||
"manufacturer": "ACME", | ||
"product": "Bluetooth Adapter 5.0", | ||
"product_id": "aa01", | ||
"vendor_id": "cc01", | ||
}, | ||
"hci2": { | ||
"address": "00:00:00:00:00:03", | ||
"hw_version": "usb:v1D6Bp0246d053F", | ||
"passive_scan": False, | ||
"sw_version": "homeassistant", | ||
"manufacturer": "ACME", | ||
"product": "Bluetooth Adapter 5.0", | ||
"product_id": "aa01", | ||
"vendor_id": "cc01", | ||
}, | ||
} | ||
|
||
with ( | ||
patch("habluetooth.manager.async_reset_adapter") as mock_async_reset_adapter, | ||
): | ||
adapters = MockLinuxAdapters() | ||
slot_manager = BleakSlotManager() | ||
manager = BluetoothManager(adapters, slot_manager) | ||
await manager.async_setup() | ||
set_manager(manager) | ||
adapter = await manager.async_get_adapter_from_address_or_recover( | ||
"00:00:00:00:00:03" | ||
) | ||
assert adapter == "hci2" | ||
adapter = await manager.async_get_adapter_from_address_or_recover( | ||
"00:00:00:00:00:02" | ||
) | ||
assert adapter == "hci1" | ||
adapter = await manager.async_get_adapter_from_address_or_recover( | ||
"00:00:00:00:00:01" | ||
) | ||
assert adapter == "hci0" | ||
|
||
assert mock_async_reset_adapter.call_count == 2 | ||
assert mock_async_reset_adapter.call_args_list == [ | ||
(("hci1", "00:00:00:00:00:00"),), | ||
(("hci2", "00:00:00:00:00:00"),), | ||
] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_create_manager() -> None: | ||
"""Return the BluetoothManager instance.""" | ||
adapters = BluetoothAdapters() | ||
slot_manager = BleakSlotManager() | ||
manager = BluetoothManager(adapters, slot_manager) | ||
set_manager(manager) | ||
assert manager |