Skip to content

Commit 431f563

Browse files
authored
Add translation of exceptions in WLED (#155570)
Co-authored-by: mik-laj <[email protected]>
1 parent e308e61 commit 431f563

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

homeassistant/components/wled/coordinator.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ async def _async_update_data(self) -> WLEDDevice:
114114
try:
115115
device = await self.wled.update()
116116
except WLEDError as error:
117-
raise UpdateFailed(f"Invalid response from API: {error}") from error
117+
raise UpdateFailed(
118+
translation_domain=DOMAIN,
119+
translation_key="invalid_response_wled_error",
120+
translation_placeholders={"error": str(error)},
121+
) from error
118122

119123
# If the device supports a WebSocket, try activating it.
120124
if (
@@ -146,4 +150,8 @@ async def _async_update_data(self) -> Releases:
146150
try:
147151
return await self.wled.releases()
148152
except WLEDError as error:
149-
raise UpdateFailed(f"Invalid response from GitHub API: {error}") from error
153+
raise UpdateFailed(
154+
translation_domain=DOMAIN,
155+
translation_key="invalid_response_github_error",
156+
translation_placeholders={"error": str(error)},
157+
) from error

homeassistant/components/wled/helpers.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from homeassistant.exceptions import HomeAssistantError
1111

12+
from .const import DOMAIN
1213
from .entity import WLEDEntity
1314

1415

@@ -29,10 +30,17 @@ async def handler(self: _WLEDEntityT, *args: _P.args, **kwargs: _P.kwargs) -> No
2930
except WLEDConnectionError as error:
3031
self.coordinator.last_update_success = False
3132
self.coordinator.async_update_listeners()
32-
raise HomeAssistantError("Error communicating with WLED API") from error
33-
33+
raise HomeAssistantError(
34+
translation_domain=DOMAIN,
35+
translation_key="connection_error",
36+
translation_placeholders={"error": str(error)},
37+
) from error
3438
except WLEDError as error:
35-
raise HomeAssistantError("Invalid response from WLED API") from error
39+
raise HomeAssistantError(
40+
translation_domain=DOMAIN,
41+
translation_key="invalid_response_wled_error",
42+
translation_placeholders={"error": str(error)},
43+
) from error
3644

3745
return handler
3846

homeassistant/components/wled/strings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@
124124
}
125125
}
126126
},
127+
"exceptions": {
128+
"connection_error": {
129+
"message": "Error communicating with WLED API: {error}"
130+
},
131+
"invalid_response_github_error": {
132+
"message": "Invalid response from GitHub API: {error}"
133+
},
134+
"invalid_response_wled_error": {
135+
"message": "Invalid response from WLED API: {error}"
136+
}
137+
},
127138
"options": {
128139
"step": {
129140
"init": {

tests/components/wled/test_button.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from wled import WLEDConnectionError, WLEDError
88

99
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
10+
from homeassistant.components.wled.const import DOMAIN
1011
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, STATE_UNKNOWN
1112
from homeassistant.core import HomeAssistant
1213
from homeassistant.exceptions import HomeAssistantError
@@ -51,28 +52,33 @@ async def test_button_restart(
5152

5253
# Test with WLED error
5354
mock_wled.reset.side_effect = WLEDError
54-
with pytest.raises(HomeAssistantError, match="Invalid response from WLED API"):
55+
with pytest.raises(HomeAssistantError) as ex:
5556
await hass.services.async_call(
5657
BUTTON_DOMAIN,
5758
SERVICE_PRESS,
5859
{ATTR_ENTITY_ID: "button.wled_rgb_light_restart"},
5960
blocking=True,
6061
)
62+
assert ex.value.translation_domain == DOMAIN
63+
assert ex.value.translation_key == "invalid_response_wled_error"
6164

6265
# Ensure this didn't made the entity unavailable
6366
assert (state := hass.states.get("button.wled_rgb_light_restart"))
6467
assert state.state != STATE_UNAVAILABLE
6568

6669
# Test with WLED connection error
6770
mock_wled.reset.side_effect = WLEDConnectionError
68-
with pytest.raises(HomeAssistantError, match="Error communicating with WLED API"):
71+
with pytest.raises(HomeAssistantError) as ex:
6972
await hass.services.async_call(
7073
BUTTON_DOMAIN,
7174
SERVICE_PRESS,
7275
{ATTR_ENTITY_ID: "button.wled_rgb_light_restart"},
7376
blocking=True,
7477
)
7578

79+
assert ex.value.translation_domain == DOMAIN
80+
assert ex.value.translation_key == "connection_error"
81+
7682
# Ensure this made the entity unavailable
7783
assert (state := hass.states.get("button.wled_rgb_light_restart"))
7884
assert state.state == STATE_UNAVAILABLE

tests/components/wled/test_update.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ async def test_update_error(
151151

152152
assert (state := hass.states.get("update.wled_rgb_light_firmware"))
153153
assert state.state == STATE_UNAVAILABLE
154-
assert "Invalid response from API" in caplog.text
154+
assert "Invalid response from WLED API" in caplog.text
155155

156156

157157
async def test_update_stay_stable(

0 commit comments

Comments
 (0)