2016-08-17 05:14:04 +00:00
|
|
|
"""The tests for the notify.group platform."""
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2016-08-17 05:14:04 +00:00
|
|
|
|
2020-09-02 22:12:07 +00:00
|
|
|
from homeassistant import config as hass_config
|
2019-03-28 03:36:13 +00:00
|
|
|
import homeassistant.components.demo.notify as demo
|
2020-09-02 22:12:07 +00:00
|
|
|
from homeassistant.components.group import SERVICE_RELOAD
|
2019-12-09 08:39:26 +00:00
|
|
|
import homeassistant.components.group.notify as group
|
|
|
|
import homeassistant.components.notify as notify
|
2020-10-23 10:05:07 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2016-08-17 05:14:04 +00:00
|
|
|
|
2021-11-02 03:47:05 +00:00
|
|
|
from tests.common import get_fixture_path
|
|
|
|
|
2016-08-17 05:14:04 +00:00
|
|
|
|
2020-10-23 10:05:07 +00:00
|
|
|
async def test_send_message_with_data(hass):
|
|
|
|
"""Test sending a message with to a notify group."""
|
|
|
|
service1 = demo.DemoNotificationService(hass)
|
|
|
|
service2 = demo.DemoNotificationService(hass)
|
2016-08-17 05:14:04 +00:00
|
|
|
|
2020-10-23 10:05:07 +00:00
|
|
|
service1.send_message = MagicMock(autospec=True)
|
|
|
|
service2.send_message = MagicMock(autospec=True)
|
2017-01-18 06:08:03 +00:00
|
|
|
|
2020-10-23 10:05:07 +00:00
|
|
|
def mock_get_service(hass, config, discovery_info=None):
|
|
|
|
if config["name"] == "demo1":
|
|
|
|
return service1
|
|
|
|
return service2
|
2016-11-04 05:56:55 +00:00
|
|
|
|
2020-10-23 10:05:07 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"group",
|
|
|
|
{},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2016-11-04 05:56:55 +00:00
|
|
|
|
2020-10-23 10:05:07 +00:00
|
|
|
with patch.object(demo, "get_service", mock_get_service):
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
notify.DOMAIN,
|
|
|
|
{
|
|
|
|
"notify": [
|
|
|
|
{"name": "demo1", "platform": "demo"},
|
|
|
|
{"name": "demo2", "platform": "demo"},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
service = await group.async_get_service(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"services": [
|
|
|
|
{"service": "demo1"},
|
2019-07-31 19:25:30 +00:00
|
|
|
{
|
2020-10-23 10:05:07 +00:00
|
|
|
"service": "demo2",
|
|
|
|
"data": {
|
|
|
|
"target": "unnamed device",
|
|
|
|
"data": {"test": "message"},
|
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2020-10-23 10:05:07 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
2016-08-17 05:14:04 +00:00
|
|
|
|
2020-10-23 10:05:07 +00:00
|
|
|
"""Test sending a message with to a notify group."""
|
|
|
|
await service.async_send_message(
|
|
|
|
"Hello", title="Test notification", data={"hello": "world"}
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert service1.send_message.mock_calls[0][1][0] == "Hello"
|
|
|
|
assert service1.send_message.mock_calls[0][2] == {
|
|
|
|
"title": "Test notification",
|
|
|
|
"data": {"hello": "world"},
|
|
|
|
}
|
|
|
|
assert service2.send_message.mock_calls[0][1][0] == "Hello"
|
|
|
|
assert service2.send_message.mock_calls[0][2] == {
|
|
|
|
"target": ["unnamed device"],
|
|
|
|
"title": "Test notification",
|
|
|
|
"data": {"hello": "world", "test": "message"},
|
|
|
|
}
|
2020-09-02 22:12:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_reload_notify(hass):
|
|
|
|
"""Verify we can reload the notify service."""
|
|
|
|
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"group",
|
|
|
|
{},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
notify.DOMAIN,
|
|
|
|
{
|
|
|
|
notify.DOMAIN: [
|
|
|
|
{"name": "demo1", "platform": "demo"},
|
|
|
|
{"name": "demo2", "platform": "demo"},
|
|
|
|
{
|
|
|
|
"name": "group_notify",
|
|
|
|
"platform": "group",
|
|
|
|
"services": [{"service": "demo1"}],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.services.has_service(notify.DOMAIN, "demo1")
|
|
|
|
assert hass.services.has_service(notify.DOMAIN, "demo2")
|
|
|
|
assert hass.services.has_service(notify.DOMAIN, "group_notify")
|
|
|
|
|
2021-11-02 03:47:05 +00:00
|
|
|
yaml_path = get_fixture_path("configuration.yaml", "group")
|
|
|
|
|
2020-09-02 22:12:07 +00:00
|
|
|
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
|
|
|
|
await hass.services.async_call(
|
|
|
|
"group",
|
|
|
|
SERVICE_RELOAD,
|
|
|
|
{},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.services.has_service(notify.DOMAIN, "demo1")
|
|
|
|
assert hass.services.has_service(notify.DOMAIN, "demo2")
|
|
|
|
assert not hass.services.has_service(notify.DOMAIN, "group_notify")
|
|
|
|
assert hass.services.has_service(notify.DOMAIN, "new_group_notify")
|