Use bluetooth address instead of uuid on MacOS (#89926)

pull/89866/merge
J. Nick Koston 2023-03-20 01:07:41 -10:00 committed by GitHub
parent e258f36ded
commit 146a31163c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 3 deletions

View File

@ -91,12 +91,16 @@ def create_bleak_scanner(
"detection_callback": detection_callback, "detection_callback": detection_callback,
"scanning_mode": SCANNING_MODE_TO_BLEAK[scanning_mode], "scanning_mode": SCANNING_MODE_TO_BLEAK[scanning_mode],
} }
if platform.system() == "Linux": system = platform.system()
if system == "Linux":
# Only Linux supports multiple adapters # Only Linux supports multiple adapters
if adapter: if adapter:
scanner_kwargs["adapter"] = adapter scanner_kwargs["adapter"] = adapter
if scanning_mode == BluetoothScanningMode.PASSIVE: if scanning_mode == BluetoothScanningMode.PASSIVE:
scanner_kwargs["bluez"] = PASSIVE_SCANNER_ARGS 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) _LOGGER.debug("Initializing bluetooth scanner with %s", scanner_kwargs)
try: try:

View File

@ -2,7 +2,7 @@
import asyncio import asyncio
from datetime import timedelta from datetime import timedelta
import time import time
from unittest.mock import MagicMock, patch from unittest.mock import ANY, MagicMock, patch
from bleak import BleakError from bleak import BleakError
from bleak.backends.scanner import AdvertisementDataCallback from bleak.backends.scanner import AdvertisementDataCallback
@ -18,6 +18,7 @@ from homeassistant.components.bluetooth.scanner import NEED_RESET_ERRORS
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from . import ( from . import (
@ -27,7 +28,7 @@ from . import (
generate_ble_device, generate_ble_device,
) )
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( async def test_config_entry_can_be_reloaded_when_stop_raises(
@ -580,3 +581,50 @@ async def test_restart_takes_longer_than_watchdog_time(
await hass.async_block_till_done() await hass.async_block_till_done()
assert "already restarting" in caplog.text 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},
}