mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 00:37:53 +00:00
Update mqtt.publish() function to use template_payload. Reorganise publish service. Use mqtt.publish() in tests.
This commit is contained in:
parent
26fc637ab5
commit
d52e2019c0
@ -57,11 +57,14 @@ ATTR_RETAIN = 'retain'
|
||||
MAX_RECONNECT_WAIT = 300 # seconds
|
||||
|
||||
|
||||
def publish(hass, topic, payload, qos=None, retain=None):
|
||||
# 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
|
||||
}
|
||||
if qos is not None:
|
||||
data[ATTR_QOS] = qos
|
||||
@ -70,6 +73,7 @@ def publish(hass, topic, payload, qos=None, retain=None):
|
||||
data[ATTR_RETAIN] = retain
|
||||
|
||||
hass.services.call(DOMAIN, SERVICE_PUBLISH, data)
|
||||
# pylint: enable=too-many-arguments
|
||||
|
||||
|
||||
def subscribe(hass, topic, callback, qos=DEFAULT_QOS):
|
||||
@ -134,24 +138,25 @@ def setup(hass, config):
|
||||
"""Handle MQTT publish service calls."""
|
||||
msg_topic = call.data.get(ATTR_TOPIC)
|
||||
payload = call.data.get(ATTR_PAYLOAD)
|
||||
payload_template = call.data.get(ATTR_PAYLOAD_TEMPLATE)
|
||||
qos = call.data.get(ATTR_QOS, DEFAULT_QOS)
|
||||
retain = call.data.get(ATTR_RETAIN, DEFAULT_RETAIN)
|
||||
if payload is None:
|
||||
if payload_template is None:
|
||||
_LOGGER.error(
|
||||
"You must set either '%s' or '%s' to use this service",
|
||||
ATTR_PAYLOAD, ATTR_PAYLOAD_TEMPLATE)
|
||||
return
|
||||
try:
|
||||
payload_template = call.data.get(ATTR_PAYLOAD_TEMPLATE)
|
||||
if payload_template is None:
|
||||
_LOGGER.error(
|
||||
"You must set either '%s' or '%s' to use this service",
|
||||
ATTR_PAYLOAD, ATTR_PAYLOAD_TEMPLATE)
|
||||
return
|
||||
payload = template.render(hass, payload_template)
|
||||
except AttributeError as exc:
|
||||
except template.jinja2.TemplateError as exc:
|
||||
_LOGGER.error(
|
||||
"Unable to publish to '%s': rendering payload template of "
|
||||
"'%s' failed because %s.",
|
||||
msg_topic, payload_template, exc)
|
||||
return
|
||||
if msg_topic is None or payload is None:
|
||||
return
|
||||
qos = call.data.get(ATTR_QOS, DEFAULT_QOS)
|
||||
retain = call.data.get(ATTR_RETAIN, DEFAULT_RETAIN)
|
||||
MQTT_CLIENT.publish(msg_topic, payload, qos, retain)
|
||||
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_mqtt)
|
||||
|
@ -80,10 +80,9 @@ class TestMQTT(unittest.TestCase):
|
||||
"""
|
||||
If 'payload_template' is provided and 'payload' is not, then render it.
|
||||
"""
|
||||
self.hass.services.call(mqtt.DOMAIN, mqtt.SERVICE_PUBLISH, {
|
||||
mqtt.ATTR_TOPIC: "test/topic",
|
||||
mqtt.ATTR_PAYLOAD_TEMPLATE: "{{ 1+1 }}"
|
||||
}, blocking=True)
|
||||
mqtt.publish(self.hass, "test/topic",
|
||||
**{mqtt.ATTR_PAYLOAD_TEMPLATE: "{{ 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")
|
||||
|
||||
@ -93,11 +92,9 @@ class TestMQTT(unittest.TestCase):
|
||||
"""
|
||||
payload = "not a template"
|
||||
payload_template = "a 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)
|
||||
mqtt.publish(self.hass, "test/topic", payload,
|
||||
**{mqtt.ATTR_PAYLOAD_TEMPLATE: payload_template})
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertTrue(mqtt.MQTT_CLIENT.publish.called)
|
||||
self.assertEqual(mqtt.MQTT_CLIENT.publish.call_args[0][1], payload)
|
||||
|
||||
@ -105,9 +102,8 @@ class TestMQTT(unittest.TestCase):
|
||||
"""
|
||||
If neither 'payload' or 'payload_template' is provided then fail.
|
||||
"""
|
||||
self.hass.services.call(mqtt.DOMAIN, mqtt.SERVICE_PUBLISH, {
|
||||
mqtt.ATTR_TOPIC: "test/topic"
|
||||
}, blocking=True)
|
||||
mqtt.publish(self.hass, "test/topic")
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertFalse(mqtt.MQTT_CLIENT.publish.called)
|
||||
|
||||
def test_subscribe_topic(self):
|
||||
|
Loading…
x
Reference in New Issue
Block a user