Translate the exceptions of LaMetric (#184158)

This commit is contained in:
Franck Nijhof
2026-10-03 21:50:21 +02:00
committed by GitHub
parent 9d2ad09244
commit f7ff4ca3c6
7 changed files with 63 additions and 14 deletions
@@ -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
+11 -3
View File
@@ -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
@@ -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:
+10 -2
View File
@@ -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
@@ -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": {
+23 -3
View File
@@ -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,
+6 -4
View File
@@ -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,