diff --git a/homeassistant/components/wled/coordinator.py b/homeassistant/components/wled/coordinator.py index 8e2855e9f05433..24939926a5f365 100644 --- a/homeassistant/components/wled/coordinator.py +++ b/homeassistant/components/wled/coordinator.py @@ -112,7 +112,11 @@ async def _async_update_data(self) -> WLEDDevice: try: device = await self.wled.update() except WLEDError as error: - raise UpdateFailed(f"Invalid response from API: {error}") from error + raise UpdateFailed( + translation_domain=DOMAIN, + translation_key="invalid_response_wled_error", + translation_placeholders={"error": str(error)}, + ) from error # If the device supports a WebSocket, try activating it. if ( @@ -144,4 +148,8 @@ async def _async_update_data(self) -> Releases: try: return await self.wled.releases() except WLEDError as error: - raise UpdateFailed(f"Invalid response from GitHub API: {error}") from error + raise UpdateFailed( + translation_domain=DOMAIN, + translation_key="invalid_response_github_error", + translation_placeholders={"error": str(error)}, + ) from error diff --git a/homeassistant/components/wled/helpers.py b/homeassistant/components/wled/helpers.py index 216dba67c94651..367abf8815a888 100644 --- a/homeassistant/components/wled/helpers.py +++ b/homeassistant/components/wled/helpers.py @@ -9,6 +9,7 @@ from homeassistant.exceptions import HomeAssistantError +from .const import DOMAIN from .entity import WLEDEntity @@ -29,10 +30,17 @@ async def handler(self: _WLEDEntityT, *args: _P.args, **kwargs: _P.kwargs) -> No except WLEDConnectionError as error: self.coordinator.last_update_success = False self.coordinator.async_update_listeners() - raise HomeAssistantError("Error communicating with WLED API") from error - + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="connection_error", + translation_placeholders={"error": str(error)}, + ) from error except WLEDError as error: - raise HomeAssistantError("Invalid response from WLED API") from error + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="invalid_response_wled_error", + translation_placeholders={"error": str(error)}, + ) from error return handler diff --git a/homeassistant/components/wled/strings.json b/homeassistant/components/wled/strings.json index 76ef57652583fb..0e5cb8bb16ddab 100644 --- a/homeassistant/components/wled/strings.json +++ b/homeassistant/components/wled/strings.json @@ -124,6 +124,17 @@ } } }, + "exceptions": { + "connection_error": { + "message": "Error communicating with WLED API: {error}" + }, + "invalid_response_github_error": { + "message": "Invalid response from GitHub API: {error}" + }, + "invalid_response_wled_error": { + "message": "Invalid response from WLED API: {error}" + } + }, "options": { "step": { "init": { diff --git a/tests/components/wled/test_button.py b/tests/components/wled/test_button.py index b3061e6594a4c0..510819da0c7789 100644 --- a/tests/components/wled/test_button.py +++ b/tests/components/wled/test_button.py @@ -7,6 +7,7 @@ from wled import WLEDConnectionError, WLEDError from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS +from homeassistant.components.wled.const import DOMAIN from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, STATE_UNKNOWN from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -51,13 +52,15 @@ async def test_button_restart( # Test with WLED error mock_wled.reset.side_effect = WLEDError - with pytest.raises(HomeAssistantError, match="Invalid response from WLED API"): + with pytest.raises(HomeAssistantError) as ex: await hass.services.async_call( BUTTON_DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: "button.wled_rgb_light_restart"}, blocking=True, ) + assert ex.value.translation_domain == DOMAIN + assert ex.value.translation_key == "invalid_response_wled_error" # Ensure this didn't made the entity unavailable assert (state := hass.states.get("button.wled_rgb_light_restart")) @@ -65,7 +68,7 @@ async def test_button_restart( # Test with WLED connection error mock_wled.reset.side_effect = WLEDConnectionError - with pytest.raises(HomeAssistantError, match="Error communicating with WLED API"): + with pytest.raises(HomeAssistantError) as ex: await hass.services.async_call( BUTTON_DOMAIN, SERVICE_PRESS, @@ -73,6 +76,9 @@ async def test_button_restart( blocking=True, ) + assert ex.value.translation_domain == DOMAIN + assert ex.value.translation_key == "connection_error" + # Ensure this made the entity unavailable assert (state := hass.states.get("button.wled_rgb_light_restart")) assert state.state == STATE_UNAVAILABLE diff --git a/tests/components/wled/test_update.py b/tests/components/wled/test_update.py index a27aa918385ee0..990d9c650f83d2 100644 --- a/tests/components/wled/test_update.py +++ b/tests/components/wled/test_update.py @@ -151,7 +151,7 @@ async def test_update_error( assert (state := hass.states.get("update.wled_rgb_light_firmware")) assert state.state == STATE_UNAVAILABLE - assert "Invalid response from API" in caplog.text + assert "Invalid response from WLED API" in caplog.text async def test_update_stay_stable(