Create additional mqtt helper function for using template payload.
This commit is contained in:
parent
d52e2019c0
commit
4e0c7f8a3d
2 changed files with 29 additions and 18 deletions
|
@ -57,23 +57,28 @@ ATTR_RETAIN = 'retain'
|
||||||
MAX_RECONNECT_WAIT = 300 # seconds
|
MAX_RECONNECT_WAIT = 300 # seconds
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-many-arguments
|
def _build_publish_data(topic, qos, retain):
|
||||||
def publish(hass, topic, payload=None, qos=None,
|
"""Build the arguments for the publish service without the payload."""
|
||||||
retain=None, payload_template=None):
|
data = {ATTR_TOPIC: topic}
|
||||||
"""Publish message to an MQTT topic."""
|
|
||||||
data = {
|
|
||||||
ATTR_TOPIC: topic,
|
|
||||||
ATTR_PAYLOAD: payload,
|
|
||||||
ATTR_PAYLOAD_TEMPLATE: payload_template
|
|
||||||
}
|
|
||||||
if qos is not None:
|
if qos is not None:
|
||||||
data[ATTR_QOS] = qos
|
data[ATTR_QOS] = qos
|
||||||
|
|
||||||
if retain is not None:
|
if retain is not None:
|
||||||
data[ATTR_RETAIN] = retain
|
data[ATTR_RETAIN] = retain
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def publish(hass, topic, payload, qos=None, retain=None):
|
||||||
|
"""Publish message to an MQTT topic."""
|
||||||
|
data = _build_publish_data(topic, qos, retain)
|
||||||
|
data[ATTR_PAYLOAD] = payload
|
||||||
|
hass.services.call(DOMAIN, SERVICE_PUBLISH, data)
|
||||||
|
|
||||||
|
|
||||||
|
def publish_template(hass, topic, payload_template, qos=None, retain=None):
|
||||||
|
"""Publish message to an MQTT topic using a template payload."""
|
||||||
|
data = _build_publish_data(topic, qos, retain)
|
||||||
|
data[ATTR_PAYLOAD_TEMPLATE] = payload_template
|
||||||
hass.services.call(DOMAIN, SERVICE_PUBLISH, data)
|
hass.services.call(DOMAIN, SERVICE_PUBLISH, data)
|
||||||
# pylint: enable=too-many-arguments
|
|
||||||
|
|
||||||
|
|
||||||
def subscribe(hass, topic, callback, qos=DEFAULT_QOS):
|
def subscribe(hass, topic, callback, qos=DEFAULT_QOS):
|
||||||
|
|
|
@ -80,8 +80,7 @@ class TestMQTT(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
If 'payload_template' is provided and 'payload' is not, then render it.
|
If 'payload_template' is provided and 'payload' is not, then render it.
|
||||||
"""
|
"""
|
||||||
mqtt.publish(self.hass, "test/topic",
|
mqtt.publish_template(self.hass, "test/topic", "{{ 1+1 }}")
|
||||||
**{mqtt.ATTR_PAYLOAD_TEMPLATE: "{{ 1+1 }}"})
|
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
self.assertTrue(mqtt.MQTT_CLIENT.publish.called)
|
self.assertTrue(mqtt.MQTT_CLIENT.publish.called)
|
||||||
self.assertEqual(mqtt.MQTT_CLIENT.publish.call_args[0][1], "2")
|
self.assertEqual(mqtt.MQTT_CLIENT.publish.call_args[0][1], "2")
|
||||||
|
@ -92,9 +91,13 @@ class TestMQTT(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
payload = "not a template"
|
payload = "not a template"
|
||||||
payload_template = "a template"
|
payload_template = "a template"
|
||||||
mqtt.publish(self.hass, "test/topic", payload,
|
# Call the service directly because the helper functions don't allow
|
||||||
**{mqtt.ATTR_PAYLOAD_TEMPLATE: payload_template})
|
# you to provide payload AND payload_template.
|
||||||
self.hass.pool.block_till_done()
|
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.assertTrue(mqtt.MQTT_CLIENT.publish.called)
|
||||||
self.assertEqual(mqtt.MQTT_CLIENT.publish.call_args[0][1], payload)
|
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.
|
If neither 'payload' or 'payload_template' is provided then fail.
|
||||||
"""
|
"""
|
||||||
mqtt.publish(self.hass, "test/topic")
|
# Call the service directly because the helper functions require you to
|
||||||
self.hass.pool.block_till_done()
|
# 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)
|
self.assertFalse(mqtt.MQTT_CLIENT.publish.called)
|
||||||
|
|
||||||
def test_subscribe_topic(self):
|
def test_subscribe_topic(self):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue