From 4e0c7f8a3d9c45dc5f74e15ed06f8f27e6cd0945 Mon Sep 17 00:00:00 2001 From: Flyte Date: Wed, 10 Feb 2016 22:38:33 +0000 Subject: [PATCH] Create additional mqtt helper function for using template payload. --- homeassistant/components/mqtt/__init__.py | 27 ++++++++++++++--------- tests/components/test_mqtt.py | 20 +++++++++++------ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 5e4e9ba2308..0721177c23d 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -57,23 +57,28 @@ ATTR_RETAIN = 'retain' MAX_RECONNECT_WAIT = 300 # seconds -# pylint: disable=too-many-arguments -def publish(hass, topic, payload=None, qos=None, - retain=None, payload_template=None): - """Publish message to an MQTT topic.""" - data = { - ATTR_TOPIC: topic, - ATTR_PAYLOAD: payload, - ATTR_PAYLOAD_TEMPLATE: payload_template - } +def _build_publish_data(topic, qos, retain): + """Build the arguments for the publish service without the payload.""" + data = {ATTR_TOPIC: topic} if qos is not None: data[ATTR_QOS] = qos - if retain is not None: 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) -# pylint: enable=too-many-arguments def subscribe(hass, topic, callback, qos=DEFAULT_QOS): diff --git a/tests/components/test_mqtt.py b/tests/components/test_mqtt.py index de8192a6b10..1a33eb6276b 100644 --- a/tests/components/test_mqtt.py +++ b/tests/components/test_mqtt.py @@ -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):