Add translation of exceptions in WLED (#155570)

Co-authored-by: mik-laj <12058428+mik-laj@users.noreply.github.com>
This commit is contained in:
Kamil Breguła
2025-11-03 22:59:08 +01:00
committed by GitHub
co-authored by mik-laj
parent e308e610c6
commit 431f563ff6
5 changed files with 41 additions and 8 deletions
+10 -2
View File
@@ -114,7 +114,11 @@ class WLEDDataUpdateCoordinator(DataUpdateCoordinator[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 (
@@ -146,4 +150,8 @@ class WLEDReleasesDataUpdateCoordinator(DataUpdateCoordinator[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
+11 -3
View File
@@ -9,6 +9,7 @@ from wled import WLEDConnectionError, WLEDError
from homeassistant.exceptions import HomeAssistantError
from .const import DOMAIN
from .entity import WLEDEntity
@@ -29,10 +30,17 @@ def wled_exception_handler[_WLEDEntityT: WLEDEntity, **_P](
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
@@ -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": {
+8 -2
View File
@@ -7,6 +7,7 @@ from syrupy.assertion import SnapshotAssertion
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
+1 -1
View File
@@ -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(