From ea6368775b6612cf0c7b3f92f74658fabf64fa97 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 13 Oct 2022 09:04:36 +0200 Subject: [PATCH] Make notifiers of Alert optional (#80209) --- homeassistant/components/alert/__init__.py | 7 +++- tests/components/alert/test_init.py | 38 +++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/alert/__init__.py b/homeassistant/components/alert/__init__.py index 460b418201f..4f8f1dade93 100644 --- a/homeassistant/components/alert/__init__.py +++ b/homeassistant/components/alert/__init__.py @@ -68,7 +68,9 @@ ALERT_SCHEMA = vol.Schema( vol.Optional(CONF_DONE_MESSAGE): cv.template, vol.Optional(CONF_TITLE): cv.template, vol.Optional(CONF_DATA): dict, - vol.Required(CONF_NOTIFIERS): vol.All(cv.ensure_list, [cv.string]), + vol.Optional(CONF_NOTIFIERS, default=list): vol.All( + cv.ensure_list, [cv.string] + ), } ) @@ -269,6 +271,9 @@ class Alert(Entity): async def _send_notification_message(self, message: Any) -> None: + if not self._notifiers: + return + msg_payload = {ATTR_MESSAGE: message} if self._title_template is not None: diff --git a/tests/components/alert/test_init.py b/tests/components/alert/test_init.py index e4f90a1a989..3d2067a9ed9 100644 --- a/tests/components/alert/test_init.py +++ b/tests/components/alert/test_init.py @@ -28,7 +28,7 @@ from homeassistant.const import ( STATE_OFF, STATE_ON, ) -from homeassistant.core import callback +from homeassistant.core import HomeAssistant, callback from homeassistant.setup import async_setup_component NAME = "alert_test" @@ -223,6 +223,42 @@ async def test_notification(hass): assert len(events) == 2 +async def test_no_notifiers(hass: HomeAssistant) -> None: + """Test we send no notifications when there are not no.""" + events = [] + + @callback + def record_event(event): + """Add recorded event to set.""" + events.append(event) + + hass.services.async_register(notify.DOMAIN, NOTIFIER, record_event) + + assert await async_setup_component( + hass, + DOMAIN, + { + DOMAIN: { + NAME: { + CONF_NAME: NAME, + CONF_ENTITY_ID: TEST_ENTITY, + CONF_STATE: STATE_ON, + CONF_REPEAT: 30, + } + } + }, + ) + assert len(events) == 0 + + hass.states.async_set("sensor.test", STATE_ON) + await hass.async_block_till_done() + assert len(events) == 0 + + hass.states.async_set("sensor.test", STATE_OFF) + await hass.async_block_till_done() + assert len(events) == 0 + + async def test_sending_non_templated_notification(hass, mock_notifier): """Test notifications.""" assert await async_setup_component(hass, DOMAIN, TEST_CONFIG)