Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bluetooth address instead of uuid on MacOS #89926

Merged
merged 3 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion homeassistant/components/bluetooth/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,16 @@ def create_bleak_scanner(
"detection_callback": detection_callback,
"scanning_mode": SCANNING_MODE_TO_BLEAK[scanning_mode],
}
if platform.system() == "Linux":
system = platform.system()
if system == "Linux":
# Only Linux supports multiple adapters
if adapter:
scanner_kwargs["adapter"] = adapter
if scanning_mode == BluetoothScanningMode.PASSIVE:
scanner_kwargs["bluez"] = PASSIVE_SCANNER_ARGS
elif system == "Darwin":
# We want mac address on macOS
scanner_kwargs["cb"] = {"use_bdaddr": True}
_LOGGER.debug("Initializing bluetooth scanner with %s", scanner_kwargs)

try:
Expand Down
52 changes: 50 additions & 2 deletions tests/components/bluetooth/test_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import asyncio
from datetime import timedelta
import time
from unittest.mock import MagicMock, patch
from unittest.mock import ANY, MagicMock, patch

from bleak import BleakError
from bleak.backends.scanner import AdvertisementDataCallback, BLEDevice
Expand All @@ -18,11 +18,12 @@
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util

from . import _get_manager, async_setup_with_one_adapter, generate_advertisement_data

from tests.common import async_fire_time_changed
from tests.common import MockConfigEntry, async_fire_time_changed


async def test_config_entry_can_be_reloaded_when_stop_raises(
Expand Down Expand Up @@ -575,3 +576,50 @@ def register_detection_callback(self, callback: AdvertisementDataCallback):
await hass.async_block_till_done()

assert "already restarting" in caplog.text


async def test_setup_and_stop_macos(
hass: HomeAssistant, mock_bleak_scanner_start: MagicMock, macos_adapter: None
) -> None:
"""Test we enable use_bdaddr on MacOS."""
entry = MockConfigEntry(
domain=bluetooth.DOMAIN,
data={},
unique_id="00:00:00:00:00:00",
)
entry.add_to_hass(hass)
init_kwargs = None

class MockBleakScanner:
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",
MockBleakScanner,
):
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 == {
"detection_callback": ANY,
"scanning_mode": "active",
"cb": {"use_bdaddr": True},
}