Handle Bluetooth adapters in a crashed state (#115790)
* Skip bluetooth discovery for Bluetooth adapters in a crashed state * fixes * fixes * adjust * coverage * coverage * fix race
This commit is contained in:
parent
05c37648c4
commit
1d6ae01baa
5 changed files with 103 additions and 1 deletions
|
@ -196,6 +196,17 @@ async def _async_start_adapter_discovery(
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Set up the bluetooth integration."""
|
"""Set up the bluetooth integration."""
|
||||||
|
if platform.system() == "Linux":
|
||||||
|
# Remove any config entries that are using the default address
|
||||||
|
# that were created from discovering adapters in a crashed state
|
||||||
|
#
|
||||||
|
# DEFAULT_ADDRESS is perfectly valid on MacOS but on
|
||||||
|
# Linux it means the adapter is not yet configured
|
||||||
|
# or crashed
|
||||||
|
for entry in list(hass.config_entries.async_entries(DOMAIN)):
|
||||||
|
if entry.unique_id == DEFAULT_ADDRESS:
|
||||||
|
await hass.config_entries.async_remove(entry.entry_id)
|
||||||
|
|
||||||
bluetooth_adapters = get_adapters()
|
bluetooth_adapters = get_adapters()
|
||||||
bluetooth_storage = BluetoothStorage(hass)
|
bluetooth_storage = BluetoothStorage(hass)
|
||||||
slot_manager = BleakSlotManager()
|
slot_manager = BleakSlotManager()
|
||||||
|
@ -257,13 +268,19 @@ async def async_discover_adapters(
|
||||||
adapters: dict[str, AdapterDetails],
|
adapters: dict[str, AdapterDetails],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Discover adapters and start flows."""
|
"""Discover adapters and start flows."""
|
||||||
if platform.system() == "Windows":
|
system = platform.system()
|
||||||
|
if system == "Windows":
|
||||||
# We currently do not have a good way to detect if a bluetooth device is
|
# We currently do not have a good way to detect if a bluetooth device is
|
||||||
# available on Windows. We will just assume that it is not unless they
|
# available on Windows. We will just assume that it is not unless they
|
||||||
# actively add it.
|
# actively add it.
|
||||||
return
|
return
|
||||||
|
|
||||||
for adapter, details in adapters.items():
|
for adapter, details in adapters.items():
|
||||||
|
if system == "Linux" and details[ADAPTER_ADDRESS] == DEFAULT_ADDRESS:
|
||||||
|
# DEFAULT_ADDRESS is perfectly valid on MacOS but on
|
||||||
|
# Linux it means the adapter is not yet configured
|
||||||
|
# or crashed so we should not try to start a flow for it.
|
||||||
|
continue
|
||||||
discovery_flow.async_create_flow(
|
discovery_flow.async_create_flow(
|
||||||
hass,
|
hass,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
|
|
@ -2,11 +2,13 @@
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import platform
|
||||||
from typing import Any, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
from bluetooth_adapters import (
|
from bluetooth_adapters import (
|
||||||
ADAPTER_ADDRESS,
|
ADAPTER_ADDRESS,
|
||||||
ADAPTER_MANUFACTURER,
|
ADAPTER_MANUFACTURER,
|
||||||
|
DEFAULT_ADDRESS,
|
||||||
AdapterDetails,
|
AdapterDetails,
|
||||||
adapter_human_name,
|
adapter_human_name,
|
||||||
adapter_model,
|
adapter_model,
|
||||||
|
@ -133,10 +135,15 @@ class BluetoothConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
bluetooth_adapters = get_adapters()
|
bluetooth_adapters = get_adapters()
|
||||||
await bluetooth_adapters.refresh()
|
await bluetooth_adapters.refresh()
|
||||||
self._adapters = bluetooth_adapters.adapters
|
self._adapters = bluetooth_adapters.adapters
|
||||||
|
system = platform.system()
|
||||||
unconfigured_adapters = [
|
unconfigured_adapters = [
|
||||||
adapter
|
adapter
|
||||||
for adapter, details in self._adapters.items()
|
for adapter, details in self._adapters.items()
|
||||||
if details[ADAPTER_ADDRESS] not in configured_addresses
|
if details[ADAPTER_ADDRESS] not in configured_addresses
|
||||||
|
# DEFAULT_ADDRESS is perfectly valid on MacOS but on
|
||||||
|
# Linux it means the adapter is not yet configured
|
||||||
|
# or crashed
|
||||||
|
and not (system == "Linux" and details[ADAPTER_ADDRESS] == DEFAULT_ADDRESS)
|
||||||
]
|
]
|
||||||
if not unconfigured_adapters:
|
if not unconfigured_adapters:
|
||||||
ignored_adapters = len(
|
ignored_adapters = len(
|
||||||
|
|
|
@ -219,6 +219,45 @@ def two_adapters_fixture():
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="crashed_adapter")
|
||||||
|
def crashed_adapter_fixture():
|
||||||
|
"""Fixture that mocks one crashed adapter on Linux."""
|
||||||
|
with (
|
||||||
|
patch(
|
||||||
|
"homeassistant.components.bluetooth.platform.system",
|
||||||
|
return_value="Linux",
|
||||||
|
),
|
||||||
|
patch(
|
||||||
|
"habluetooth.scanner.platform.system",
|
||||||
|
return_value="Linux",
|
||||||
|
),
|
||||||
|
patch(
|
||||||
|
"bluetooth_adapters.systems.platform.system",
|
||||||
|
return_value="Linux",
|
||||||
|
),
|
||||||
|
patch("habluetooth.scanner.SYSTEM", "Linux"),
|
||||||
|
patch(
|
||||||
|
"bluetooth_adapters.systems.linux.LinuxAdapters.refresh",
|
||||||
|
),
|
||||||
|
patch(
|
||||||
|
"bluetooth_adapters.systems.linux.LinuxAdapters.adapters",
|
||||||
|
{
|
||||||
|
"hci0": {
|
||||||
|
"address": "00:00:00:00:00:00",
|
||||||
|
"hw_version": "usb:v1D6Bp0246d053F",
|
||||||
|
"passive_scan": True,
|
||||||
|
"sw_version": "homeassistant",
|
||||||
|
"manufacturer": None,
|
||||||
|
"product": None,
|
||||||
|
"product_id": None,
|
||||||
|
"vendor_id": None,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
):
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="one_adapter_old_bluez")
|
@pytest.fixture(name="one_adapter_old_bluez")
|
||||||
def one_adapter_old_bluez():
|
def one_adapter_old_bluez():
|
||||||
"""Fixture that mocks two adapters on Linux."""
|
"""Fixture that mocks two adapters on Linux."""
|
||||||
|
|
|
@ -32,6 +32,9 @@ async def test_options_flow_disabled_not_setup(
|
||||||
domain=DOMAIN, data={}, options={}, unique_id=DEFAULT_ADDRESS
|
domain=DOMAIN, data={}, options={}, unique_id=DEFAULT_ADDRESS
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
ws_client = await hass_ws_client(hass)
|
ws_client = await hass_ws_client(hass)
|
||||||
|
|
||||||
await ws_client.send_json(
|
await ws_client.send_json(
|
||||||
|
@ -103,6 +106,19 @@ async def test_async_step_user_linux_one_adapter(
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
async def test_async_step_user_linux_crashed_adapter(
|
||||||
|
hass: HomeAssistant, crashed_adapter: None
|
||||||
|
) -> None:
|
||||||
|
"""Test setting up manually with one crashed adapter on Linux."""
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
context={"source": config_entries.SOURCE_USER},
|
||||||
|
data={},
|
||||||
|
)
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "no_adapters"
|
||||||
|
|
||||||
|
|
||||||
async def test_async_step_user_linux_two_adapters(
|
async def test_async_step_user_linux_two_adapters(
|
||||||
hass: HomeAssistant, two_adapters: None
|
hass: HomeAssistant, two_adapters: None
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
|
@ -2807,6 +2807,19 @@ async def test_can_unsetup_bluetooth_single_adapter_macos(
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
|
||||||
|
async def test_default_address_config_entries_removed_linux(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_bleak_scanner_start: MagicMock,
|
||||||
|
one_adapter: None,
|
||||||
|
) -> None:
|
||||||
|
"""Test default address entries are removed on linux."""
|
||||||
|
entry = MockConfigEntry(domain=bluetooth.DOMAIN, data={}, unique_id=DEFAULT_ADDRESS)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
await async_setup_component(hass, bluetooth.DOMAIN, {})
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert not hass.config_entries.async_entries(bluetooth.DOMAIN)
|
||||||
|
|
||||||
|
|
||||||
async def test_can_unsetup_bluetooth_single_adapter_linux(
|
async def test_can_unsetup_bluetooth_single_adapter_linux(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_bleak_scanner_start: MagicMock,
|
mock_bleak_scanner_start: MagicMock,
|
||||||
|
@ -2889,6 +2902,16 @@ async def test_auto_detect_bluetooth_adapters_linux_multiple(
|
||||||
assert len(hass.config_entries.flow.async_progress(bluetooth.DOMAIN)) == 2
|
assert len(hass.config_entries.flow.async_progress(bluetooth.DOMAIN)) == 2
|
||||||
|
|
||||||
|
|
||||||
|
async def test_auto_detect_bluetooth_adapters_skips_crashed(
|
||||||
|
hass: HomeAssistant, crashed_adapter: None
|
||||||
|
) -> None:
|
||||||
|
"""Test we skip crashed adapters on linux."""
|
||||||
|
assert await async_setup_component(hass, bluetooth.DOMAIN, {})
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert not hass.config_entries.async_entries(bluetooth.DOMAIN)
|
||||||
|
assert len(hass.config_entries.flow.async_progress(bluetooth.DOMAIN)) == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_auto_detect_bluetooth_adapters_linux_none_found(
|
async def test_auto_detect_bluetooth_adapters_linux_none_found(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue