mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
parent
d466bae244
commit
de150ecbc9
@ -91,19 +91,22 @@ def setup(hass, config):
|
|||||||
|
|
||||||
def notify_message(notify_service, call):
|
def notify_message(notify_service, call):
|
||||||
"""Handle sending notification message service calls."""
|
"""Handle sending notification message service calls."""
|
||||||
|
kwargs = {}
|
||||||
message = call.data[ATTR_MESSAGE]
|
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:
|
if targets.get(call.service) is not None:
|
||||||
target = targets[call.service]
|
kwargs[ATTR_TARGET] = targets[call.service]
|
||||||
else:
|
else:
|
||||||
target = call.data.get(ATTR_TARGET)
|
kwargs[ATTR_TARGET] = call.data.get(ATTR_TARGET)
|
||||||
message = template.render(hass, message)
|
|
||||||
data = call.data.get(ATTR_DATA)
|
|
||||||
|
|
||||||
notify_service.send_message(message, title=title, target=target,
|
kwargs[ATTR_MESSAGE] = template.render(hass, message)
|
||||||
data=data)
|
kwargs[ATTR_DATA] = call.data.get(ATTR_DATA)
|
||||||
|
|
||||||
|
notify_service.send_message(**kwargs)
|
||||||
|
|
||||||
service_call_handler = partial(notify_message, notify_service)
|
service_call_handler = partial(notify_message, notify_service)
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ class TestNotifyDemo(unittest.TestCase):
|
|||||||
'data': {'hello': 'world'}
|
'data': {'hello': 'world'}
|
||||||
} == data
|
} == 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."""
|
"""Test if we can call a notify from a script."""
|
||||||
yaml_conf = """
|
yaml_conf = """
|
||||||
service: notify.notify
|
service: notify.notify
|
||||||
@ -92,7 +92,38 @@ data_template:
|
|||||||
assert {
|
assert {
|
||||||
'message': 'Test 123 4',
|
'message': 'Test 123 4',
|
||||||
'target': None,
|
'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': {
|
'data': {
|
||||||
'push': {
|
'push': {
|
||||||
'sound':
|
'sound':
|
||||||
|
Loading…
x
Reference in New Issue
Block a user