2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the notify demo platform."""
|
2015-12-10 07:46:50 +00:00
|
|
|
import unittest
|
2017-01-15 02:53:14 +00:00
|
|
|
from unittest.mock import patch
|
2015-12-10 07:46:50 +00:00
|
|
|
|
2018-11-30 20:28:35 +00:00
|
|
|
import pytest
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-03-28 03:36:13 +00:00
|
|
|
import homeassistant.components.demo.notify as demo
|
2019-12-08 16:59:27 +00:00
|
|
|
import homeassistant.components.notify as notify
|
2017-01-15 02:53:14 +00:00
|
|
|
from homeassistant.core import callback
|
2020-02-24 22:56:00 +00:00
|
|
|
from homeassistant.helpers import discovery
|
2019-12-08 16:59:27 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2018-09-26 16:02:05 +00:00
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
from tests.common import assert_setup_component, get_test_home_assistant
|
2018-09-26 16:02:05 +00:00
|
|
|
from tests.components.notify import common
|
2015-12-10 07:46:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG = {notify.DOMAIN: {"platform": "demo"}}
|
2016-02-14 23:08:23 +00:00
|
|
|
|
2015-12-10 07:46:50 +00:00
|
|
|
|
|
|
|
class TestNotifyDemo(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the demo notify."""
|
2015-12-10 07:46:50 +00:00
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-02-14 23:08:23 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2015-12-10 07:46:50 +00:00
|
|
|
self.events = []
|
2016-08-17 05:05:41 +00:00
|
|
|
self.calls = []
|
2015-12-10 07:46:50 +00:00
|
|
|
|
2016-11-04 04:58:18 +00:00
|
|
|
@callback
|
2015-12-10 07:46:50 +00:00
|
|
|
def record_event(event):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Record event to send notification."""
|
2015-12-10 07:46:50 +00:00
|
|
|
self.events.append(event)
|
|
|
|
|
|
|
|
self.hass.bus.listen(demo.EVENT_NOTIFY, record_event)
|
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2018-05-13 09:06:15 +00:00
|
|
|
"""Stop down everything that was started."""
|
2015-12-10 07:46:50 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
def _setup_notify(self):
|
2019-04-09 20:59:15 +00:00
|
|
|
with assert_setup_component(1, notify.DOMAIN) as config:
|
2017-01-15 02:53:14 +00:00
|
|
|
assert setup_component(self.hass, notify.DOMAIN, CONFIG)
|
|
|
|
assert config[notify.DOMAIN]
|
2019-04-09 06:16:55 +00:00
|
|
|
self.hass.block_till_done()
|
2017-01-15 02:53:14 +00:00
|
|
|
|
|
|
|
def test_setup(self):
|
|
|
|
"""Test setup."""
|
|
|
|
self._setup_notify()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("homeassistant.components.demo.notify.get_service", autospec=True)
|
2017-01-15 02:53:14 +00:00
|
|
|
def test_no_notify_service(self, mock_demo_get_service):
|
|
|
|
"""Test missing platform notify service instance."""
|
|
|
|
mock_demo_get_service.return_value = None
|
2019-07-31 19:25:30 +00:00
|
|
|
with self.assertLogs(
|
|
|
|
"homeassistant.components.notify", level="ERROR"
|
|
|
|
) as log_handle:
|
2017-01-15 02:53:14 +00:00
|
|
|
self._setup_notify()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
assert mock_demo_get_service.called
|
2019-07-31 19:25:30 +00:00
|
|
|
assert log_handle.output == [
|
|
|
|
"ERROR:homeassistant.components.notify:"
|
|
|
|
"Failed to initialize notification service demo"
|
|
|
|
]
|
2017-01-15 02:53:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("homeassistant.components.demo.notify.get_service", autospec=True)
|
2017-01-15 02:53:14 +00:00
|
|
|
def test_discover_notify(self, mock_demo_get_service):
|
|
|
|
"""Test discovery of notify demo platform."""
|
|
|
|
assert notify.DOMAIN not in self.hass.config.components
|
|
|
|
discovery.load_platform(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, "notify", "demo", {"test_key": "test_val"}, {"notify": {}}
|
|
|
|
)
|
2017-01-15 02:53:14 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
assert notify.DOMAIN in self.hass.config.components
|
|
|
|
assert mock_demo_get_service.called
|
2019-04-14 23:59:06 +00:00
|
|
|
assert mock_demo_get_service.mock_calls[0][1] == (
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass,
|
|
|
|
{},
|
|
|
|
{"test_key": "test_val"},
|
|
|
|
)
|
2017-01-15 02:53:14 +00:00
|
|
|
|
2016-11-04 04:58:18 +00:00
|
|
|
@callback
|
2016-08-17 05:05:41 +00:00
|
|
|
def record_calls(self, *args):
|
2018-08-24 08:28:43 +00:00
|
|
|
"""Record calls."""
|
2016-08-17 05:05:41 +00:00
|
|
|
self.calls.append(args)
|
|
|
|
|
2016-03-18 12:33:52 +00:00
|
|
|
def test_sending_none_message(self):
|
|
|
|
"""Test send with None as message."""
|
2017-01-15 02:53:14 +00:00
|
|
|
self._setup_notify()
|
2018-11-30 20:28:35 +00:00
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
common.send_message(self.hass, None)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert len(self.events) == 0
|
2016-03-18 12:33:52 +00:00
|
|
|
|
2015-12-10 07:46:50 +00:00
|
|
|
def test_sending_templated_message(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Send a templated message."""
|
2017-01-15 02:53:14 +00:00
|
|
|
self._setup_notify()
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.states.set("sensor.temperature", 10)
|
|
|
|
common.send_message(
|
|
|
|
self.hass,
|
|
|
|
"{{ states.sensor.temperature.state }}",
|
|
|
|
"{{ states.sensor.temperature.name }}",
|
|
|
|
)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-12-10 07:46:50 +00:00
|
|
|
last_event = self.events[-1]
|
2019-07-31 19:25:30 +00:00
|
|
|
assert last_event.data[notify.ATTR_TITLE] == "temperature"
|
|
|
|
assert last_event.data[notify.ATTR_MESSAGE] == "10"
|
2016-08-10 03:23:57 +00:00
|
|
|
|
|
|
|
def test_method_forwards_correct_data(self):
|
|
|
|
"""Test that all data from the service gets forwarded to service."""
|
2017-01-15 02:53:14 +00:00
|
|
|
self._setup_notify()
|
2019-07-31 19:25:30 +00:00
|
|
|
common.send_message(self.hass, "my message", "my title", {"hello": "world"})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert len(self.events) == 1
|
2016-08-10 03:23:57 +00:00
|
|
|
data = self.events[0].data
|
|
|
|
assert {
|
2019-07-31 19:25:30 +00:00
|
|
|
"message": "my message",
|
|
|
|
"title": "my title",
|
|
|
|
"data": {"hello": "world"},
|
2016-08-10 03:23:57 +00:00
|
|
|
} == data
|
2016-08-10 04:03:06 +00:00
|
|
|
|
2016-09-10 14:36:55 +00:00
|
|
|
def test_calling_notify_from_script_loaded_from_yaml_without_title(self):
|
2016-08-10 04:03:06 +00:00
|
|
|
"""Test if we can call a notify from a script."""
|
2017-01-15 02:53:14 +00:00
|
|
|
self._setup_notify()
|
2020-02-24 22:56:00 +00:00
|
|
|
step = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"service": "notify.notify",
|
|
|
|
"data": {
|
|
|
|
"data": {
|
|
|
|
"push": {"sound": "US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav"}
|
2016-10-18 03:16:36 +00:00
|
|
|
}
|
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"data_template": {"message": "Test 123 {{ 2 + 2 }}\n"},
|
2016-10-18 03:16:36 +00:00
|
|
|
}
|
2020-02-24 22:56:00 +00:00
|
|
|
setup_component(self.hass, "script", {"script": {"test": {"sequence": step}}})
|
|
|
|
self.hass.services.call("script", "test")
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert len(self.events) == 1
|
2016-08-10 04:03:06 +00:00
|
|
|
assert {
|
2019-07-31 19:25:30 +00:00
|
|
|
"message": "Test 123 4",
|
|
|
|
"data": {
|
|
|
|
"push": {"sound": "US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav"}
|
|
|
|
},
|
2016-09-10 14:36:55 +00:00
|
|
|
} == self.events[0].data
|
|
|
|
|
|
|
|
def test_calling_notify_from_script_loaded_from_yaml_with_title(self):
|
|
|
|
"""Test if we can call a notify from a script."""
|
2017-01-15 02:53:14 +00:00
|
|
|
self._setup_notify()
|
2020-02-24 22:56:00 +00:00
|
|
|
step = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"service": "notify.notify",
|
|
|
|
"data": {
|
|
|
|
"data": {
|
|
|
|
"push": {"sound": "US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav"}
|
2016-10-18 03:16:36 +00:00
|
|
|
}
|
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"data_template": {"message": "Test 123 {{ 2 + 2 }}\n", "title": "Test"},
|
2016-10-18 03:16:36 +00:00
|
|
|
}
|
2020-02-24 22:56:00 +00:00
|
|
|
setup_component(self.hass, "script", {"script": {"test": {"sequence": step}}})
|
|
|
|
self.hass.services.call("script", "test")
|
2016-10-31 15:47:29 +00:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert len(self.events) == 1
|
2016-09-10 14:36:55 +00:00
|
|
|
assert {
|
2019-07-31 19:25:30 +00:00
|
|
|
"message": "Test 123 4",
|
|
|
|
"title": "Test",
|
|
|
|
"data": {
|
|
|
|
"push": {"sound": "US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav"}
|
|
|
|
},
|
2016-08-10 04:03:06 +00:00
|
|
|
} == self.events[0].data
|
2016-08-17 05:05:41 +00:00
|
|
|
|
|
|
|
def test_targets_are_services(self):
|
|
|
|
"""Test that all targets are exposed as individual services."""
|
2017-01-15 02:53:14 +00:00
|
|
|
self._setup_notify()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.hass.services.has_service("notify", "demo") is not None
|
2016-09-25 16:41:11 +00:00
|
|
|
service = "demo_test_target_name"
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.hass.services.has_service("notify", service) is not None
|
2016-08-17 05:05:41 +00:00
|
|
|
|
|
|
|
def test_messages_to_targets_route(self):
|
|
|
|
"""Test message routing to specific target services."""
|
2017-01-15 02:53:14 +00:00
|
|
|
self._setup_notify()
|
2016-08-17 05:05:41 +00:00
|
|
|
self.hass.bus.listen_once("notify", self.record_calls)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.services.call(
|
|
|
|
"notify",
|
|
|
|
"demo_test_target_name",
|
|
|
|
{"message": "my message", "title": "my title", "data": {"hello": "world"}},
|
|
|
|
)
|
2016-08-17 05:05:41 +00:00
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-17 05:05:41 +00:00
|
|
|
|
|
|
|
data = self.calls[0][0].data
|
|
|
|
|
|
|
|
assert {
|
2019-07-31 19:25:30 +00:00
|
|
|
"message": "my message",
|
|
|
|
"target": ["test target id"],
|
|
|
|
"title": "my title",
|
|
|
|
"data": {"hello": "world"},
|
2016-08-17 05:05:41 +00:00
|
|
|
} == data
|