From cdca7b2b8e115cdc44011c8959f76c4870213695 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 4 Jan 2024 12:02:55 -1000 Subject: [PATCH] feat: ignore adapters with a 00:00:00:00:00:00 mac address (#105) --- src/bluetooth_adapters/const.py | 2 + src/bluetooth_adapters/systems/linux.py | 8 +- tests/test_init.py | 102 +++++++++++++++++++++++- 3 files changed, 109 insertions(+), 3 deletions(-) diff --git a/src/bluetooth_adapters/const.py b/src/bluetooth_adapters/const.py index 4f88aed..6b14b58 100644 --- a/src/bluetooth_adapters/const.py +++ b/src/bluetooth_adapters/const.py @@ -9,4 +9,6 @@ # Some operating systems hide the adapter address for privacy reasons (ex MacOS) DEFAULT_ADDRESS: Final = "00:00:00:00:00:00" +EMPTY_MAC_ADDRESS: Final = "00:00:00:00:00:00" + DEFAULT_CONNECTION_SLOTS: Final = 5 diff --git a/src/bluetooth_adapters/systems/linux.py b/src/bluetooth_adapters/systems/linux.py index 4f972ae..1b70ed1 100644 --- a/src/bluetooth_adapters/systems/linux.py +++ b/src/bluetooth_adapters/systems/linux.py @@ -10,7 +10,7 @@ from usb_devices import BluetoothDevice, NotAUSBDeviceError from ..adapters import BluetoothAdapters -from ..const import UNIX_DEFAULT_BLUETOOTH_ADAPTER +from ..const import EMPTY_MAC_ADDRESS, UNIX_DEFAULT_BLUETOOTH_ADAPTER from ..dbus import BlueZDBusObjects from ..history import AdvertisementHistory from ..models import AdapterDetails @@ -104,9 +104,13 @@ def adapters(self) -> dict[str, AdapterDetails]: for adapter, details in adapter_details.items(): if not (adapter1 := details.get("org.bluez.Adapter1")): continue + mac_address = adapter1["Address"] + if mac_address == EMPTY_MAC_ADDRESS: + # Ignore adapters with 00:00:00:00:00:00 address + # https://github.com/home-assistant/operating-system/issues/2944 + continue device = self._devices[adapter] usb_device = device.usb_device - mac_address = adapter1["Address"] if ( usb_device is None or usb_device.vendor_id == usb_device.manufacturer diff --git a/tests/test_init.py b/tests/test_init.py index d0652af..87da0c8 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -513,6 +513,92 @@ async def connect(self): "org.freedesktop.DBus.Properties": {}, }, "/org/bluez/hci1": {}, + "/org/bluez/hci2": { + "org.bluez.Adapter1": { + "Address": "00:00:00:00:00:00", + "AddressType": "public", + "Alias": "homeassistant", + "Class": 2883584, + "Discoverable": False, + "DiscoverableTimeout": 180, + "Discovering": True, + "Modalias": "usb:v1D6Bp0246d053F", + "Name": "homeassistant", + "Pairable": False, + "PairableTimeout": 0, + "Powered": True, + "Roles": ["central", "peripheral"], + "UUIDs": [ + "0000110e-0000-1000-8000-00805f9b34fb", + "0000110a-0000-1000-8000-00805f9b34fb", + "00001200-0000-1000-8000-00805f9b34fb", + "0000110b-0000-1000-8000-00805f9b34fb", + "00001108-0000-1000-8000-00805f9b34fb", + "0000110c-0000-1000-8000-00805f9b34fb", + "00001800-0000-1000-8000-00805f9b34fb", + "00001801-0000-1000-8000-00805f9b34fb", + "0000180a-0000-1000-8000-00805f9b34fb", + "00001112-0000-1000-8000-00805f9b34fb", + ], + }, + "org.bluez.GattManager1": {}, + "org.bluez.LEAdvertisingManager1": { + "ActiveInstances": 0, + "SupportedIncludes": [ + "tx-power", + "appearance", + "local-name", + ], + "SupportedInstances": 5, + }, + "org.bluez.Media1": {}, + "org.bluez.NetworkServer1": {}, + "org.freedesktop.DBus.Introspectable": {}, + "org.freedesktop.DBus.Properties": {}, + }, + "/org/bluez/hci3": { + "org.bluez.Adapter1": { + "Address": "00:1A:7D:DA:71:05", + "AddressType": "public", + "Alias": "homeassistant", + "Class": 2883584, + "Discoverable": False, + "DiscoverableTimeout": 180, + "Discovering": True, + "Modalias": "usb:v1D6Bp0246d053F", + "Name": "homeassistant", + "Pairable": False, + "PairableTimeout": 0, + "Powered": True, + "Roles": ["central", "peripheral"], + "UUIDs": [ + "0000110e-0000-1000-8000-00805f9b34fb", + "0000110a-0000-1000-8000-00805f9b34fb", + "00001200-0000-1000-8000-00805f9b34fb", + "0000110b-0000-1000-8000-00805f9b34fb", + "00001108-0000-1000-8000-00805f9b34fb", + "0000110c-0000-1000-8000-00805f9b34fb", + "00001800-0000-1000-8000-00805f9b34fb", + "00001801-0000-1000-8000-00805f9b34fb", + "0000180a-0000-1000-8000-00805f9b34fb", + "00001112-0000-1000-8000-00805f9b34fb", + ], + }, + "org.bluez.GattManager1": {}, + "org.bluez.LEAdvertisingManager1": { + "ActiveInstances": 0, + "SupportedIncludes": [ + "tx-power", + "appearance", + "local-name", + ], + "SupportedInstances": 5, + }, + "org.bluez.Media1": {}, + "org.bluez.NetworkServer1": {}, + "org.freedesktop.DBus.Introspectable": {}, + "org.freedesktop.DBus.Properties": {}, + }, "/org/bluez/hci1/any": {}, "/org/bluez/hci0/dev_54_D2_72_AB_35_95": { "org.freedesktop.DBus.Introspectable": {}, @@ -600,16 +686,30 @@ def setup(self, *args, **kwargs): device=ANY, advertisement_data=ANY, source="hci0" ) } + # hci0 should show + # hci1 is empty so it should not be in the list + # hci2 should not show because it has a 00:00:00:00:00:00 address + # hci3 should show assert bluetooth_adapters.adapters == { "hci0": { "address": "00:1A:7D:DA:71:04", + "hw_version": "usb:v1D6Bp0246d053F", "manufacturer": "XTech", + "passive_scan": False, "product": "Bluetooth 4.0 USB Adapter", - "vendor_id": "0a12", "product_id": "0001", + "sw_version": "homeassistant", + "vendor_id": "0a12", + }, + "hci3": { + "address": "00:1A:7D:DA:71:05", "hw_version": "usb:v1D6Bp0246d053F", + "manufacturer": "XTech", "passive_scan": False, + "product": "Bluetooth 4.0 USB Adapter", + "product_id": "0001", "sw_version": "homeassistant", + "vendor_id": "0a12", }, }