diff --git a/homeassistant/components/homewizard/helpers.py b/homeassistant/components/homewizard/helpers.py index 3f7fc064931..4f12a4f9726 100644 --- a/homeassistant/components/homewizard/helpers.py +++ b/homeassistant/components/homewizard/helpers.py @@ -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 diff --git a/homeassistant/components/homewizard/strings.json b/homeassistant/components/homewizard/strings.json index 3bc55b3c848..acdb321d6ff 100644 --- a/homeassistant/components/homewizard/strings.json +++ b/homeassistant/components/homewizard/strings.json @@ -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" + } } } diff --git a/tests/components/homewizard/test_button.py b/tests/components/homewizard/test_button.py index 25ef73e1459..c25a4ed0f4e 100644 --- a/tests/components/homewizard/test_button.py +++ b/tests/components/homewizard/test_button.py @@ -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, diff --git a/tests/components/homewizard/test_number.py b/tests/components/homewizard/test_number.py index 9af4cac665c..ebd8d80ece2 100644 --- a/tests/components/homewizard/test_number.py +++ b/tests/components/homewizard/test_number.py @@ -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, diff --git a/tests/components/homewizard/test_switch.py b/tests/components/homewizard/test_switch.py index 0b88f1ca949..2f6e777a3a8 100644 --- a/tests/components/homewizard/test_switch.py +++ b/tests/components/homewizard/test_switch.py @@ -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,