1
mirror of https://github.com/home-assistant/core synced 2024-09-09 12:51:22 +02:00

Add template parsing to notify

This commit is contained in:
Paulus Schoutsen 2015-12-09 23:46:50 -08:00
parent af09a305cf
commit d1383ac94d
3 changed files with 54 additions and 3 deletions

View File

@ -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)

View File

View File

@ -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')