Add bluetooth options flow to pick the adapter (#75701)

This commit is contained in:
J. Nick Koston
2022-07-25 09:52:35 -05:00
committed by GitHub
parent 3df7892454
commit a813cf987b
15 changed files with 318 additions and 31 deletions

View File

@@ -0,0 +1,27 @@
"""The bluetooth integration utilities."""
from __future__ import annotations
import platform
from .const import MACOS_DEFAULT_BLUETOOTH_ADAPTER, UNIX_DEFAULT_BLUETOOTH_ADAPTER
async def async_get_bluetooth_adapters() -> list[str]:
"""Return a list of bluetooth adapters."""
if platform.system() == "Windows": # We don't have a good way to detect on windows
return []
if platform.system() == "Darwin": # CoreBluetooth is built in on MacOS hardware
return [MACOS_DEFAULT_BLUETOOTH_ADAPTER]
from bluetooth_adapters import ( # pylint: disable=import-outside-toplevel
get_bluetooth_adapters,
)
adapters = await get_bluetooth_adapters()
if (
UNIX_DEFAULT_BLUETOOTH_ADAPTER in adapters
and adapters[0] != UNIX_DEFAULT_BLUETOOTH_ADAPTER
):
# The default adapter always needs to be the first in the list
# because that is how bleak works.
adapters.insert(0, adapters.pop(adapters.index(UNIX_DEFAULT_BLUETOOTH_ADAPTER)))
return adapters