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
|
|
|
|
|
2016-09-17 17:29:58 +00:00
|
|
|
from homeassistant.bootstrap import setup_component
|
2015-12-10 07:46:50 +00:00
|
|
|
import homeassistant.components.notify as notify
|
|
|
|
from homeassistant.components.notify import demo
|
2016-08-10 04:03:06 +00:00
|
|
|
from homeassistant.helpers import script
|
2015-12-10 07:46:50 +00:00
|
|
|
|
2016-02-14 23:08:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
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
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-02-14 23:08:23 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2016-09-17 17:29:58 +00:00
|
|
|
self.assertTrue(setup_component(self.hass, notify.DOMAIN, {
|
2015-12-10 07:46:50 +00:00
|
|
|
'notify': {
|
|
|
|
'platform': 'demo'
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
self.events = []
|
2016-08-17 05:05:41 +00:00
|
|
|
self.calls = []
|
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
|
2016-03-09 09:25:50 +00:00
|
|
|
""""Stop down everything that was started."""
|
2015-12-10 07:46:50 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2016-08-17 05:05:41 +00:00
|
|
|
def record_calls(self, *args):
|
|
|
|
"""Helper for recording calls."""
|
|
|
|
self.calls.append(args)
|
|
|
|
|
2016-03-18 12:33:52 +00:00
|
|
|
def test_sending_none_message(self):
|
|
|
|
"""Test send with None as message."""
|
|
|
|
notify.send_message(self.hass, None)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-03-18 12:33:52 +00:00
|
|
|
self.assertTrue(len(self.events) == 0)
|
|
|
|
|
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."""
|
2015-12-10 07:46:50 +00:00
|
|
|
self.hass.states.set('sensor.temperature', 10)
|
|
|
|
notify.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]
|
|
|
|
self.assertEqual(last_event.data[notify.ATTR_TITLE], 'temperature')
|
|
|
|
self.assertEqual(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."""
|
|
|
|
notify.send_message(self.hass, 'my message', 'my title',
|
|
|
|
{'hello': 'world'})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-10 03:23:57 +00:00
|
|
|
self.assertTrue(len(self.events) == 1)
|
|
|
|
data = self.events[0].data
|
|
|
|
assert {
|
|
|
|
'message': 'my message',
|
|
|
|
'title': 'my title',
|
|
|
|
'data': {'hello': 'world'}
|
|
|
|
} == 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."""
|
2016-10-18 03:16:36 +00:00
|
|
|
conf = {
|
|
|
|
'service': 'notify.notify',
|
|
|
|
'data': {
|
|
|
|
'data': {
|
|
|
|
'push': {
|
|
|
|
'sound':
|
|
|
|
'US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'data_template': {'message': 'Test 123 {{ 2 + 2 }}\n'},
|
|
|
|
}
|
2016-08-10 04:03:06 +00:00
|
|
|
|
|
|
|
script.call_from_config(self.hass, conf)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-10 04:03:06 +00:00
|
|
|
self.assertTrue(len(self.events) == 1)
|
|
|
|
assert {
|
|
|
|
'message': 'Test 123 4',
|
2016-09-10 14:36:55 +00:00
|
|
|
'data': {
|
|
|
|
'push': {
|
|
|
|
'sound':
|
|
|
|
'US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav'}}
|
|
|
|
} == 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."""
|
2016-10-18 03:16:36 +00:00
|
|
|
conf = {
|
|
|
|
'service': 'notify.notify',
|
|
|
|
'data': {
|
|
|
|
'data': {
|
|
|
|
'push': {
|
|
|
|
'sound':
|
|
|
|
'US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'data_template': {
|
|
|
|
'message': 'Test 123 {{ 2 + 2 }}\n',
|
|
|
|
'title': 'Test'
|
|
|
|
}
|
|
|
|
}
|
2016-09-10 14:36:55 +00:00
|
|
|
|
|
|
|
script.call_from_config(self.hass, conf)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertTrue(len(self.events) == 1)
|
|
|
|
assert {
|
|
|
|
'message': 'Test 123 4',
|
|
|
|
'title': 'Test',
|
2016-08-10 04:03:06 +00:00
|
|
|
'data': {
|
|
|
|
'push': {
|
|
|
|
'sound':
|
|
|
|
'US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav'}}
|
|
|
|
} == 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."""
|
|
|
|
self.assertIsNotNone(self.hass.services.has_service("notify", "demo"))
|
2016-09-25 16:41:11 +00:00
|
|
|
service = "demo_test_target_name"
|
2016-08-17 05:05:41 +00:00
|
|
|
self.assertIsNotNone(self.hass.services.has_service("notify", service))
|
|
|
|
|
|
|
|
def test_messages_to_targets_route(self):
|
|
|
|
"""Test message routing to specific target services."""
|
|
|
|
self.hass.bus.listen_once("notify", self.record_calls)
|
|
|
|
|
2016-09-25 16:41:11 +00:00
|
|
|
self.hass.services.call("notify", "demo_test_target_name",
|
2016-08-17 05:05:41 +00:00
|
|
|
{'message': 'my message',
|
|
|
|
'title': 'my title',
|
|
|
|
'data': {'hello': 'world'}})
|
|
|
|
|
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 {
|
|
|
|
'message': 'my message',
|
2016-10-11 05:31:15 +00:00
|
|
|
'target': ['test target id'],
|
2016-08-17 05:05:41 +00:00
|
|
|
'title': 'my title',
|
|
|
|
'data': {'hello': 'world'}
|
|
|
|
} == data
|