From de150ecbc9272e8de82cf5ed6c7ec53e769cb5a3 Mon Sep 17 00:00:00 2001 From: Lewis Juggins Date: Sat, 10 Sep 2016 15:36:55 +0100 Subject: [PATCH] Hotfix for #3100 (#3302) --- homeassistant/components/notify/__init__.py | 19 ++++++----- tests/components/notify/test_demo.py | 35 +++++++++++++++++++-- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index d7ebfbbcd1f..9fb6ca1f842 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -91,19 +91,22 @@ def setup(hass, config): def notify_message(notify_service, call): """Handle sending notification message service calls.""" + kwargs = {} message = call.data[ATTR_MESSAGE] + title = call.data.get(ATTR_TITLE) + + if title: + kwargs[ATTR_TITLE] = template.render(hass, title) - title = template.render( - hass, call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)) if targets.get(call.service) is not None: - target = targets[call.service] + kwargs[ATTR_TARGET] = targets[call.service] else: - target = call.data.get(ATTR_TARGET) - message = template.render(hass, message) - data = call.data.get(ATTR_DATA) + kwargs[ATTR_TARGET] = call.data.get(ATTR_TARGET) - notify_service.send_message(message, title=title, target=target, - data=data) + kwargs[ATTR_MESSAGE] = template.render(hass, message) + kwargs[ATTR_DATA] = call.data.get(ATTR_DATA) + + notify_service.send_message(**kwargs) service_call_handler = partial(notify_message, notify_service) diff --git a/tests/components/notify/test_demo.py b/tests/components/notify/test_demo.py index f0a05a01c1f..6f0daeaf7b8 100644 --- a/tests/components/notify/test_demo.py +++ b/tests/components/notify/test_demo.py @@ -68,7 +68,7 @@ class TestNotifyDemo(unittest.TestCase): 'data': {'hello': 'world'} } == data - def test_calling_notify_from_script_loaded_from_yaml(self): + def test_calling_notify_from_script_loaded_from_yaml_without_title(self): """Test if we can call a notify from a script.""" yaml_conf = """ service: notify.notify @@ -92,7 +92,38 @@ data_template: assert { 'message': 'Test 123 4', 'target': None, - 'title': 'Home Assistant', + '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.""" + yaml_conf = """ +service: notify.notify +data: + data: + push: + sound: US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav +data_template: + title: Test + message: > + Test 123 {{ 2 + 2 }} +""" + + with tempfile.NamedTemporaryFile() as fp: + fp.write(yaml_conf.encode('utf-8')) + fp.flush() + conf = yaml.load_yaml(fp.name) + + 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', + 'target': None, 'data': { 'push': { 'sound':