Handle Alert exception on notification failure (#93632)
parent
9282cb21ab
commit
41eca41638
|
@ -26,6 +26,7 @@ from homeassistant.const import (
|
||||||
STATE_ON,
|
STATE_ON,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HassJob, HomeAssistant
|
from homeassistant.core import HassJob, HomeAssistant
|
||||||
|
from homeassistant.exceptions import ServiceNotFound
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
|
@ -293,9 +294,15 @@ class Alert(Entity):
|
||||||
LOGGER.debug(msg_payload)
|
LOGGER.debug(msg_payload)
|
||||||
|
|
||||||
for target in self._notifiers:
|
for target in self._notifiers:
|
||||||
await self.hass.services.async_call(
|
try:
|
||||||
DOMAIN_NOTIFY, target, msg_payload, context=self._context
|
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 def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Async Unacknowledge alert."""
|
"""Async Unacknowledge alert."""
|
||||||
|
|
|
@ -36,6 +36,7 @@ from tests.common import async_mock_service
|
||||||
NAME = "alert_test"
|
NAME = "alert_test"
|
||||||
DONE_MESSAGE = "alert_gone"
|
DONE_MESSAGE = "alert_gone"
|
||||||
NOTIFIER = "test"
|
NOTIFIER = "test"
|
||||||
|
BAD_NOTIFIER = "bad_notifier"
|
||||||
TEMPLATE = "{{ states.sensor.test.entity_id }}"
|
TEMPLATE = "{{ states.sensor.test.entity_id }}"
|
||||||
TEST_ENTITY = "sensor.test"
|
TEST_ENTITY = "sensor.test"
|
||||||
TITLE = "{{ states.sensor.test.entity_id }}"
|
TITLE = "{{ states.sensor.test.entity_id }}"
|
||||||
|
@ -199,6 +200,26 @@ async def test_notification(
|
||||||
assert len(mock_notifier) == 2
|
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(
|
async def test_no_notifiers(
|
||||||
hass: HomeAssistant, mock_notifier: list[ServiceCall]
|
hass: HomeAssistant, mock_notifier: list[ServiceCall]
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
Loading…
Reference in New Issue