From 6e63c17b396a6d7ed024fa2a6da581445ef8853a Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Tue, 8 Jul 2025 18:58:48 +0300 Subject: [PATCH] Improve exceptions in Alexa Devices (#148260) --- .../components/alexa_devices/config_flow.py | 11 ++++++++++- .../components/alexa_devices/coordinator.py | 14 ++++++++++++-- .../components/alexa_devices/strings.json | 5 +++-- homeassistant/components/alexa_devices/utils.py | 4 ++-- tests/components/alexa_devices/test_config_flow.py | 9 ++++++++- tests/components/alexa_devices/test_utils.py | 4 ++-- 6 files changed, 37 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/alexa_devices/config_flow.py b/homeassistant/components/alexa_devices/config_flow.py index aa9bbb4ae5e..5ee3bc2e5f0 100644 --- a/homeassistant/components/alexa_devices/config_flow.py +++ b/homeassistant/components/alexa_devices/config_flow.py @@ -6,7 +6,12 @@ from collections.abc import Mapping from typing import Any from aioamazondevices.api import AmazonEchoApi -from aioamazondevices.exceptions import CannotAuthenticate, CannotConnect, WrongCountry +from aioamazondevices.exceptions import ( + CannotAuthenticate, + CannotConnect, + CannotRetrieveData, + WrongCountry, +) import voluptuous as vol from homeassistant.config_entries import ConfigFlow, ConfigFlowResult @@ -57,6 +62,8 @@ class AmazonDevicesConfigFlow(ConfigFlow, domain=DOMAIN): errors["base"] = "cannot_connect" except CannotAuthenticate: errors["base"] = "invalid_auth" + except CannotRetrieveData: + errors["base"] = "cannot_retrieve_data" except WrongCountry: errors["base"] = "wrong_country" else: @@ -106,6 +113,8 @@ class AmazonDevicesConfigFlow(ConfigFlow, domain=DOMAIN): errors["base"] = "cannot_connect" except CannotAuthenticate: errors["base"] = "invalid_auth" + except CannotRetrieveData: + errors["base"] = "cannot_retrieve_data" else: return self.async_update_reload_and_abort( reauth_entry, diff --git a/homeassistant/components/alexa_devices/coordinator.py b/homeassistant/components/alexa_devices/coordinator.py index 031f52abebf..7af66f4bb8b 100644 --- a/homeassistant/components/alexa_devices/coordinator.py +++ b/homeassistant/components/alexa_devices/coordinator.py @@ -52,8 +52,18 @@ class AmazonDevicesCoordinator(DataUpdateCoordinator[dict[str, AmazonDevice]]): try: await self.api.login_mode_stored_data() return await self.api.get_devices_data() - except (CannotConnect, CannotRetrieveData) as err: - raise UpdateFailed(f"Error occurred while updating {self.name}") from err + except CannotConnect as err: + raise UpdateFailed( + translation_domain=DOMAIN, + translation_key="cannot_connect_with_error", + translation_placeholders={"error": repr(err)}, + ) from err + except CannotRetrieveData as err: + raise UpdateFailed( + translation_domain=DOMAIN, + translation_key="cannot_retrieve_data_with_error", + translation_placeholders={"error": repr(err)}, + ) from err except CannotAuthenticate as err: raise ConfigEntryAuthFailed( translation_domain=DOMAIN, diff --git a/homeassistant/components/alexa_devices/strings.json b/homeassistant/components/alexa_devices/strings.json index 03a6cc3de64..19cc39cab42 100644 --- a/homeassistant/components/alexa_devices/strings.json +++ b/homeassistant/components/alexa_devices/strings.json @@ -43,6 +43,7 @@ }, "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", + "cannot_retrieve_data": "Unable to retrieve data from Amazon. Please try again later.", "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", "wrong_country": "Wrong country selected. Please select the country where your Amazon account is registered.", "unknown": "[%key:common::config_flow::error::unknown%]" @@ -84,10 +85,10 @@ } }, "exceptions": { - "cannot_connect": { + "cannot_connect_with_error": { "message": "Error connecting: {error}" }, - "cannot_retrieve_data": { + "cannot_retrieve_data_with_error": { "message": "Error retrieving data: {error}" } } diff --git a/homeassistant/components/alexa_devices/utils.py b/homeassistant/components/alexa_devices/utils.py index 4d1365d1d41..437b681413b 100644 --- a/homeassistant/components/alexa_devices/utils.py +++ b/homeassistant/components/alexa_devices/utils.py @@ -26,14 +26,14 @@ def alexa_api_call[_T: AmazonEntity, **_P]( self.coordinator.last_update_success = False raise HomeAssistantError( translation_domain=DOMAIN, - translation_key="cannot_connect", + translation_key="cannot_connect_with_error", translation_placeholders={"error": repr(err)}, ) from err except CannotRetrieveData as err: self.coordinator.last_update_success = False raise HomeAssistantError( translation_domain=DOMAIN, - translation_key="cannot_retrieve_data", + translation_key="cannot_retrieve_data_with_error", translation_placeholders={"error": repr(err)}, ) from err diff --git a/tests/components/alexa_devices/test_config_flow.py b/tests/components/alexa_devices/test_config_flow.py index def3a6ec547..e1b2974184b 100644 --- a/tests/components/alexa_devices/test_config_flow.py +++ b/tests/components/alexa_devices/test_config_flow.py @@ -2,7 +2,12 @@ from unittest.mock import AsyncMock -from aioamazondevices.exceptions import CannotAuthenticate, CannotConnect, WrongCountry +from aioamazondevices.exceptions import ( + CannotAuthenticate, + CannotConnect, + CannotRetrieveData, + WrongCountry, +) import pytest from homeassistant.components.alexa_devices.const import CONF_LOGIN_DATA, DOMAIN @@ -57,6 +62,7 @@ async def test_full_flow( [ (CannotConnect, "cannot_connect"), (CannotAuthenticate, "invalid_auth"), + (CannotRetrieveData, "cannot_retrieve_data"), (WrongCountry, "wrong_country"), ], ) @@ -165,6 +171,7 @@ async def test_reauth_successful( [ (CannotConnect, "cannot_connect"), (CannotAuthenticate, "invalid_auth"), + (CannotRetrieveData, "cannot_retrieve_data"), ], ) async def test_reauth_not_successful( diff --git a/tests/components/alexa_devices/test_utils.py b/tests/components/alexa_devices/test_utils.py index 12009719a2f..1cf190bd297 100644 --- a/tests/components/alexa_devices/test_utils.py +++ b/tests/components/alexa_devices/test_utils.py @@ -21,8 +21,8 @@ ENTITY_ID = "switch.echo_test_do_not_disturb" @pytest.mark.parametrize( ("side_effect", "key", "error"), [ - (CannotConnect, "cannot_connect", "CannotConnect()"), - (CannotRetrieveData, "cannot_retrieve_data", "CannotRetrieveData()"), + (CannotConnect, "cannot_connect_with_error", "CannotConnect()"), + (CannotRetrieveData, "cannot_retrieve_data_with_error", "CannotRetrieveData()"), ], ) async def test_alexa_api_call_exceptions(