From 7a7e16bbb6030eb75df509f132d1ac796b638fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Tue, 8 Jul 2025 05:52:41 +0100 Subject: [PATCH] Change how subscription information is fetched (#148337) Co-authored-by: Franck Nijhof --- homeassistant/components/cloud/repairs.py | 6 +++--- homeassistant/components/cloud/subscription.py | 17 +++++------------ tests/components/cloud/conftest.py | 6 +++++- tests/components/cloud/test_http_api.py | 11 ++++++----- tests/components/cloud/test_subscription.py | 13 ++++++++----- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/homeassistant/components/cloud/repairs.py b/homeassistant/components/cloud/repairs.py index fe418fb5340..ed66cb8244f 100644 --- a/homeassistant/components/cloud/repairs.py +++ b/homeassistant/components/cloud/repairs.py @@ -3,8 +3,8 @@ from __future__ import annotations import asyncio -from typing import Any +from hass_nabucasa.payments_api import SubscriptionInfo import voluptuous as vol from homeassistant.components.repairs import ( @@ -26,7 +26,7 @@ MAX_RETRIES = 60 # This allows for 10 minutes of retries @callback def async_manage_legacy_subscription_issue( hass: HomeAssistant, - subscription_info: dict[str, Any], + subscription_info: SubscriptionInfo, ) -> None: """Manage the legacy subscription issue. @@ -50,7 +50,7 @@ class LegacySubscriptionRepairFlow(RepairsFlow): """Handler for an issue fixing flow.""" wait_task: asyncio.Task | None = None - _data: dict[str, Any] | None = None + _data: SubscriptionInfo | None = None async def async_step_init(self, _: None = None) -> FlowResult: """Handle the first step of a fix flow.""" diff --git a/homeassistant/components/cloud/subscription.py b/homeassistant/components/cloud/subscription.py index dc6679a6e40..9ee154dbff4 100644 --- a/homeassistant/components/cloud/subscription.py +++ b/homeassistant/components/cloud/subscription.py @@ -8,6 +8,7 @@ from typing import Any from aiohttp.client_exceptions import ClientError from hass_nabucasa import Cloud, cloud_api +from hass_nabucasa.payments_api import PaymentsApiError, SubscriptionInfo from .client import CloudClient from .const import REQUEST_TIMEOUT @@ -15,21 +16,13 @@ from .const import REQUEST_TIMEOUT _LOGGER = logging.getLogger(__name__) -async def async_subscription_info(cloud: Cloud[CloudClient]) -> dict[str, Any] | None: +async def async_subscription_info(cloud: Cloud[CloudClient]) -> SubscriptionInfo | None: """Fetch the subscription info.""" try: async with asyncio.timeout(REQUEST_TIMEOUT): - return await cloud_api.async_subscription_info(cloud) - except TimeoutError: - _LOGGER.error( - ( - "A timeout of %s was reached while trying to fetch subscription" - " information" - ), - REQUEST_TIMEOUT, - ) - except ClientError: - _LOGGER.error("Failed to fetch subscription information") + return await cloud.payments.subscription_info() + except PaymentsApiError as exception: + _LOGGER.error("Failed to fetch subscription information - %s", exception) return None diff --git a/tests/components/cloud/conftest.py b/tests/components/cloud/conftest.py index 0e118f251de..e63af0ced09 100644 --- a/tests/components/cloud/conftest.py +++ b/tests/components/cloud/conftest.py @@ -5,7 +5,7 @@ from pathlib import Path from typing import Any from unittest.mock import DEFAULT, AsyncMock, MagicMock, PropertyMock, patch -from hass_nabucasa import Cloud +from hass_nabucasa import Cloud, payments_api from hass_nabucasa.auth import CognitoAuth from hass_nabucasa.cloudhooks import Cloudhooks from hass_nabucasa.const import DEFAULT_SERVERS, DEFAULT_VALUES, STATE_CONNECTED @@ -71,6 +71,10 @@ async def cloud_fixture() -> AsyncGenerator[MagicMock]: mock_cloud.voice = MagicMock(spec=Voice) mock_cloud.files = MagicMock(spec=Files) mock_cloud.started = None + mock_cloud.payments = MagicMock( + spec=payments_api.PaymentsApi, + subscription_info=AsyncMock(), + ) mock_cloud.ice_servers = MagicMock( spec=IceServers, async_register_ice_servers_listener=AsyncMock( diff --git a/tests/components/cloud/test_http_api.py b/tests/components/cloud/test_http_api.py index 79764e552c7..84630bc0320 100644 --- a/tests/components/cloud/test_http_api.py +++ b/tests/components/cloud/test_http_api.py @@ -18,6 +18,7 @@ from hass_nabucasa.auth import ( UnknownError, ) from hass_nabucasa.const import STATE_CONNECTED +from hass_nabucasa.payments_api import PaymentsApiError from hass_nabucasa.remote import CertificateStatus import pytest from syrupy.assertion import SnapshotAssertion @@ -1008,16 +1009,14 @@ async def test_websocket_subscription_info( cloud: MagicMock, setup_cloud: None, ) -> None: - """Test subscription info and connecting because valid account.""" - aioclient_mock.get(SUBSCRIPTION_INFO_URL, json={"provider": "stripe"}) + """Test subscription info.""" + cloud.payments.subscription_info.return_value = {"provider": "stripe"} client = await hass_ws_client(hass) - mock_renew = cloud.auth.async_renew_access_token await client.send_json({"id": 5, "type": "cloud/subscription"}) response = await client.receive_json() assert response["result"] == {"provider": "stripe"} - assert mock_renew.call_count == 1 async def test_websocket_subscription_fail( @@ -1028,7 +1027,9 @@ async def test_websocket_subscription_fail( setup_cloud: None, ) -> None: """Test subscription info fail.""" - aioclient_mock.get(SUBSCRIPTION_INFO_URL, status=HTTPStatus.INTERNAL_SERVER_ERROR) + cloud.payments.subscription_info.side_effect = PaymentsApiError( + "Failed to fetch subscription information" + ) client = await hass_ws_client(hass) await client.send_json({"id": 5, "type": "cloud/subscription"}) diff --git a/tests/components/cloud/test_subscription.py b/tests/components/cloud/test_subscription.py index 22839b585fd..c34ca1bc871 100644 --- a/tests/components/cloud/test_subscription.py +++ b/tests/components/cloud/test_subscription.py @@ -2,7 +2,7 @@ from unittest.mock import AsyncMock, Mock -from hass_nabucasa import Cloud +from hass_nabucasa import Cloud, payments_api import pytest from homeassistant.components.cloud.subscription import ( @@ -22,6 +22,10 @@ async def mocked_cloud_object(hass: HomeAssistant) -> Cloud: accounts_server="accounts.nabucasa.com", auth=Mock(async_check_token=AsyncMock()), websession=async_get_clientsession(hass), + payments=Mock( + spec=payments_api.PaymentsApi, + subscription_info=AsyncMock(), + ), ) @@ -31,14 +35,13 @@ async def test_fetching_subscription_with_timeout_error( mocked_cloud: Cloud, ) -> None: """Test that we handle timeout error.""" - aioclient_mock.get( - "https://accounts.nabucasa.com/payments/subscription_info", - exc=TimeoutError(), + mocked_cloud.payments.subscription_info.side_effect = payments_api.PaymentsApiError( + "Timeout reached while calling API" ) assert await async_subscription_info(mocked_cloud) is None assert ( - "A timeout of 10 was reached while trying to fetch subscription information" + "Failed to fetch subscription information - Timeout reached while calling API" in caplog.text )