diff --git a/homeassistant/components/lametric/coordinator.py b/homeassistant/components/lametric/coordinator.py index 17f34632e868..b355085eb0ab 100644 --- a/homeassistant/components/lametric/coordinator.py +++ b/homeassistant/components/lametric/coordinator.py @@ -42,5 +42,7 @@ class LaMetricDataUpdateCoordinator(DataUpdateCoordinator[Device]): raise ConfigEntryAuthFailed from err except LaMetricError as ex: raise UpdateFailed( - "Could not fetch device information from LaMetric device" + translation_domain=DOMAIN, + translation_key="update_failed", + translation_placeholders={"error": str(ex)}, ) from ex diff --git a/homeassistant/components/lametric/notify.py b/homeassistant/components/lametric/notify.py index 7972486d58f1..3615711197e2 100644 --- a/homeassistant/components/lametric/notify.py +++ b/homeassistant/components/lametric/notify.py @@ -27,7 +27,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.util.enum import try_parse_enum -from .const import CONF_CYCLES, CONF_ICON_TYPE, CONF_PRIORITY, CONF_SOUND +from .const import CONF_CYCLES, CONF_ICON_TYPE, CONF_PRIORITY, CONF_SOUND, DOMAIN from .coordinator import LaMetricConfigEntry, LaMetricDataUpdateCoordinator from .entity import LaMetricEntity from .helpers import lametric_exception_handler @@ -102,7 +102,11 @@ class LaMetricNotificationService(BaseNotificationService): if (snd := try_parse_enum(AlarmSound, data[CONF_SOUND])) is None and ( snd := try_parse_enum(NotificationSound, data[CONF_SOUND]) ) is None: - raise ServiceValidationError("Unknown sound provided") + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="unknown_sound", + translation_placeholders={"sound": str(data[CONF_SOUND])}, + ) sound = Sound(sound=snd, category=None) notification = Notification( @@ -123,4 +127,8 @@ class LaMetricNotificationService(BaseNotificationService): try: await self.lametric.notify(notification=notification) except LaMetricError as ex: - raise HomeAssistantError("Could not send LaMetric notification") from ex + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="notification_failed", + translation_placeholders={"error": str(ex)}, + ) from ex diff --git a/homeassistant/components/lametric/quality_scale.yaml b/homeassistant/components/lametric/quality_scale.yaml index 13dd11626a5c..e8a5d5fb8887 100644 --- a/homeassistant/components/lametric/quality_scale.yaml +++ b/homeassistant/components/lametric/quality_scale.yaml @@ -66,7 +66,7 @@ rules: entity-device-class: done entity-disabled-by-default: done entity-translations: done - exception-translations: todo + exception-translations: done icon-translations: done reconfiguration-flow: todo repair-issues: diff --git a/homeassistant/components/lametric/services.py b/homeassistant/components/lametric/services.py index 384676824e9a..50f7f48cca0d 100644 --- a/homeassistant/components/lametric/services.py +++ b/homeassistant/components/lametric/services.py @@ -124,7 +124,11 @@ async def async_send_notification( if (snd := try_parse_enum(AlarmSound, call.data[CONF_SOUND])) is None and ( snd := try_parse_enum(NotificationSound, call.data[CONF_SOUND]) ) is None: - raise ServiceValidationError("Unknown sound provided") + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="unknown_sound", + translation_placeholders={"sound": str(call.data[CONF_SOUND])}, + ) sound = Sound(sound=snd, category=None) notification = Notification( @@ -140,4 +144,8 @@ async def async_send_notification( try: await coordinator.lametric.notify(notification=notification) except LaMetricError as ex: - raise HomeAssistantError("Could not send LaMetric notification") from ex + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="notification_failed", + translation_placeholders={"error": str(ex)}, + ) from ex diff --git a/homeassistant/components/lametric/strings.json b/homeassistant/components/lametric/strings.json index 68198bf68605..f181d9981812 100644 --- a/homeassistant/components/lametric/strings.json +++ b/homeassistant/components/lametric/strings.json @@ -112,6 +112,15 @@ }, "invalid_response": { "message": "Invalid response from the LaMetric device" + }, + "notification_failed": { + "message": "Could not send the notification to the LaMetric device: {error}" + }, + "unknown_sound": { + "message": "Unknown sound: {sound}" + }, + "update_failed": { + "message": "Could not fetch the state of the LaMetric device: {error}" } }, "selector": { diff --git a/tests/components/lametric/test_notify.py b/tests/components/lametric/test_notify.py index eb26ad0467c6..3c5b78476ca8 100644 --- a/tests/components/lametric/test_notify.py +++ b/tests/components/lametric/test_notify.py @@ -30,7 +30,7 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import HomeAssistant -from homeassistant.exceptions import HomeAssistantError +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import entity_registry as er from tests.common import MockConfigEntry, snapshot_platform @@ -122,15 +122,35 @@ async def test_notification_options( assert frame.text == "The secret of getting ahead is getting started" +async def test_notification_unknown_sound( + hass: HomeAssistant, + mock_lametric: MagicMock, +) -> None: + """Test an unknown sound is refused, naming the sound.""" + with pytest.raises(ServiceValidationError, match="Unknown sound: nope"): + await hass.services.async_call( + NOTIFY_DOMAIN, + NOTIFY_SERVICE, + { + ATTR_MESSAGE: "Silence is golden", + ATTR_DATA: {"sound": "nope"}, + }, + blocking=True, + ) + + mock_lametric.notify.assert_not_called() + + async def test_notification_error( hass: HomeAssistant, mock_lametric: MagicMock, ) -> None: """Test the LaMetric notification error.""" - mock_lametric.notify.side_effect = LaMetricError + mock_lametric.notify.side_effect = LaMetricError("Fail to validate") with pytest.raises( - HomeAssistantError, match="Could not send LaMetric notification" + HomeAssistantError, + match="Could not send the notification to the LaMetric device: Fail to validate", ): await hass.services.async_call( NOTIFY_DOMAIN, diff --git a/tests/components/lametric/test_services.py b/tests/components/lametric/test_services.py index b9b5c4c8b3ad..edccd44631d1 100644 --- a/tests/components/lametric/test_services.py +++ b/tests/components/lametric/test_services.py @@ -103,9 +103,10 @@ async def test_service_chart( assert type(frame) is Chart assert frame.data == [1, 2, 3, 4, 5, 4, 3, 2, 1] - mock_lametric.notify.side_effect = LaMetricError + mock_lametric.notify.side_effect = LaMetricError("Fail to validate") with pytest.raises( - HomeAssistantError, match="Could not send LaMetric notification" + HomeAssistantError, + match="Could not send the notification to the LaMetric device: Fail to validate", ): await hass.services.async_call( DOMAIN, @@ -193,9 +194,10 @@ async def test_service_message( assert frame.icon == "6916" assert frame.text == "Meow!" - mock_lametric.notify.side_effect = LaMetricError + mock_lametric.notify.side_effect = LaMetricError("Fail to validate") with pytest.raises( - HomeAssistantError, match="Could not send LaMetric notification" + HomeAssistantError, + match="Could not send the notification to the LaMetric device: Fail to validate", ): await hass.services.async_call( DOMAIN,