From c91417e71f765e01f43722c6b0f247d650b67f01 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 30 Nov 2022 16:36:02 -1000 Subject: [PATCH] Fix yale access bluetooth locks delaying startup when key changes (#83024) If the keys changed for the yale locks, the locks would be slow to setup. Because august had yalexs_ble as an after dep, it would be waiting with the new keys but not able to setup because it was waiting for the locks to setup which would be trying over and over until they failed because the key had changed out from under it. This change moves some more code into the lib to avoid the dep and allows both to startup at the same time so the cloud service can feed the new keys in if needed without waiting for the lock to fail to setup changelog: https://github.com/bdraco/yalexs-ble/compare/v1.9.8...v1.10.0 --- homeassistant/components/august/__init__.py | 13 ++++++---- homeassistant/components/august/manifest.json | 5 ++-- .../components/yalexs_ble/__init__.py | 25 +------------------ .../components/yalexs_ble/manifest.json | 2 +- requirements_all.txt | 5 +++- requirements_test_all.txt | 5 +++- tests/components/august/test_init.py | 4 +-- 7 files changed, 22 insertions(+), 37 deletions(-) diff --git a/homeassistant/components/august/__init__.py b/homeassistant/components/august/__init__.py index f4a0f57eb76..249d9e51a85 100644 --- a/homeassistant/components/august/__init__.py +++ b/homeassistant/components/august/__init__.py @@ -12,9 +12,9 @@ from yalexs.exceptions import AugustApiAIOHTTPError from yalexs.lock import Lock, LockDetail from yalexs.pubnub_activity import activities_from_pubnub_message from yalexs.pubnub_async import AugustPubNub, async_create_pubnub +from yalexs_ble import YaleXSBLEDiscovery -from homeassistant.components import yalexs_ble -from homeassistant.config_entries import ConfigEntry +from homeassistant.config_entries import SOURCE_INTEGRATION_DISCOVERY, ConfigEntry from homeassistant.const import CONF_PASSWORD from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ( @@ -22,7 +22,7 @@ from homeassistant.exceptions import ( ConfigEntryNotReady, HomeAssistantError, ) -from homeassistant.helpers import device_registry as dr +from homeassistant.helpers import device_registry as dr, discovery_flow from .activity import ActivityStream from .const import DOMAIN, MIN_TIME_BETWEEN_DETAIL_UPDATES, PLATFORMS @@ -38,6 +38,7 @@ API_CACHED_ATTRS = { "lock_status", "lock_status_datetime", } +YALEXS_BLE_DOMAIN = "yalexs_ble" async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: @@ -100,9 +101,11 @@ def _async_trigger_ble_lock_discovery( ): """Update keys for the yalexs-ble integration if available.""" for lock_detail in locks_with_offline_keys: - yalexs_ble.async_discovery( + discovery_flow.async_create_flow( hass, - yalexs_ble.YaleXSBLEDiscovery( + YALEXS_BLE_DOMAIN, + context={"source": SOURCE_INTEGRATION_DISCOVERY}, + data=YaleXSBLEDiscovery( { "name": lock_detail.device_name, "address": lock_detail.mac_address, diff --git a/homeassistant/components/august/manifest.json b/homeassistant/components/august/manifest.json index b7dde070049..5470f03eb4c 100644 --- a/homeassistant/components/august/manifest.json +++ b/homeassistant/components/august/manifest.json @@ -2,7 +2,7 @@ "domain": "august", "name": "August", "documentation": "https://www.home-assistant.io/integrations/august", - "requirements": ["yalexs==1.2.6"], + "requirements": ["yalexs==1.2.6", "yalexs_ble==1.10.0"], "codeowners": ["@bdraco"], "dhcp": [ { @@ -24,6 +24,5 @@ ], "config_flow": true, "iot_class": "cloud_push", - "loggers": ["pubnub", "yalexs"], - "after_dependencies": ["yalexs_ble"] + "loggers": ["pubnub", "yalexs"] } diff --git a/homeassistant/components/yalexs_ble/__init__.py b/homeassistant/components/yalexs_ble/__init__.py index 7a2b3146265..fcecec19e6b 100644 --- a/homeassistant/components/yalexs_ble/__init__.py +++ b/homeassistant/components/yalexs_ble/__init__.py @@ -2,17 +2,15 @@ from __future__ import annotations import asyncio -from typing import TypedDict import async_timeout from yalexs_ble import PushLock, local_name_is_unique from homeassistant.components import bluetooth -from homeassistant.config_entries import SOURCE_INTEGRATION_DISCOVERY, ConfigEntry +from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_ADDRESS, Platform from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady -from homeassistant.helpers import discovery_flow from .const import CONF_KEY, CONF_LOCAL_NAME, CONF_SLOT, DEVICE_TIMEOUT, DOMAIN from .models import YaleXSBLEData @@ -21,27 +19,6 @@ from .util import async_find_existing_service_info, bluetooth_callback_matcher PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.LOCK, Platform.SENSOR] -class YaleXSBLEDiscovery(TypedDict): - """A validated discovery of a Yale XS BLE device.""" - - name: str - address: str - serial: str - key: str - slot: int - - -@callback -def async_discovery(hass: HomeAssistant, discovery: YaleXSBLEDiscovery) -> None: - """Update keys for the yalexs-ble integration if available.""" - discovery_flow.async_create_flow( - hass, - DOMAIN, - context={"source": SOURCE_INTEGRATION_DISCOVERY}, - data=discovery, - ) - - async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Yale Access Bluetooth from a config entry.""" local_name = entry.data[CONF_LOCAL_NAME] diff --git a/homeassistant/components/yalexs_ble/manifest.json b/homeassistant/components/yalexs_ble/manifest.json index 73c802261d4..b54dda37548 100644 --- a/homeassistant/components/yalexs_ble/manifest.json +++ b/homeassistant/components/yalexs_ble/manifest.json @@ -3,7 +3,7 @@ "name": "Yale Access Bluetooth", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/yalexs_ble", - "requirements": ["yalexs-ble==1.9.8"], + "requirements": ["yalexs-ble==1.10.0"], "dependencies": ["bluetooth"], "codeowners": ["@bdraco"], "bluetooth": [ diff --git a/requirements_all.txt b/requirements_all.txt index 2f78334191d..7b17103b48e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2603,11 +2603,14 @@ xs1-api-client==3.0.0 yalesmartalarmclient==0.3.9 # homeassistant.components.yalexs_ble -yalexs-ble==1.9.8 +yalexs-ble==1.10.0 # homeassistant.components.august yalexs==1.2.6 +# homeassistant.components.august +yalexs_ble==1.10.0 + # homeassistant.components.yeelight yeelight==0.7.10 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 9f9a632c43a..b98918720cd 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1813,11 +1813,14 @@ xmltodict==0.13.0 yalesmartalarmclient==0.3.9 # homeassistant.components.yalexs_ble -yalexs-ble==1.9.8 +yalexs-ble==1.10.0 # homeassistant.components.august yalexs==1.2.6 +# homeassistant.components.august +yalexs_ble==1.10.0 + # homeassistant.components.yeelight yeelight==0.7.10 diff --git a/tests/components/august/test_init.py b/tests/components/august/test_init.py index a4bc3d7b16f..ef82efae177 100644 --- a/tests/components/august/test_init.py +++ b/tests/components/august/test_init.py @@ -332,7 +332,7 @@ async def test_load_triggers_ble_discovery(hass): august_lock_without_key = await _mock_operative_august_lock_detail(hass) with patch( - "homeassistant.components.august.yalexs_ble.async_discovery" + "homeassistant.components.august.discovery_flow.async_create_flow" ) as mock_discovery: config_entry = await _create_august_with_devices( hass, [august_lock_with_key, august_lock_without_key] @@ -341,7 +341,7 @@ async def test_load_triggers_ble_discovery(hass): assert config_entry.state is ConfigEntryState.LOADED assert len(mock_discovery.mock_calls) == 1 - assert mock_discovery.mock_calls[0][1][1] == { + assert mock_discovery.mock_calls[0].kwargs["data"] == { "name": "Front Door Lock", "address": None, "serial": "X2FSW05DGA",