core/tests/components/notify/test_init.py

100 lines
3.4 KiB
Python

"""The tests for notify services that change targets."""
from homeassistant.components import notify
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
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
async def test_warn_template(hass, caplog):
"""Test warning when template used."""
assert await async_setup_component(hass, "notify", {})
await hass.services.async_call(
"notify",
"persistent_notification",
{"message": "{{ 1 + 1 }}", "title": "Test notif {{ 1 + 1 }}"},
blocking=True,
)
# We should only log it once
assert caplog.text.count("Passing templates to notify service is deprecated") == 1
assert hass.states.get("persistent_notification.notification") is not None