Improve user-facing error messages in HomeWizard Energy (#104547)

pull/104579/head
Franck Nijhof 2023-11-27 07:47:46 +01:00 committed by GitHub
parent 670e5a2eae
commit 321b24b146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 10 deletions

View File

@ -8,6 +8,7 @@ from homewizard_energy.errors import DisabledError, RequestError
from homeassistant.exceptions import HomeAssistantError
from .const import DOMAIN
from .entity import HomeWizardEntity
_HomeWizardEntityT = TypeVar("_HomeWizardEntityT", bound=HomeWizardEntity)
@ -30,11 +31,19 @@ def homewizard_exception_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

View File

@ -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"
}
}
}

View File

@ -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,
@ -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,

View File

@ -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,
@ -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,

View File

@ -120,7 +120,10 @@ 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,
@ -128,7 +131,10 @@ async def test_switch_entities(
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,
@ -139,7 +145,10 @@ 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,
@ -147,7 +156,10 @@ async def test_switch_entities(
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,