Create additional mqtt helper function for using template payload.

This commit is contained in:
Flyte 2016-02-10 22:38:33 +00:00
parent d52e2019c0
commit 4e0c7f8a3d
2 changed files with 29 additions and 18 deletions

View file

@ -80,8 +80,7 @@ class TestMQTT(unittest.TestCase):
"""
If 'payload_template' is provided and 'payload' is not, then render it.
"""
mqtt.publish(self.hass, "test/topic",
**{mqtt.ATTR_PAYLOAD_TEMPLATE: "{{ 1+1 }}"})
mqtt.publish_template(self.hass, "test/topic", "{{ 1+1 }}")
self.hass.pool.block_till_done()
self.assertTrue(mqtt.MQTT_CLIENT.publish.called)
self.assertEqual(mqtt.MQTT_CLIENT.publish.call_args[0][1], "2")
@ -92,9 +91,13 @@ class TestMQTT(unittest.TestCase):
"""
payload = "not a template"
payload_template = "a template"
mqtt.publish(self.hass, "test/topic", payload,
**{mqtt.ATTR_PAYLOAD_TEMPLATE: payload_template})
self.hass.pool.block_till_done()
# Call the service directly because the helper functions don't allow
# you to provide payload AND payload_template.
self.hass.services.call(mqtt.DOMAIN, mqtt.SERVICE_PUBLISH, {
mqtt.ATTR_TOPIC: "test/topic",
mqtt.ATTR_PAYLOAD: payload,
mqtt.ATTR_PAYLOAD_TEMPLATE: payload_template
}, blocking=True)
self.assertTrue(mqtt.MQTT_CLIENT.publish.called)
self.assertEqual(mqtt.MQTT_CLIENT.publish.call_args[0][1], payload)
@ -102,8 +105,11 @@ class TestMQTT(unittest.TestCase):
"""
If neither 'payload' or 'payload_template' is provided then fail.
"""
mqtt.publish(self.hass, "test/topic")
self.hass.pool.block_till_done()
# Call the service directly because the helper functions require you to
# provide a payload.
self.hass.services.call(mqtt.DOMAIN, mqtt.SERVICE_PUBLISH, {
mqtt.ATTR_TOPIC: "test/topic"
}, blocking=True)
self.assertFalse(mqtt.MQTT_CLIENT.publish.called)
def test_subscribe_topic(self):