diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index 426c28bccff..d3439baf4fb 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -173,7 +173,10 @@ class BaseNotificationService: target_name = slugify(f"{self._target_service_name_prefix}_{name}") if target_name in stale_targets: stale_targets.remove(target_name) - if target_name in self.registered_targets: + if ( + target_name in self.registered_targets + and target == self.registered_targets[target_name] + ): continue self.registered_targets[target_name] = target self.hass.services.async_register( diff --git a/tests/components/notify/test_init.py b/tests/components/notify/test_init.py new file mode 100644 index 00000000000..cce26750c1c --- /dev/null +++ b/tests/components/notify/test_init.py @@ -0,0 +1,83 @@ +"""The tests for notify services that change targets.""" +from homeassistant.components import notify +from homeassistant.core import HomeAssistant + + +async def test_same_targets(hass: HomeAssistant): + """Test not changing the targets in a notify service.""" + test = NotificationService(hass) + await test.async_setup(hass, "notify", "test") + await test.async_register_services() + await hass.async_block_till_done() + + assert hasattr(test, "registered_targets") + assert test.registered_targets == {"test_a": 1, "test_b": 2} + + await test.async_register_services() + await hass.async_block_till_done() + assert test.registered_targets == {"test_a": 1, "test_b": 2} + + +async def test_change_targets(hass: HomeAssistant): + """Test changing the targets in a notify service.""" + test = NotificationService(hass) + await test.async_setup(hass, "notify", "test") + await test.async_register_services() + await hass.async_block_till_done() + + assert hasattr(test, "registered_targets") + assert test.registered_targets == {"test_a": 1, "test_b": 2} + + test.target_list = {"a": 0} + await test.async_register_services() + await hass.async_block_till_done() + assert test.target_list == {"a": 0} + assert test.registered_targets == {"test_a": 0} + + +async def test_add_targets(hass: HomeAssistant): + """Test adding the targets in a notify service.""" + test = NotificationService(hass) + await test.async_setup(hass, "notify", "test") + await test.async_register_services() + await hass.async_block_till_done() + + assert hasattr(test, "registered_targets") + assert test.registered_targets == {"test_a": 1, "test_b": 2} + + test.target_list = {"a": 1, "b": 2, "c": 3} + await test.async_register_services() + await hass.async_block_till_done() + assert test.target_list == {"a": 1, "b": 2, "c": 3} + assert test.registered_targets == {"test_a": 1, "test_b": 2, "test_c": 3} + + +async def test_remove_targets(hass: HomeAssistant): + """Test removing targets from the targets in a notify service.""" + test = NotificationService(hass) + await test.async_setup(hass, "notify", "test") + await test.async_register_services() + await hass.async_block_till_done() + + assert hasattr(test, "registered_targets") + assert test.registered_targets == {"test_a": 1, "test_b": 2} + + test.target_list = {"c": 1} + await test.async_register_services() + await hass.async_block_till_done() + assert test.target_list == {"c": 1} + assert test.registered_targets == {"test_c": 1} + + +class NotificationService(notify.BaseNotificationService): + """A test class for notification services.""" + + def __init__(self, hass): + """Initialize the service.""" + self.hass = hass + self.target_list = {"a": 1, "b": 2} + + @property + def targets(self): + """Return a dictionary of devices.""" + return self.target_list