From 481007f09d0faf4e4cd16d8790ce0d4c75620542 Mon Sep 17 00:00:00 2001 From: Javier Torres Date: Thu, 29 Oct 2026 10:03:29 +0100 Subject: [PATCH] Align cloud auth request timeouts with hass-nabucasa (#189309) --- homeassistant/components/cloud/const.py | 9 ++++++++- homeassistant/components/cloud/http_api.py | 15 ++++++++------- homeassistant/components/cloud/subscription.py | 10 +++++----- tests/components/cloud/test_subscription.py | 7 ++++--- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/cloud/const.py b/homeassistant/components/cloud/const.py index 31bd796eea3b..eb1b691c6bf8 100644 --- a/homeassistant/components/cloud/const.py +++ b/homeassistant/components/cloud/const.py @@ -3,6 +3,8 @@ import asyncio from typing import TYPE_CHECKING, Any +from hass_nabucasa.auth.const import AUTH_RENEW_TIMEOUT, AUTH_USER_TIMEOUT + from homeassistant.util.hass_dict import HassKey from homeassistant.util.signal_type import SignalType @@ -24,7 +26,12 @@ DATA_PENDING_AUTO_LOGIN: HassKey[AutoLoginController | None] = HassKey( ) EVENT_CLOUD_EVENT = "cloud_event" -REQUEST_TIMEOUT = 10 +# Timeout for user actions that use the cloud API +USER_REQUEST_TIMEOUT = AUTH_USER_TIMEOUT + 10 +# Timeout for other uses of the cloud API (that may need to renew auth) +CLOUD_REQUEST_TIMEOUT = AUTH_RENEW_TIMEOUT + 10 +# Timeout for other requests that do not use cloud auth +REQUEST_WITHOUT_AUTH_TIMEOUT = 10 PREVIEW_FEATURE_STT_V2 = "stt_v2" diff --git a/homeassistant/components/cloud/http_api.py b/homeassistant/components/cloud/http_api.py index 81d14887e6ac..d787bd6db319 100644 --- a/homeassistant/components/cloud/http_api.py +++ b/homeassistant/components/cloud/http_api.py @@ -66,7 +66,8 @@ from .const import ( PREF_GOOGLE_SECURE_DEVICES_PIN, PREF_REMOTE_ALLOW_REMOTE_ENABLE, PREF_TTS_DEFAULT_VOICE, - REQUEST_TIMEOUT, + REQUEST_WITHOUT_AUTH_TIMEOUT, + USER_REQUEST_TIMEOUT, VOICE_STYLE_SEPERATOR, ) from .google_config import CLOUD_GOOGLE @@ -351,7 +352,7 @@ class CloudLogoutView(HomeAssistantView): hass = request.app[KEY_HASS] cloud = hass.data[DATA_CLOUD] - async with asyncio.timeout(REQUEST_TIMEOUT): + async with asyncio.timeout(REQUEST_WITHOUT_AUTH_TIMEOUT): await cloud.logout() return self.json_message("ok") @@ -399,7 +400,7 @@ class CloudRegisterView(HomeAssistantView): client_metadata = await _async_location_client_metadata(hass) - async with asyncio.timeout(REQUEST_TIMEOUT): + async with asyncio.timeout(USER_REQUEST_TIMEOUT): await cloud.auth.async_register( data["email"], data["password"], @@ -425,7 +426,7 @@ class CloudRegisterAutoLoginView(HomeAssistantView): client_metadata = await _async_location_client_metadata(hass) - async with asyncio.timeout(REQUEST_TIMEOUT): + async with asyncio.timeout(USER_REQUEST_TIMEOUT): hass.data[DATA_PENDING_AUTO_LOGIN] = await cloud.register_and_auto_login( data["email"], data["password"], @@ -449,7 +450,7 @@ class CloudResendConfirmView(HomeAssistantView): hass = request.app[KEY_HASS] cloud = hass.data[DATA_CLOUD] - async with asyncio.timeout(REQUEST_TIMEOUT): + async with asyncio.timeout(USER_REQUEST_TIMEOUT): await cloud.auth.async_resend_email_confirm(data["email"]) return self.json_message("ok") @@ -473,7 +474,7 @@ class CloudForgotPasswordView(HomeAssistantView): hass = request.app[KEY_HASS] cloud = hass.data[DATA_CLOUD] - async with asyncio.timeout(REQUEST_TIMEOUT): + async with asyncio.timeout(USER_REQUEST_TIMEOUT): await cloud.auth.async_forgot_password(data["email"]) return self.json_message("ok") @@ -858,7 +859,7 @@ async def websocket_resend_auto_login_confirm( ) return - async with asyncio.timeout(REQUEST_TIMEOUT): + async with asyncio.timeout(USER_REQUEST_TIMEOUT): await controller.resend() connection.send_result(msg["id"]) diff --git a/homeassistant/components/cloud/subscription.py b/homeassistant/components/cloud/subscription.py index cfc2e40c1ff1..14bf00e845dd 100644 --- a/homeassistant/components/cloud/subscription.py +++ b/homeassistant/components/cloud/subscription.py @@ -11,7 +11,7 @@ from hass_nabucasa import ( ) from .client import CloudClient -from .const import REQUEST_TIMEOUT +from .const import CLOUD_REQUEST_TIMEOUT _LOGGER = logging.getLogger(__name__) @@ -19,7 +19,7 @@ _LOGGER = logging.getLogger(__name__) async def async_subscription_info(cloud: Cloud[CloudClient]) -> SubscriptionInfo | None: """Fetch the subscription info.""" try: - async with asyncio.timeout(REQUEST_TIMEOUT): + async with asyncio.timeout(CLOUD_REQUEST_TIMEOUT): return await cloud.payments.subscription_info() except PaymentsApiError as exception: _LOGGER.error("Failed to fetch subscription information - %s", exception) @@ -27,7 +27,7 @@ async def async_subscription_info(cloud: Cloud[CloudClient]) -> SubscriptionInfo _LOGGER.error( "A timeout of %s was reached while trying to" " fetch subscription information", - REQUEST_TIMEOUT, + CLOUD_REQUEST_TIMEOUT, ) return None @@ -37,12 +37,12 @@ async def async_migrate_paypal_agreement( ) -> MigratePaypalAgreementInfo | None: """Migrate a paypal agreement from legacy.""" try: - async with asyncio.timeout(REQUEST_TIMEOUT): + async with asyncio.timeout(CLOUD_REQUEST_TIMEOUT): return await cloud.payments.migrate_paypal_agreement() except TimeoutError: _LOGGER.error( "A timeout of %s was reached while trying to start agreement migration", - REQUEST_TIMEOUT, + CLOUD_REQUEST_TIMEOUT, ) except PaymentsApiError as exception: _LOGGER.error("Failed to start agreement migration - %s", exception) diff --git a/tests/components/cloud/test_subscription.py b/tests/components/cloud/test_subscription.py index 38c26779e306..c936a9e3b684 100644 --- a/tests/components/cloud/test_subscription.py +++ b/tests/components/cloud/test_subscription.py @@ -7,6 +7,7 @@ from hass_nabucasa import Cloud, payments_api import pytest from pytest_homeassistant.test_util.aiohttp import AiohttpClientMocker +from homeassistant.components.cloud.const import CLOUD_REQUEST_TIMEOUT from homeassistant.components.cloud.subscription import ( async_migrate_paypal_agreement, async_subscription_info, @@ -53,7 +54,7 @@ async def test_fetching_subscription_with_timeout_error( ) -> None: """Test that we handle timeout error.""" mocked_cloud.payments.subscription_info = lambda: asyncio.sleep(1) - with patch("homeassistant.components.cloud.subscription.REQUEST_TIMEOUT", 0): + with patch("homeassistant.components.cloud.subscription.CLOUD_REQUEST_TIMEOUT", 0): assert await async_subscription_info(mocked_cloud) is None assert ( @@ -72,6 +73,6 @@ async def test_migrate_paypal_agreement_with_timeout_error( assert await async_migrate_paypal_agreement(mocked_cloud) is None assert ( - "A timeout of 10 was reached while trying to start agreement migration" - in caplog.text + f"A timeout of {CLOUD_REQUEST_TIMEOUT} was reached while trying to" + " start agreement migration" in caplog.text )