diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index 9182f1dbf3a3ed30c06ac3f7e4c43b0d8e295b6e..245b9c6fde3c0e76aebbc2d9fcf30bea67d62ccb 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -13,6 +13,7 @@ import os import homeassistant.bootstrap as bootstrap from homeassistant.config import load_yaml_config_file from homeassistant.helpers import config_per_platform +from homeassistant.util import template from homeassistant.const import CONF_NAME @@ -33,9 +34,16 @@ SERVICE_NOTIFY = "notify" _LOGGER = logging.getLogger(__name__) -def send_message(hass, message): +def send_message(hass, message, title=None): """ Send a notification message. """ - hass.services.call(DOMAIN, SERVICE_NOTIFY, {ATTR_MESSAGE: message}) + data = { + ATTR_MESSAGE: message + } + + if title is not None: + data[ATTR_TITLE] = title + + hass.services.call(DOMAIN, SERVICE_NOTIFY, data) def setup(hass, config): @@ -70,8 +78,10 @@ def setup(hass, config): if message is None: return - title = call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT) + title = template.render( + hass, call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)) target = call.data.get(ATTR_TARGET) + message = template.render(hass, message) notify_service.send_message(message, title=title, target=target) diff --git a/tests/components/notify/__init__.py b/tests/components/notify/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tests/components/notify/test_demo.py b/tests/components/notify/test_demo.py new file mode 100644 index 0000000000000000000000000000000000000000..182a2a3861027bdbed12b96f7ed85199c9afc4d3 --- /dev/null +++ b/tests/components/notify/test_demo.py @@ -0,0 +1,41 @@ +""" +tests.components.notify.test_demo +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Tests notify demo component +""" +import unittest + +import homeassistant.core as ha +import homeassistant.components.notify as notify +from homeassistant.components.notify import demo + + +class TestNotifyDemo(unittest.TestCase): + """ Test the demo notify. """ + + def setUp(self): # pylint: disable=invalid-name + self.hass = ha.HomeAssistant() + self.assertTrue(notify.setup(self.hass, { + 'notify': { + 'platform': 'demo' + } + })) + self.events = [] + + def record_event(event): + self.events.append(event) + + self.hass.bus.listen(demo.EVENT_NOTIFY, record_event) + + def tearDown(self): # pylint: disable=invalid-name + """ Stop down stuff we started. """ + self.hass.stop() + + def test_sending_templated_message(self): + self.hass.states.set('sensor.temperature', 10) + notify.send_message(self.hass, '{{ states.sensor.temperature.state }}', + '{{ states.sensor.temperature.name }}') + self.hass.pool.block_till_done() + last_event = self.events[-1] + self.assertEqual(last_event.data[notify.ATTR_TITLE], 'temperature') + self.assertEqual(last_event.data[notify.ATTR_MESSAGE], '10')