Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions homeassistant/components/wled/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
14 changes: 11 additions & 3 deletions homeassistant/components/wled/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from homeassistant.exceptions import HomeAssistantError

from .const import DOMAIN
from .entity import WLEDEntity


Expand All @@ -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

Expand Down
11 changes: 11 additions & 0 deletions homeassistant/components/wled/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
10 changes: 8 additions & 2 deletions tests/components/wled/test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,28 +52,33 @@ 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"))
assert state.state != STATE_UNAVAILABLE

# 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,
{ATTR_ENTITY_ID: "button.wled_rgb_light_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
2 changes: 1 addition & 1 deletion tests/components/wled/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading