Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve user-facing error messages in HomeWizard Energy #104547

Merged
merged 1 commit into from
Nov 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions homeassistant/components/homewizard/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from homeassistant.exceptions import HomeAssistantError

from .const import DOMAIN
from .entity import HomeWizardEntity

_HomeWizardEntityT = TypeVar("_HomeWizardEntityT", bound=HomeWizardEntity)
Expand All @@ -30,11 +31,19 @@ async def handler(
try:
await func(self, *args, **kwargs)
except RequestError as ex:
raise HomeAssistantError from ex
raise HomeAssistantError(
"An error occurred while communicating with HomeWizard device",
translation_domain=DOMAIN,
translation_key="communication_error",
) from ex
except DisabledError as ex:
await self.hass.config_entries.async_reload(
self.coordinator.config_entry.entry_id
)
raise HomeAssistantError from ex
raise HomeAssistantError(
"The local API of the HomeWizard device is disabled",
translation_domain=DOMAIN,
translation_key="api_disabled",
) from ex

return handler
8 changes: 8 additions & 0 deletions homeassistant/components/homewizard/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,13 @@
"name": "Cloud connection"
}
}
},
"exceptions": {
"api_disabled": {
"message": "The local API of the HomeWizard device is disabled"
},
"communication_error": {
"message": "An error occurred while communicating with HomeWizard device"
}
}
}
10 changes: 8 additions & 2 deletions tests/components/homewizard/test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ async def test_identify_button(
# Raise RequestError when identify is called
mock_homewizardenergy.identify.side_effect = RequestError()

with pytest.raises(HomeAssistantError):
with pytest.raises(
HomeAssistantError,
match=r"^An error occurred while communicating with HomeWizard device$",
):
await hass.services.async_call(
button.DOMAIN,
button.SERVICE_PRESS,
Expand All @@ -73,7 +76,10 @@ async def test_identify_button(
# Raise RequestError when identify is called
mock_homewizardenergy.identify.side_effect = DisabledError()

with pytest.raises(HomeAssistantError):
with pytest.raises(
HomeAssistantError,
match=r"^The local API of the HomeWizard device is disabled$",
):
await hass.services.async_call(
button.DOMAIN,
button.SERVICE_PRESS,
Expand Down
10 changes: 8 additions & 2 deletions tests/components/homewizard/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ async def test_number_entities(
mock_homewizardenergy.state_set.assert_called_with(brightness=127)

mock_homewizardenergy.state_set.side_effect = RequestError
with pytest.raises(HomeAssistantError):
with pytest.raises(
HomeAssistantError,
match=r"^An error occurred while communicating with HomeWizard device$",
):
await hass.services.async_call(
number.DOMAIN,
SERVICE_SET_VALUE,
Expand All @@ -79,7 +82,10 @@ async def test_number_entities(
)

mock_homewizardenergy.state_set.side_effect = DisabledError
with pytest.raises(HomeAssistantError):
with pytest.raises(
HomeAssistantError,
match=r"^The local API of the HomeWizard device is disabled$",
):
await hass.services.async_call(
number.DOMAIN,
SERVICE_SET_VALUE,
Expand Down
20 changes: 16 additions & 4 deletions tests/components/homewizard/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,21 @@ async def test_switch_entities(
# Test request error handling
mocked_method.side_effect = RequestError

with pytest.raises(HomeAssistantError):
with pytest.raises(
HomeAssistantError,
match=r"^An error occurred while communicating with HomeWizard device$",
):
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)

with pytest.raises(HomeAssistantError):
with pytest.raises(
HomeAssistantError,
match=r"^An error occurred while communicating with HomeWizard device$",
):
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_OFF,
Expand All @@ -139,15 +145,21 @@ async def test_switch_entities(
# Test disabled error handling
mocked_method.side_effect = DisabledError

with pytest.raises(HomeAssistantError):
with pytest.raises(
HomeAssistantError,
match=r"^The local API of the HomeWizard device is disabled$",
):
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)

with pytest.raises(HomeAssistantError):
with pytest.raises(
HomeAssistantError,
match=r"^The local API of the HomeWizard device is disabled$",
):
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_OFF,
Expand Down