Skip to content

Commit

Permalink
Rebase - Handle Alert exception on notification failure
Browse files Browse the repository at this point in the history
  • Loading branch information
karwosts committed Aug 3, 2023
1 parent 620525b commit 81df0f4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
13 changes: 10 additions & 3 deletions homeassistant/components/alert/__init__.py
Expand Up @@ -26,6 +26,7 @@
STATE_ON,
)
from homeassistant.core import HassJob, HomeAssistant
from homeassistant.exceptions import ServiceNotFound
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
Expand Down Expand Up @@ -293,9 +294,15 @@ async def _send_notification_message(self, message: Any) -> None:
LOGGER.debug(msg_payload)

for target in self._notifiers:
await self.hass.services.async_call(
DOMAIN_NOTIFY, target, msg_payload, context=self._context
)
try:
await self.hass.services.async_call(
DOMAIN_NOTIFY, target, msg_payload, context=self._context
)
except ServiceNotFound:
LOGGER.error(
"Failed to call notify.%s, retrying at next notification interval",
target,
)

async def async_turn_on(self, **kwargs: Any) -> None:
"""Async Unacknowledge alert."""
Expand Down
21 changes: 21 additions & 0 deletions tests/components/alert/test_init.py
Expand Up @@ -36,6 +36,7 @@
NAME = "alert_test"
DONE_MESSAGE = "alert_gone"
NOTIFIER = "test"
BAD_NOTIFIER = "bad_notifier"
TEMPLATE = "{{ states.sensor.test.entity_id }}"
TEST_ENTITY = "sensor.test"
TITLE = "{{ states.sensor.test.entity_id }}"
Expand Down Expand Up @@ -199,6 +200,26 @@ async def test_notification(
assert len(mock_notifier) == 2


async def test_bad_notifier(
hass: HomeAssistant, mock_notifier: list[ServiceCall]
) -> None:
"""Test a broken notifier does not break the alert."""
config = deepcopy(TEST_CONFIG)
config[DOMAIN][NAME][CONF_NOTIFIERS] = [BAD_NOTIFIER, NOTIFIER]
assert await async_setup_component(hass, DOMAIN, config)
assert len(mock_notifier) == 0

hass.states.async_set("sensor.test", STATE_ON)
await hass.async_block_till_done()
assert len(mock_notifier) == 1
assert hass.states.get(ENTITY_ID).state == STATE_ON

hass.states.async_set("sensor.test", STATE_OFF)
await hass.async_block_till_done()
assert len(mock_notifier) == 2
assert hass.states.get(ENTITY_ID).state == STATE_IDLE


async def test_no_notifiers(
hass: HomeAssistant, mock_notifier: list[ServiceCall]
) -> None:
Expand Down

0 comments on commit 81df0f4

Please sign in to comment.