Allow notify services to update existing targets (#45283)
This commit is contained in:
parent
8d3564e275
commit
4928476abe
2 changed files with 87 additions and 1 deletions
|
@ -173,7 +173,10 @@ class BaseNotificationService:
|
||||||
target_name = slugify(f"{self._target_service_name_prefix}_{name}")
|
target_name = slugify(f"{self._target_service_name_prefix}_{name}")
|
||||||
if target_name in stale_targets:
|
if target_name in stale_targets:
|
||||||
stale_targets.remove(target_name)
|
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
|
continue
|
||||||
self.registered_targets[target_name] = target
|
self.registered_targets[target_name] = target
|
||||||
self.hass.services.async_register(
|
self.hass.services.async_register(
|
||||||
|
|
83
tests/components/notify/test_init.py
Normal file
83
tests/components/notify/test_init.py
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue