mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
Small cleanups to bluetooth internals (#92045)
* Small cleanups to bluetooth internals Improve the performance of _async_on_advertisement Fixes ``` tests/components/bluetooth/test_models.py::test_ble_device_with_proxy_client_out_of_connections_uses_best_available tests/components/bluetooth/test_models.py::test_ble_device_with_proxy_client_out_of_connections_uses_best_available_macos /Users/bdraco/home-assistant/homeassistant/components/bluetooth/wrappers.py:226: FutureWarning: This method will be removed future version, pass the callback to the BleakClient constructor instead. self._backend.set_disconnected_callback( tests/components/bluetooth/test_models.py::test_ble_device_with_proxy_client_out_of_connections_uses_best_available_macos /Users/bdraco/home-assistant/tests/components/bluetooth/test_models.py:506: FutureWarning: BLEDevice.metadata is deprecated and will be removed in a future version of Bleak, use AdvertisementData instead switchbot_proxy_device_no_connection_slot.metadata["delegate"] = 0 tests/components/bluetooth/test_models.py::test_ble_device_with_proxy_client_out_of_connections_uses_best_available_macos /Users/bdraco/home-assistant/tests/components/bluetooth/test_models.py:521: FutureWarning: BLEDevice.metadata is deprecated and will be removed in a future version of Bleak, use AdvertisementData instead switchbot_proxy_device_has_connection_slot.metadata["delegate"] = 0 tests/components/bluetooth/test_models.py::test_ble_device_with_proxy_client_out_of_connections_uses_best_available_macos /Users/bdraco/home-assistant/tests/components/bluetooth/test_models.py:535: FutureWarning: BLEDevice.metadata is deprecated and will be removed in a future version of Bleak, use AdvertisementData instead switchbot_device.metadata["delegate"] = 0 ``` * put back kwargs
This commit is contained in:
parent
c429bfae3e
commit
6b931b208f
@ -309,43 +309,28 @@ class BaseHaRemoteScanner(BaseHaScanner):
|
|||||||
# merges the dicts on PropertiesChanged
|
# merges the dicts on PropertiesChanged
|
||||||
prev_device = prev_discovery[0]
|
prev_device = prev_discovery[0]
|
||||||
prev_advertisement = prev_discovery[1]
|
prev_advertisement = prev_discovery[1]
|
||||||
if (
|
prev_service_uuids = prev_advertisement.service_uuids
|
||||||
local_name
|
prev_service_data = prev_advertisement.service_data
|
||||||
and prev_device.name
|
prev_manufacturer_data = prev_advertisement.manufacturer_data
|
||||||
and len(prev_device.name) > len(local_name)
|
prev_name = prev_device.name
|
||||||
):
|
|
||||||
local_name = prev_device.name
|
|
||||||
if service_uuids and service_uuids != prev_advertisement.service_uuids:
|
|
||||||
service_uuids = list(
|
|
||||||
set(service_uuids + prev_advertisement.service_uuids)
|
|
||||||
)
|
|
||||||
elif not service_uuids:
|
|
||||||
service_uuids = prev_advertisement.service_uuids
|
|
||||||
if service_data and service_data != prev_advertisement.service_data:
|
|
||||||
service_data = {**prev_advertisement.service_data, **service_data}
|
|
||||||
elif not service_data:
|
|
||||||
service_data = prev_advertisement.service_data
|
|
||||||
if (
|
|
||||||
manufacturer_data
|
|
||||||
and manufacturer_data != prev_advertisement.manufacturer_data
|
|
||||||
):
|
|
||||||
manufacturer_data = {
|
|
||||||
**prev_advertisement.manufacturer_data,
|
|
||||||
**manufacturer_data,
|
|
||||||
}
|
|
||||||
elif not manufacturer_data:
|
|
||||||
manufacturer_data = prev_advertisement.manufacturer_data
|
|
||||||
|
|
||||||
advertisement_data = AdvertisementData(
|
if local_name and prev_name and len(prev_name) > len(local_name):
|
||||||
local_name=None if local_name == "" else local_name,
|
local_name = prev_name
|
||||||
manufacturer_data=manufacturer_data,
|
|
||||||
service_data=service_data,
|
if service_uuids and service_uuids != prev_service_uuids:
|
||||||
service_uuids=service_uuids,
|
service_uuids = list(set(service_uuids + prev_service_uuids))
|
||||||
rssi=rssi,
|
elif not service_uuids:
|
||||||
tx_power=NO_RSSI_VALUE if tx_power is None else tx_power,
|
service_uuids = prev_service_uuids
|
||||||
platform_data=(),
|
|
||||||
)
|
if service_data and service_data != prev_service_data:
|
||||||
if prev_discovery:
|
service_data = prev_service_data | service_data
|
||||||
|
elif not service_data:
|
||||||
|
service_data = prev_service_data
|
||||||
|
|
||||||
|
if manufacturer_data and manufacturer_data != prev_manufacturer_data:
|
||||||
|
manufacturer_data = prev_manufacturer_data | manufacturer_data
|
||||||
|
elif not manufacturer_data:
|
||||||
|
manufacturer_data = prev_manufacturer_data
|
||||||
#
|
#
|
||||||
# Bleak updates the BLEDevice via create_or_update_device.
|
# Bleak updates the BLEDevice via create_or_update_device.
|
||||||
# We need to do the same to ensure integrations that already
|
# We need to do the same to ensure integrations that already
|
||||||
@ -366,6 +351,16 @@ class BaseHaRemoteScanner(BaseHaScanner):
|
|||||||
details=self._details | details,
|
details=self._details | details,
|
||||||
rssi=rssi, # deprecated, will be removed in newer bleak
|
rssi=rssi, # deprecated, will be removed in newer bleak
|
||||||
)
|
)
|
||||||
|
|
||||||
|
advertisement_data = AdvertisementData(
|
||||||
|
local_name=None if local_name == "" else local_name,
|
||||||
|
manufacturer_data=manufacturer_data,
|
||||||
|
service_data=service_data,
|
||||||
|
service_uuids=service_uuids,
|
||||||
|
tx_power=NO_RSSI_VALUE if tx_power is None else tx_power,
|
||||||
|
rssi=rssi,
|
||||||
|
platform_data=(),
|
||||||
|
)
|
||||||
self._discovered_device_advertisement_datas[address] = (
|
self._discovered_device_advertisement_datas[address] = (
|
||||||
device,
|
device,
|
||||||
advertisement_data,
|
advertisement_data,
|
||||||
@ -373,12 +368,12 @@ class BaseHaRemoteScanner(BaseHaScanner):
|
|||||||
self._discovered_device_timestamps[address] = now
|
self._discovered_device_timestamps[address] = now
|
||||||
self._new_info_callback(
|
self._new_info_callback(
|
||||||
BluetoothServiceInfoBleak(
|
BluetoothServiceInfoBleak(
|
||||||
name=advertisement_data.local_name or device.name or device.address,
|
name=local_name or address,
|
||||||
address=device.address,
|
address=address,
|
||||||
rssi=rssi,
|
rssi=rssi,
|
||||||
manufacturer_data=advertisement_data.manufacturer_data,
|
manufacturer_data=manufacturer_data,
|
||||||
service_data=advertisement_data.service_data,
|
service_data=service_data,
|
||||||
service_uuids=advertisement_data.service_uuids,
|
service_uuids=service_uuids,
|
||||||
source=self.source,
|
source=self.source,
|
||||||
device=device,
|
device=device,
|
||||||
advertisement=advertisement_data,
|
advertisement=advertisement_data,
|
||||||
|
@ -503,7 +503,6 @@ async def test_ble_device_with_proxy_client_out_of_connections_uses_best_availab
|
|||||||
},
|
},
|
||||||
rssi=-30,
|
rssi=-30,
|
||||||
)
|
)
|
||||||
switchbot_proxy_device_no_connection_slot.metadata["delegate"] = 0
|
|
||||||
switchbot_proxy_device_no_connection_slot_adv = generate_advertisement_data(
|
switchbot_proxy_device_no_connection_slot_adv = generate_advertisement_data(
|
||||||
local_name="wohand",
|
local_name="wohand",
|
||||||
service_uuids=[],
|
service_uuids=[],
|
||||||
@ -518,7 +517,6 @@ async def test_ble_device_with_proxy_client_out_of_connections_uses_best_availab
|
|||||||
"path": "/org/bluez/hci0/dev_44_44_33_11_23_45",
|
"path": "/org/bluez/hci0/dev_44_44_33_11_23_45",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
switchbot_proxy_device_has_connection_slot.metadata["delegate"] = 0
|
|
||||||
switchbot_proxy_device_has_connection_slot_adv = generate_advertisement_data(
|
switchbot_proxy_device_has_connection_slot_adv = generate_advertisement_data(
|
||||||
local_name="wohand",
|
local_name="wohand",
|
||||||
service_uuids=[],
|
service_uuids=[],
|
||||||
@ -532,7 +530,6 @@ async def test_ble_device_with_proxy_client_out_of_connections_uses_best_availab
|
|||||||
{},
|
{},
|
||||||
rssi=-100,
|
rssi=-100,
|
||||||
)
|
)
|
||||||
switchbot_device.metadata["delegate"] = 0
|
|
||||||
switchbot_device_adv = generate_advertisement_data(
|
switchbot_device_adv = generate_advertisement_data(
|
||||||
local_name="wohand",
|
local_name="wohand",
|
||||||
service_uuids=[],
|
service_uuids=[],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user