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:
@@ -5,38 +5,88 @@ from unittest.mock import patch
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.bluetooth.const import (
|
||||
CONF_ADAPTER,
|
||||
CONF_DETAILS,
|
||||
DEFAULT_ADDRESS,
|
||||
DOMAIN,
|
||||
MACOS_DEFAULT_BLUETOOTH_ADAPTER,
|
||||
AdapterDetails,
|
||||
)
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_async_step_user(hass):
|
||||
"""Test setting up manually."""
|
||||
async def test_async_step_user_macos(hass, macos_adapter):
|
||||
"""Test setting up manually with one adapter on MacOS."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
data={},
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "enable_bluetooth"
|
||||
assert result["step_id"] == "single_adapter"
|
||||
with patch(
|
||||
"homeassistant.components.bluetooth.async_setup", return_value=True
|
||||
), patch(
|
||||
"homeassistant.components.bluetooth.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={}
|
||||
)
|
||||
assert result2["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "Bluetooth"
|
||||
assert result2["title"] == "Core Bluetooth"
|
||||
assert result2["data"] == {}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_async_step_user_only_allows_one(hass):
|
||||
async def test_async_step_user_linux_one_adapter(hass, one_adapter):
|
||||
"""Test setting up manually with one adapter on Linux."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
data={},
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "single_adapter"
|
||||
with patch(
|
||||
"homeassistant.components.bluetooth.async_setup", return_value=True
|
||||
), patch(
|
||||
"homeassistant.components.bluetooth.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={}
|
||||
)
|
||||
assert result2["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "00:00:00:00:00:01"
|
||||
assert result2["data"] == {}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_async_step_user_linux_two_adapters(hass, two_adapters):
|
||||
"""Test setting up manually with two adapters on Linux."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
data={},
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "multiple_adapters"
|
||||
with patch(
|
||||
"homeassistant.components.bluetooth.async_setup", return_value=True
|
||||
), patch(
|
||||
"homeassistant.components.bluetooth.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_ADAPTER: "hci1"}
|
||||
)
|
||||
assert result2["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "00:00:00:00:00:02"
|
||||
assert result2["data"] == {}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_async_step_user_only_allows_one(hass, macos_adapter):
|
||||
"""Test setting up manually with an existing entry."""
|
||||
entry = MockConfigEntry(domain=DOMAIN)
|
||||
entry = MockConfigEntry(domain=DOMAIN, unique_id=DEFAULT_ADDRESS)
|
||||
entry.add_to_hass(hass)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
@@ -44,34 +94,48 @@ async def test_async_step_user_only_allows_one(hass):
|
||||
data={},
|
||||
)
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
assert result["reason"] == "no_adapters"
|
||||
|
||||
|
||||
async def test_async_step_integration_discovery(hass):
|
||||
"""Test setting up from integration discovery."""
|
||||
|
||||
details = AdapterDetails(
|
||||
address="00:00:00:00:00:01", sw_version="1.23.5", hw_version="1.2.3"
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
|
||||
data={},
|
||||
data={CONF_ADAPTER: "hci0", CONF_DETAILS: details},
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "enable_bluetooth"
|
||||
assert result["step_id"] == "single_adapter"
|
||||
with patch(
|
||||
"homeassistant.components.bluetooth.async_setup", return_value=True
|
||||
), patch(
|
||||
"homeassistant.components.bluetooth.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={}
|
||||
)
|
||||
assert result2["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "Bluetooth"
|
||||
assert result2["title"] == "00:00:00:00:00:01"
|
||||
assert result2["data"] == {}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_async_step_integration_discovery_during_onboarding(hass):
|
||||
async def test_async_step_integration_discovery_during_onboarding_one_adapter(
|
||||
hass, one_adapter
|
||||
):
|
||||
"""Test setting up from integration discovery during onboarding."""
|
||||
details = AdapterDetails(
|
||||
address="00:00:00:00:00:01", sw_version="1.23.5", hw_version="1.2.3"
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bluetooth.async_setup", return_value=True
|
||||
), patch(
|
||||
"homeassistant.components.bluetooth.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry, patch(
|
||||
"homeassistant.components.onboarding.async_is_onboarded",
|
||||
@@ -80,10 +144,77 @@ async def test_async_step_integration_discovery_during_onboarding(hass):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
|
||||
data={},
|
||||
data={CONF_ADAPTER: "hci0", CONF_DETAILS: details},
|
||||
)
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Bluetooth"
|
||||
assert result["title"] == "00:00:00:00:00:01"
|
||||
assert result["data"] == {}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
assert len(mock_onboarding.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_async_step_integration_discovery_during_onboarding_two_adapters(
|
||||
hass, two_adapters
|
||||
):
|
||||
"""Test setting up from integration discovery during onboarding."""
|
||||
details1 = AdapterDetails(
|
||||
address="00:00:00:00:00:01", sw_version="1.23.5", hw_version="1.2.3"
|
||||
)
|
||||
details2 = AdapterDetails(
|
||||
address="00:00:00:00:00:02", sw_version="1.23.5", hw_version="1.2.3"
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bluetooth.async_setup", return_value=True
|
||||
), patch(
|
||||
"homeassistant.components.bluetooth.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry, patch(
|
||||
"homeassistant.components.onboarding.async_is_onboarded",
|
||||
return_value=False,
|
||||
) as mock_onboarding:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
|
||||
data={CONF_ADAPTER: "hci0", CONF_DETAILS: details1},
|
||||
)
|
||||
result2 = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
|
||||
data={CONF_ADAPTER: "hci1", CONF_DETAILS: details2},
|
||||
)
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "00:00:00:00:00:01"
|
||||
assert result["data"] == {}
|
||||
|
||||
assert result2["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "00:00:00:00:00:02"
|
||||
assert result2["data"] == {}
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 2
|
||||
assert len(mock_onboarding.mock_calls) == 2
|
||||
|
||||
|
||||
async def test_async_step_integration_discovery_during_onboarding(hass, macos_adapter):
|
||||
"""Test setting up from integration discovery during onboarding."""
|
||||
details = AdapterDetails(
|
||||
address=DEFAULT_ADDRESS, sw_version="1.23.5", hw_version="1.2.3"
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bluetooth.async_setup", return_value=True
|
||||
), patch(
|
||||
"homeassistant.components.bluetooth.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry, patch(
|
||||
"homeassistant.components.onboarding.async_is_onboarded",
|
||||
return_value=False,
|
||||
) as mock_onboarding:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
|
||||
data={CONF_ADAPTER: "Core Bluetooth", CONF_DETAILS: details},
|
||||
)
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Core Bluetooth"
|
||||
assert result["data"] == {}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
assert len(mock_onboarding.mock_calls) == 1
|
||||
@@ -91,150 +222,16 @@ async def test_async_step_integration_discovery_during_onboarding(hass):
|
||||
|
||||
async def test_async_step_integration_discovery_already_exists(hass):
|
||||
"""Test setting up from integration discovery when an entry already exists."""
|
||||
entry = MockConfigEntry(domain=DOMAIN)
|
||||
details = AdapterDetails(
|
||||
address="00:00:00:00:00:01", sw_version="1.23.5", hw_version="1.2.3"
|
||||
)
|
||||
|
||||
entry = MockConfigEntry(domain=DOMAIN, unique_id="00:00:00:00:00:01")
|
||||
entry.add_to_hass(hass)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
|
||||
data={},
|
||||
data={CONF_ADAPTER: "hci0", CONF_DETAILS: details},
|
||||
)
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_async_step_import(hass):
|
||||
"""Test setting up from integration discovery."""
|
||||
with patch(
|
||||
"homeassistant.components.bluetooth.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={},
|
||||
)
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Bluetooth"
|
||||
assert result["data"] == {}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_async_step_import_already_exists(hass):
|
||||
"""Test setting up from yaml when an entry already exists."""
|
||||
entry = MockConfigEntry(domain=DOMAIN)
|
||||
entry.add_to_hass(hass)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={},
|
||||
)
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
@patch("homeassistant.components.bluetooth.util.platform.system", return_value="Linux")
|
||||
async def test_options_flow_linux(mock_system, hass, mock_bleak_scanner_start):
|
||||
"""Test options on Linux."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={},
|
||||
options={},
|
||||
unique_id="DOMAIN",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
# Verify we can keep it as hci0
|
||||
with patch(
|
||||
"bluetooth_adapters.get_bluetooth_adapters", return_value=["hci0", "hci1"]
|
||||
):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
assert result["errors"] is None
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_ADAPTER: "hci0",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["data"][CONF_ADAPTER] == "hci0"
|
||||
|
||||
# Verify we can change it to hci1
|
||||
with patch(
|
||||
"bluetooth_adapters.get_bluetooth_adapters", return_value=["hci0", "hci1"]
|
||||
):
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
assert result["errors"] is None
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_ADAPTER: "hci1",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["data"][CONF_ADAPTER] == "hci1"
|
||||
|
||||
|
||||
@patch("homeassistant.components.bluetooth.util.platform.system", return_value="Darwin")
|
||||
async def test_options_flow_macos(mock_system, hass, mock_bleak_scanner_start):
|
||||
"""Test options on MacOS."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={},
|
||||
options={},
|
||||
unique_id="DOMAIN",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
assert result["errors"] is None
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_ADAPTER: MACOS_DEFAULT_BLUETOOTH_ADAPTER,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["data"][CONF_ADAPTER] == MACOS_DEFAULT_BLUETOOTH_ADAPTER
|
||||
|
||||
|
||||
@patch(
|
||||
"homeassistant.components.bluetooth.util.platform.system", return_value="Windows"
|
||||
)
|
||||
async def test_options_flow_windows(mock_system, hass, mock_bleak_scanner_start):
|
||||
"""Test options on Windows."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={},
|
||||
options={},
|
||||
unique_id="DOMAIN",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "no_adapters"
|
||||
|
||||
Reference in New Issue
Block a user