Move to the new handler for migrate_paypal_agreement (#149934)

This commit is contained in:
Joakim Sørensen
2025-08-04 12:29:11 +01:00
committed by GitHub
parent 0bdf6757c4
commit 46cfdddc80
4 changed files with 20 additions and 12 deletions

View File

@@ -4,11 +4,13 @@ from __future__ import annotations
import asyncio
import logging
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 hass_nabucasa import (
Cloud,
MigratePaypalAgreementInfo,
PaymentsApiError,
SubscriptionInfo,
)
from .client import CloudClient
from .const import REQUEST_TIMEOUT
@@ -29,17 +31,17 @@ async def async_subscription_info(cloud: Cloud[CloudClient]) -> SubscriptionInfo
async def async_migrate_paypal_agreement(
cloud: Cloud[CloudClient],
) -> dict[str, Any] | None:
) -> MigratePaypalAgreementInfo | None:
"""Migrate a paypal agreement from legacy."""
try:
async with asyncio.timeout(REQUEST_TIMEOUT):
return await cloud_api.async_migrate_paypal_agreement(cloud)
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,
)
except ClientError as exception:
except PaymentsApiError as exception:
_LOGGER.error("Failed to start agreement migration - %s", exception)
return None

View File

@@ -74,6 +74,7 @@ async def cloud_fixture() -> AsyncGenerator[MagicMock]:
mock_cloud.payments = MagicMock(
spec=payments_api.PaymentsApi,
subscription_info=AsyncMock(),
migrate_paypal_agreement=AsyncMock(),
)
mock_cloud.ice_servers = MagicMock(
spec=IceServers,

View File

@@ -4,6 +4,7 @@ from datetime import timedelta
from http import HTTPStatus
from unittest.mock import patch
from hass_nabucasa.payments_api import PaymentsApiError
import pytest
from homeassistant.components.cloud.const import DOMAIN
@@ -210,7 +211,13 @@ async def test_legacy_subscription_repair_flow_timeout(
"preview": None,
}
with patch("homeassistant.components.cloud.repairs.MAX_RETRIES", new=0):
with (
patch("homeassistant.components.cloud.repairs.MAX_RETRIES", new=0),
patch(
"hass_nabucasa.payments_api.PaymentsApi.migrate_paypal_agreement",
side_effect=PaymentsApiError("some error", status=403),
),
):
resp = await client.post(f"/api/repairs/issues/fix/{flow_id}")
assert resp.status == HTTPStatus.OK
data = await resp.json()

View File

@@ -25,6 +25,7 @@ async def mocked_cloud_object(hass: HomeAssistant) -> Cloud:
payments=Mock(
spec=payments_api.PaymentsApi,
subscription_info=AsyncMock(),
migrate_paypal_agreement=AsyncMock(),
),
)
@@ -52,10 +53,7 @@ async def test_migrate_paypal_agreement_with_timeout_error(
mocked_cloud: Cloud,
) -> None:
"""Test that we handle timeout error."""
aioclient_mock.post(
"https://accounts.nabucasa.com/payments/migrate_paypal_agreement",
exc=TimeoutError(),
)
mocked_cloud.payments.migrate_paypal_agreement.side_effect = TimeoutError()
assert await async_migrate_paypal_agreement(mocked_cloud) is None
assert (