mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Add template parsing to notify
This commit is contained in:
parent
af09a305cf
commit
d1383ac94d
@ -13,6 +13,7 @@ import os
|
|||||||
import homeassistant.bootstrap as bootstrap
|
import homeassistant.bootstrap as bootstrap
|
||||||
from homeassistant.config import load_yaml_config_file
|
from homeassistant.config import load_yaml_config_file
|
||||||
from homeassistant.helpers import config_per_platform
|
from homeassistant.helpers import config_per_platform
|
||||||
|
from homeassistant.util import template
|
||||||
|
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_NAME
|
||||||
|
|
||||||
@ -33,9 +34,16 @@ SERVICE_NOTIFY = "notify"
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def send_message(hass, message):
|
def send_message(hass, message, title=None):
|
||||||
""" Send a notification message. """
|
""" 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):
|
def setup(hass, config):
|
||||||
@ -70,8 +78,10 @@ def setup(hass, config):
|
|||||||
if message is None:
|
if message is None:
|
||||||
return
|
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)
|
target = call.data.get(ATTR_TARGET)
|
||||||
|
message = template.render(hass, message)
|
||||||
|
|
||||||
notify_service.send_message(message, title=title, target=target)
|
notify_service.send_message(message, title=title, target=target)
|
||||||
|
|
||||||
|
0
tests/components/notify/__init__.py
Normal file
0
tests/components/notify/__init__.py
Normal file
41
tests/components/notify/test_demo.py
Normal file
41
tests/components/notify/test_demo.py
Normal 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')
|
Loading…
x
Reference in New Issue
Block a user