mirror of
https://github.com/home-assistant/core.git
synced 2026-11-23 14:17:19 -04:00
Add support for bleak passive scanning on linux (#75542)
This commit is contained in:
@@ -28,6 +28,11 @@ def windows_adapter():
|
||||
def one_adapter_fixture():
|
||||
"""Fixture that mocks one adapter on Linux."""
|
||||
with patch(
|
||||
"homeassistant.components.bluetooth.platform.system", return_value="Linux"
|
||||
), patch(
|
||||
"homeassistant.components.bluetooth.scanner.platform.system",
|
||||
return_value="Linux",
|
||||
), patch(
|
||||
"homeassistant.components.bluetooth.util.platform.system", return_value="Linux"
|
||||
), patch(
|
||||
"bluetooth_adapters.get_bluetooth_adapter_details",
|
||||
|
||||
@@ -6,11 +6,13 @@ from homeassistant import config_entries
|
||||
from homeassistant.components.bluetooth.const import (
|
||||
CONF_ADAPTER,
|
||||
CONF_DETAILS,
|
||||
CONF_PASSIVE,
|
||||
DEFAULT_ADDRESS,
|
||||
DOMAIN,
|
||||
AdapterDetails,
|
||||
)
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
@@ -235,3 +237,105 @@ async def test_async_step_integration_discovery_already_exists(hass):
|
||||
)
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_options_flow_linux(hass, mock_bleak_scanner_start, one_adapter):
|
||||
"""Test options on Linux."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={},
|
||||
options={},
|
||||
unique_id="00:00:00:00:00:01",
|
||||
)
|
||||
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_PASSIVE: True,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["data"][CONF_PASSIVE] is True
|
||||
|
||||
# Verify we can change it to False
|
||||
|
||||
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_PASSIVE: False,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["data"][CONF_PASSIVE] is False
|
||||
|
||||
|
||||
@patch(
|
||||
"homeassistant.components.bluetooth.config_flow.platform.system",
|
||||
return_value="Darwin",
|
||||
)
|
||||
async def test_options_flow_disabled_macos(mock_system, hass, hass_ws_client):
|
||||
"""Test options are disabled on MacOS."""
|
||||
await async_setup_component(hass, "config", {})
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={},
|
||||
options={},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
ws_client = await hass_ws_client(hass)
|
||||
|
||||
await ws_client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": "config_entries/get",
|
||||
"domain": "bluetooth",
|
||||
"type_filter": "integration",
|
||||
}
|
||||
)
|
||||
response = await ws_client.receive_json()
|
||||
assert response["result"][0]["supports_options"] is False
|
||||
|
||||
|
||||
@patch(
|
||||
"homeassistant.components.bluetooth.config_flow.platform.system",
|
||||
return_value="Linux",
|
||||
)
|
||||
async def test_options_flow_enabled_linux(mock_system, hass, hass_ws_client):
|
||||
"""Test options are enabled on Linux."""
|
||||
await async_setup_component(hass, "config", {})
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={},
|
||||
options={},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
ws_client = await hass_ws_client(hass)
|
||||
|
||||
await ws_client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": "config_entries/get",
|
||||
"domain": "bluetooth",
|
||||
"type_filter": "integration",
|
||||
}
|
||||
)
|
||||
response = await ws_client.receive_json()
|
||||
assert response["result"][0]["supports_options"] is True
|
||||
|
||||
@@ -20,6 +20,7 @@ from homeassistant.components.bluetooth import (
|
||||
scanner,
|
||||
)
|
||||
from homeassistant.components.bluetooth.const import (
|
||||
CONF_PASSIVE,
|
||||
DEFAULT_ADDRESS,
|
||||
DOMAIN,
|
||||
SOURCE_LOCAL,
|
||||
@@ -61,6 +62,52 @@ async def test_setup_and_stop(hass, mock_bleak_scanner_start, enable_bluetooth):
|
||||
assert len(mock_bleak_scanner_start.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_setup_and_stop_passive(hass, mock_bleak_scanner_start, one_adapter):
|
||||
"""Test we and setup and stop the scanner the passive scanner."""
|
||||
entry = MockConfigEntry(
|
||||
domain=bluetooth.DOMAIN,
|
||||
data={},
|
||||
options={CONF_PASSIVE: True},
|
||||
unique_id="00:00:00:00:00:01",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
init_kwargs = None
|
||||
|
||||
class MockPassiveBleakScanner:
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Init the scanner."""
|
||||
nonlocal init_kwargs
|
||||
init_kwargs = kwargs
|
||||
|
||||
async def start(self, *args, **kwargs):
|
||||
"""Start the scanner."""
|
||||
|
||||
async def stop(self, *args, **kwargs):
|
||||
"""Stop the scanner."""
|
||||
|
||||
def register_detection_callback(self, *args, **kwargs):
|
||||
"""Register a callback."""
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bluetooth.scanner.OriginalBleakScanner",
|
||||
MockPassiveBleakScanner,
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass, bluetooth.DOMAIN, {bluetooth.DOMAIN: {}}
|
||||
)
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert init_kwargs == {
|
||||
"adapter": "hci0",
|
||||
"bluez": scanner.PASSIVE_SCANNER_ARGS,
|
||||
"scanning_mode": "passive",
|
||||
}
|
||||
|
||||
|
||||
async def test_setup_and_stop_no_bluetooth(hass, caplog, macos_adapter):
|
||||
"""Test we fail gracefully when bluetooth is not available."""
|
||||
mock_bt = [
|
||||
|
||||
Reference in New Issue
Block a user