From fec887420d877f0443f57ff5271890e752fc8437 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 9 Dec 2022 15:55:06 -1000 Subject: [PATCH] Fix delay setting up new Yale Access Bluetooth entries (#83683) Entries took a while to setup because of the async_wait_init_flow_finish call in _async_setup_component The delay was so long that users thought the integration was broken We had a wait in place for advertisements to arrive during discovery in case the lock was not yet seen. Since integration discovery is deferred until after startup this wait it no longer needed --- .../components/yalexs_ble/config_flow.py | 12 +++---- homeassistant/components/yalexs_ble/util.py | 19 ---------- .../components/yalexs_ble/test_config_flow.py | 36 +++++++++---------- 3 files changed, 23 insertions(+), 44 deletions(-) diff --git a/homeassistant/components/yalexs_ble/config_flow.py b/homeassistant/components/yalexs_ble/config_flow.py index 5fee6f62848..7845e8aa5e8 100644 --- a/homeassistant/components/yalexs_ble/config_flow.py +++ b/homeassistant/components/yalexs_ble/config_flow.py @@ -1,7 +1,6 @@ """Config flow for Yale Access Bluetooth integration.""" from __future__ import annotations -import asyncio import logging from typing import Any @@ -27,7 +26,7 @@ from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.typing import DiscoveryInfoType from .const import CONF_KEY, CONF_LOCAL_NAME, CONF_SLOT, DOMAIN -from .util import async_get_service_info, human_readable_name +from .util import async_find_existing_service_info, human_readable_name _LOGGER = logging.getLogger(__name__) @@ -110,11 +109,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): ) raise AbortFlow(reason="already_configured") - try: - self._discovery_info = await async_get_service_info( - hass, local_name, address - ) - except asyncio.TimeoutError: + self._discovery_info = async_find_existing_service_info( + hass, local_name, address + ) + if not self._discovery_info: return self.async_abort(reason="no_devices_found") # Integration discovery should abort other flows unless they diff --git a/homeassistant/components/yalexs_ble/util.py b/homeassistant/components/yalexs_ble/util.py index 465f4487c0b..e361e141a42 100644 --- a/homeassistant/components/yalexs_ble/util.py +++ b/homeassistant/components/yalexs_ble/util.py @@ -6,10 +6,8 @@ import platform from yalexs_ble import local_name_is_unique from homeassistant.components.bluetooth import ( - BluetoothScanningMode, BluetoothServiceInfoBleak, async_discovered_service_info, - async_process_advertisements, ) from homeassistant.components.bluetooth.match import ( ADDRESS, @@ -18,8 +16,6 @@ from homeassistant.components.bluetooth.match import ( ) from homeassistant.core import HomeAssistant, callback -from .const import DEVICE_TIMEOUT - def bluetooth_callback_matcher( local_name: str, address: str @@ -51,21 +47,6 @@ def async_find_existing_service_info( return None -async def async_get_service_info( - hass: HomeAssistant, local_name: str, address: str -) -> BluetoothServiceInfoBleak: - """Wait for the service info for the given local_name and address.""" - if service_info := async_find_existing_service_info(hass, local_name, address): - return service_info - return await async_process_advertisements( - hass, - lambda service_info: True, - bluetooth_callback_matcher(local_name, address), - BluetoothScanningMode.ACTIVE, - DEVICE_TIMEOUT, - ) - - def short_address(address: str) -> str: """Convert a Bluetooth address to a short address.""" split_address = address.replace("-", ":").split(":") diff --git a/tests/components/yalexs_ble/test_config_flow.py b/tests/components/yalexs_ble/test_config_flow.py index 64a4e93eae2..7f9c1fdf948 100644 --- a/tests/components/yalexs_ble/test_config_flow.py +++ b/tests/components/yalexs_ble/test_config_flow.py @@ -400,8 +400,8 @@ async def test_bluetooth_step_success(hass: HomeAssistant) -> None: async def test_integration_discovery_success(hass: HomeAssistant) -> None: """Test integration discovery step success path.""" with patch( - "homeassistant.components.yalexs_ble.util.async_process_advertisements", - return_value=YALE_ACCESS_LOCK_DISCOVERY_INFO, + "homeassistant.components.yalexs_ble.util.async_discovered_service_info", + return_value=[YALE_ACCESS_LOCK_DISCOVERY_INFO], ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -443,8 +443,8 @@ async def test_integration_discovery_success(hass: HomeAssistant) -> None: async def test_integration_discovery_device_not_found(hass: HomeAssistant) -> None: """Test integration discovery when the device is not found.""" with patch( - "homeassistant.components.yalexs_ble.util.async_process_advertisements", - side_effect=asyncio.TimeoutError, + "homeassistant.components.yalexs_ble.util.async_discovered_service_info", + return_value=[], ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -483,8 +483,8 @@ async def test_integration_discovery_takes_precedence_over_bluetooth( assert flows[0]["context"]["local_name"] == YALE_ACCESS_LOCK_DISCOVERY_INFO.name with patch( - "homeassistant.components.yalexs_ble.util.async_process_advertisements", - return_value=YALE_ACCESS_LOCK_DISCOVERY_INFO, + "homeassistant.components.yalexs_ble.util.async_discovered_service_info", + return_value=[YALE_ACCESS_LOCK_DISCOVERY_INFO], ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -558,8 +558,8 @@ async def test_integration_discovery_updates_key_unique_local_name( entry.add_to_hass(hass) with patch( - "homeassistant.components.yalexs_ble.util.async_process_advertisements", - return_value=LOCK_DISCOVERY_INFO_UUID_ADDRESS, + "homeassistant.components.yalexs_ble.util.async_discovered_service_info", + return_value=[LOCK_DISCOVERY_INFO_UUID_ADDRESS], ), patch( "homeassistant.components.yalexs_ble.async_setup_entry", return_value=True, @@ -600,8 +600,8 @@ async def test_integration_discovery_updates_key_without_unique_local_name( entry.add_to_hass(hass) with patch( - "homeassistant.components.yalexs_ble.util.async_process_advertisements", - return_value=LOCK_DISCOVERY_INFO_UUID_ADDRESS, + "homeassistant.components.yalexs_ble.util.async_discovered_service_info", + return_value=[LOCK_DISCOVERY_INFO_UUID_ADDRESS], ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -649,8 +649,8 @@ async def test_integration_discovery_updates_key_duplicate_local_name( entry2.add_to_hass(hass) with patch( - "homeassistant.components.yalexs_ble.util.async_process_advertisements", - return_value=LOCK_DISCOVERY_INFO_UUID_ADDRESS, + "homeassistant.components.yalexs_ble.util.async_discovered_service_info", + return_value=[LOCK_DISCOVERY_INFO_UUID_ADDRESS], ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -695,8 +695,8 @@ async def test_integration_discovery_takes_precedence_over_bluetooth_uuid_addres assert flows[0]["context"]["local_name"] == LOCK_DISCOVERY_INFO_UUID_ADDRESS.name with patch( - "homeassistant.components.yalexs_ble.util.async_process_advertisements", - return_value=LOCK_DISCOVERY_INFO_UUID_ADDRESS, + "homeassistant.components.yalexs_ble.util.async_discovered_service_info", + return_value=[LOCK_DISCOVERY_INFO_UUID_ADDRESS], ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -775,8 +775,8 @@ async def test_integration_discovery_takes_precedence_over_bluetooth_non_unique_ assert flows[0]["context"]["local_name"] == OLD_FIRMWARE_LOCK_DISCOVERY_INFO.name with patch( - "homeassistant.components.yalexs_ble.util.async_process_advertisements", - return_value=OLD_FIRMWARE_LOCK_DISCOVERY_INFO, + "homeassistant.components.yalexs_ble.util.async_discovered_service_info", + return_value=[OLD_FIRMWARE_LOCK_DISCOVERY_INFO], ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -851,8 +851,8 @@ async def test_user_is_setting_up_lock_and_discovery_happens_in_the_middle( await valdidate_started.wait() with patch( - "homeassistant.components.yalexs_ble.util.async_process_advertisements", - return_value=LOCK_DISCOVERY_INFO_UUID_ADDRESS, + "homeassistant.components.yalexs_ble.util.async_discovered_service_info", + return_value=[LOCK_DISCOVERY_INFO_UUID_ADDRESS], ): discovery_result = await hass.config_entries.flow.async_init( DOMAIN,