1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Add support for multiple Bluetooth adapters (#76963)

This commit is contained in:
J. Nick Koston
2022-08-18 15:41:07 -10:00
committed by GitHub
parent a434d755b3
commit cd59d3ab81
17 changed files with 738 additions and 489 deletions

View File

@@ -11,23 +11,20 @@ from dbus_next import InvalidMessageError
from homeassistant.components import bluetooth
from homeassistant.components.bluetooth.const import (
CONF_ADAPTER,
SCANNER_WATCHDOG_INTERVAL,
SCANNER_WATCHDOG_TIMEOUT,
UNIX_DEFAULT_BLUETOOTH_ADAPTER,
)
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from . import _get_manager
from . import _get_manager, async_setup_with_one_adapter
from tests.common import MockConfigEntry, async_fire_time_changed
from tests.common import async_fire_time_changed
async def test_config_entry_can_be_reloaded_when_stop_raises(
hass, caplog, enable_bluetooth
hass, caplog, enable_bluetooth, macos_adapter
):
"""Test we can reload if stopping the scanner raises."""
entry = hass.config_entries.async_entries(bluetooth.DOMAIN)[0]
@@ -44,31 +41,7 @@ async def test_config_entry_can_be_reloaded_when_stop_raises(
assert "Error stopping scanner" in caplog.text
async def test_changing_the_adapter_at_runtime(hass):
"""Test we can change the adapter at runtime."""
entry = MockConfigEntry(
domain=bluetooth.DOMAIN,
data={},
options={CONF_ADAPTER: UNIX_DEFAULT_BLUETOOTH_ADAPTER},
)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.bluetooth.scanner.OriginalBleakScanner.start"
), patch("homeassistant.components.bluetooth.scanner.OriginalBleakScanner.stop"):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
entry.options = {CONF_ADAPTER: "hci1"}
await hass.config_entries.async_reload(entry.entry_id)
await hass.async_block_till_done()
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
async def test_dbus_socket_missing_in_container(hass, caplog):
async def test_dbus_socket_missing_in_container(hass, caplog, one_adapter):
"""Test we handle dbus being missing in the container."""
with patch(
@@ -77,10 +50,8 @@ async def test_dbus_socket_missing_in_container(hass, caplog):
"homeassistant.components.bluetooth.scanner.OriginalBleakScanner.start",
side_effect=FileNotFoundError,
):
assert await async_setup_component(
hass, bluetooth.DOMAIN, {bluetooth.DOMAIN: {}}
)
await hass.async_block_till_done()
await async_setup_with_one_adapter(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
@@ -90,7 +61,7 @@ async def test_dbus_socket_missing_in_container(hass, caplog):
assert "docker" in caplog.text
async def test_dbus_socket_missing(hass, caplog):
async def test_dbus_socket_missing(hass, caplog, one_adapter):
"""Test we handle dbus being missing."""
with patch(
@@ -99,10 +70,8 @@ async def test_dbus_socket_missing(hass, caplog):
"homeassistant.components.bluetooth.scanner.OriginalBleakScanner.start",
side_effect=FileNotFoundError,
):
assert await async_setup_component(
hass, bluetooth.DOMAIN, {bluetooth.DOMAIN: {}}
)
await hass.async_block_till_done()
await async_setup_with_one_adapter(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
@@ -112,7 +81,7 @@ async def test_dbus_socket_missing(hass, caplog):
assert "docker" not in caplog.text
async def test_dbus_broken_pipe_in_container(hass, caplog):
async def test_dbus_broken_pipe_in_container(hass, caplog, one_adapter):
"""Test we handle dbus broken pipe in the container."""
with patch(
@@ -121,10 +90,8 @@ async def test_dbus_broken_pipe_in_container(hass, caplog):
"homeassistant.components.bluetooth.scanner.OriginalBleakScanner.start",
side_effect=BrokenPipeError,
):
assert await async_setup_component(
hass, bluetooth.DOMAIN, {bluetooth.DOMAIN: {}}
)
await hass.async_block_till_done()
await async_setup_with_one_adapter(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
@@ -135,7 +102,7 @@ async def test_dbus_broken_pipe_in_container(hass, caplog):
assert "container" in caplog.text
async def test_dbus_broken_pipe(hass, caplog):
async def test_dbus_broken_pipe(hass, caplog, one_adapter):
"""Test we handle dbus broken pipe."""
with patch(
@@ -144,10 +111,8 @@ async def test_dbus_broken_pipe(hass, caplog):
"homeassistant.components.bluetooth.scanner.OriginalBleakScanner.start",
side_effect=BrokenPipeError,
):
assert await async_setup_component(
hass, bluetooth.DOMAIN, {bluetooth.DOMAIN: {}}
)
await hass.async_block_till_done()
await async_setup_with_one_adapter(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
@@ -158,17 +123,15 @@ async def test_dbus_broken_pipe(hass, caplog):
assert "container" not in caplog.text
async def test_invalid_dbus_message(hass, caplog):
async def test_invalid_dbus_message(hass, caplog, one_adapter):
"""Test we handle invalid dbus message."""
with patch(
"homeassistant.components.bluetooth.scanner.OriginalBleakScanner.start",
side_effect=InvalidMessageError,
):
assert await async_setup_component(
hass, bluetooth.DOMAIN, {bluetooth.DOMAIN: {}}
)
await hass.async_block_till_done()
await async_setup_with_one_adapter(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
@@ -177,7 +140,7 @@ async def test_invalid_dbus_message(hass, caplog):
assert "dbus" in caplog.text
async def test_recovery_from_dbus_restart(hass):
async def test_recovery_from_dbus_restart(hass, one_adapter):
"""Test we can recover when DBus gets restarted out from under us."""
called_start = 0
@@ -213,10 +176,8 @@ async def test_recovery_from_dbus_restart(hass):
"homeassistant.components.bluetooth.scanner.OriginalBleakScanner",
return_value=scanner,
):
assert await async_setup_component(
hass, bluetooth.DOMAIN, {bluetooth.DOMAIN: {}}
)
await hass.async_block_till_done()
await async_setup_with_one_adapter(hass)
assert called_start == 1
start_time_monotonic = 1000