From 17f1e6349a133112019ad8d7c651409862341558 Mon Sep 17 00:00:00 2001 From: Martin <32802427+mstu01@users.noreply.github.com> Date: Mon, 7 Sep 2026 13:15:24 +0200 Subject: [PATCH] Fix swallowed exceptions in action handlers for NETGEAR LTE (#181539) --- .../components/netgear_lte/notify.py | 12 ++++--- .../components/netgear_lte/strings.json | 3 ++ tests/components/netgear_lte/test_notify.py | 33 ++++++++++++++++++- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/netgear_lte/notify.py b/homeassistant/components/netgear_lte/notify.py index 56b173288d90..06e7e36c4a48 100644 --- a/homeassistant/components/netgear_lte/notify.py +++ b/homeassistant/components/netgear_lte/notify.py @@ -8,9 +8,10 @@ from eternalegypt.eternalegypt import Modem from homeassistant.components.notify import ATTR_TARGET, BaseNotificationService from homeassistant.const import CONF_RECIPIENT from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from .const import CONF_NOTIFY, LOGGER +from .const import CONF_NOTIFY, DOMAIN, LOGGER async def async_get_service( @@ -57,6 +58,9 @@ class NetgearNotifyService(BaseNotificationService): for target in targets: try: await self.modem.sms(target, message) - # pylint: disable-next=home-assistant-action-swallowed-exception - except eternalegypt.Error: - LOGGER.error("Unable to send to %s", target) + except eternalegypt.Error as err: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="send_message_failed", + translation_placeholders={"target": target}, + ) from err diff --git a/homeassistant/components/netgear_lte/strings.json b/homeassistant/components/netgear_lte/strings.json index 37042e268a1e..4edb99929e14 100644 --- a/homeassistant/components/netgear_lte/strings.json +++ b/homeassistant/components/netgear_lte/strings.json @@ -74,6 +74,9 @@ "exceptions": { "config_entry_not_found": { "message": "Failed to perform action \"{service}\". Config entry for target not found" + }, + "send_message_failed": { + "message": "Failed to send SMS to {target}." } }, "services": { diff --git a/tests/components/netgear_lte/test_notify.py b/tests/components/netgear_lte/test_notify.py index 9a55e7a7ad6f..f8f3eb2a450b 100644 --- a/tests/components/netgear_lte/test_notify.py +++ b/tests/components/netgear_lte/test_notify.py @@ -2,15 +2,21 @@ from unittest.mock import patch +import eternalegypt +import pytest + +from homeassistant.components.netgear_lte.const import DOMAIN from homeassistant.components.notify import ( ATTR_MESSAGE, ATTR_TARGET, DOMAIN as NOTIFY_DOMAIN, ) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError ICON_PATH = "/some/path" MESSAGE = "one, two, testing, testing" +TARGET = "5555555556" async def test_notify(hass: HomeAssistant, setup_integration: None) -> None: @@ -23,8 +29,33 @@ async def test_notify(hass: HomeAssistant, setup_integration: None) -> None: "netgear_lm1200", { ATTR_MESSAGE: MESSAGE, - ATTR_TARGET: "5555555556", + ATTR_TARGET: TARGET, }, blocking=True, ) assert len(mock.mock_calls) == 1 + + +@pytest.mark.usefixtures("setup_integration") +async def test_notify_error(hass: HomeAssistant) -> None: + """Test that a failed send raises an error with a translation key.""" + with ( + patch( + "homeassistant.components.netgear_lte.eternalegypt.Modem.sms", + side_effect=eternalegypt.Error, + ), + pytest.raises(HomeAssistantError) as exc_info, + ): + await hass.services.async_call( + NOTIFY_DOMAIN, + "netgear_lm1200", + { + ATTR_MESSAGE: MESSAGE, + ATTR_TARGET: TARGET, + }, + blocking=True, + ) + + assert exc_info.value.translation_domain == DOMAIN + assert exc_info.value.translation_key == "send_message_failed" + assert exc_info.value.translation_placeholders == {"target": TARGET}