From 46b18367f7c6f9390a68f2aee0817730670d87a6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 28 Nov 2022 18:54:53 -1000 Subject: [PATCH] Send back BLE address type when connecting via esphome proxies (#82890) --- homeassistant/components/bluetooth/base_scanner.py | 4 +++- homeassistant/components/esphome/bluetooth/client.py | 2 ++ homeassistant/components/esphome/bluetooth/scanner.py | 2 ++ homeassistant/components/esphome/manifest.json | 2 +- homeassistant/components/shelly/bluetooth/scanner.py | 1 + requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/bluetooth/test_base_scanner.py | 4 ++++ tests/components/bluetooth/test_diagnostics.py | 4 ++++ 9 files changed, 19 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/bluetooth/base_scanner.py b/homeassistant/components/bluetooth/base_scanner.py index b8711b0017f..6204897c35c 100644 --- a/homeassistant/components/bluetooth/base_scanner.py +++ b/homeassistant/components/bluetooth/base_scanner.py @@ -73,6 +73,7 @@ class BaseHaScanner: "address": device_adv[0].address, "rssi": device_adv[0].rssi, "advertisement_data": device_adv[1], + "details": device_adv[0].details, } for device_adv in self.discovered_devices_and_advertisement_data.values() ], @@ -162,6 +163,7 @@ class BaseHaRemoteScanner(BaseHaScanner): service_data: dict[str, bytes], manufacturer_data: dict[int, bytes], tx_power: int | None, + details: dict[Any, Any], ) -> None: """Call the registered callback.""" now = MONOTONIC_TIME() @@ -201,7 +203,7 @@ class BaseHaRemoteScanner(BaseHaScanner): device = BLEDevice( # type: ignore[no-untyped-call] address=address, name=local_name, - details=self._details, + details=self._details | details, rssi=rssi, # deprecated, will be removed in newer bleak ) self._discovered_device_advertisement_datas[address] = ( diff --git a/homeassistant/components/esphome/bluetooth/client.py b/homeassistant/components/esphome/bluetooth/client.py index 7bd893f643e..e3730a216b7 100644 --- a/homeassistant/components/esphome/bluetooth/client.py +++ b/homeassistant/components/esphome/bluetooth/client.py @@ -147,6 +147,7 @@ class ESPHomeClient(BaseBleakClient): device_info = self.entry_data.device_info assert device_info is not None self._connection_version = device_info.bluetooth_proxy_version + self._address_type = address_or_ble_device.details["address_type"] def __str__(self) -> str: """Return the string representation of the client.""" @@ -292,6 +293,7 @@ class ESPHomeClient(BaseBleakClient): timeout=timeout, has_cache=has_cache, version=self._connection_version, + address_type=self._address_type, ) ) except Exception: # pylint: disable=broad-except diff --git a/homeassistant/components/esphome/bluetooth/scanner.py b/homeassistant/components/esphome/bluetooth/scanner.py index 3df9a4c6aaa..b6cd3d2ec35 100644 --- a/homeassistant/components/esphome/bluetooth/scanner.py +++ b/homeassistant/components/esphome/bluetooth/scanner.py @@ -27,6 +27,7 @@ class ESPHomeScanner(BaseHaRemoteScanner): adv.service_data, adv.manufacturer_data, None, + {"address_type": adv.address_type}, ) async def async_diagnostics(self) -> dict[str, Any]: @@ -39,6 +40,7 @@ class ESPHomeScanner(BaseHaRemoteScanner): "address": device_adv[0].address, "rssi": device_adv[0].rssi, "advertisement_data": device_adv[1], + "details": device_adv[0].details, } for device_adv in self.discovered_devices_and_advertisement_data.values() ], diff --git a/homeassistant/components/esphome/manifest.json b/homeassistant/components/esphome/manifest.json index b3885326e90..25dc956ea3b 100644 --- a/homeassistant/components/esphome/manifest.json +++ b/homeassistant/components/esphome/manifest.json @@ -3,7 +3,7 @@ "name": "ESPHome", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/esphome", - "requirements": ["aioesphomeapi==12.1.0"], + "requirements": ["aioesphomeapi==12.2.0"], "zeroconf": ["_esphomelib._tcp.local."], "dhcp": [{ "registered_devices": true }], "codeowners": ["@OttoWinter", "@jesserockz"], diff --git a/homeassistant/components/shelly/bluetooth/scanner.py b/homeassistant/components/shelly/bluetooth/scanner.py index a6fb72ecc98..f255d01c78b 100644 --- a/homeassistant/components/shelly/bluetooth/scanner.py +++ b/homeassistant/components/shelly/bluetooth/scanner.py @@ -44,4 +44,5 @@ class ShellyBLEScanner(BaseHaRemoteScanner): parsed.service_data, parsed.manufacturer_data, parsed.tx_power, + {}, ) diff --git a/requirements_all.txt b/requirements_all.txt index 525943690a8..88e519fdc7a 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -156,7 +156,7 @@ aioecowitt==2022.09.3 aioemonitor==1.0.5 # homeassistant.components.esphome -aioesphomeapi==12.1.0 +aioesphomeapi==12.2.0 # homeassistant.components.flo aioflo==2021.11.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 97fdf6a6f3e..6cb0c2eac79 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -143,7 +143,7 @@ aioecowitt==2022.09.3 aioemonitor==1.0.5 # homeassistant.components.esphome -aioesphomeapi==12.1.0 +aioesphomeapi==12.2.0 # homeassistant.components.flo aioflo==2021.11.0 diff --git a/tests/components/bluetooth/test_base_scanner.py b/tests/components/bluetooth/test_base_scanner.py index a4042ada005..19c47361a6a 100644 --- a/tests/components/bluetooth/test_base_scanner.py +++ b/tests/components/bluetooth/test_base_scanner.py @@ -64,6 +64,7 @@ async def test_remote_scanner(hass, enable_bluetooth): advertisement_data.service_data, advertisement_data.manufacturer_data, advertisement_data.tx_power, + {"scanner_specific_data": "test"}, ) new_info_callback = manager.scanner_adv_received @@ -134,6 +135,7 @@ async def test_remote_scanner_expires_connectable(hass, enable_bluetooth): advertisement_data.service_data, advertisement_data.manufacturer_data, advertisement_data.tx_power, + {"scanner_specific_data": "test"}, ) new_info_callback = manager.scanner_adv_received @@ -204,6 +206,7 @@ async def test_remote_scanner_expires_non_connectable(hass, enable_bluetooth): advertisement_data.service_data, advertisement_data.manufacturer_data, advertisement_data.tx_power, + {"scanner_specific_data": "test"}, ) new_info_callback = manager.scanner_adv_received @@ -297,6 +300,7 @@ async def test_base_scanner_connecting_behavior(hass, enable_bluetooth): advertisement_data.service_data, advertisement_data.manufacturer_data, advertisement_data.tx_power, + {"scanner_specific_data": "test"}, ) new_info_callback = manager.scanner_adv_received diff --git a/tests/components/bluetooth/test_diagnostics.py b/tests/components/bluetooth/test_diagnostics.py index bfd39f74303..417375e9820 100644 --- a/tests/components/bluetooth/test_diagnostics.py +++ b/tests/components/bluetooth/test_diagnostics.py @@ -155,6 +155,7 @@ async def test_diagnostics( ], "name": "x", "rssi": -60, + "details": None, } ], "last_detection": ANY, @@ -179,6 +180,7 @@ async def test_diagnostics( ], "name": "x", "rssi": -60, + "details": None, } ], "last_detection": ANY, @@ -203,6 +205,7 @@ async def test_diagnostics( ], "name": "x", "rssi": -60, + "details": None, } ], "last_detection": ANY, @@ -366,6 +369,7 @@ async def test_diagnostics_macos( ], "name": "x", "rssi": -60, + "details": None, } ], "last_detection": ANY,