core/homeassistant/components/mqtt/trigger.py

97 lines
2.9 KiB
Python
Raw Normal View History

"""Offer MQTT listening automation rules."""
import json
import logging
import voluptuous as vol
2015-08-10 00:12:22 +00:00
from homeassistant.const import CONF_PAYLOAD, CONF_PLATFORM, CONF_VALUE_TEMPLATE
from homeassistant.core import HassJob, callback
from homeassistant.helpers import config_validation as cv, template
2015-08-10 00:12:22 +00:00
from .. import mqtt
# mypy: allow-untyped-defs
2019-07-31 19:25:30 +00:00
CONF_ENCODING = "encoding"
CONF_QOS = "qos"
2019-07-31 19:25:30 +00:00
CONF_TOPIC = "topic"
DEFAULT_ENCODING = "utf-8"
DEFAULT_QOS = 0
2015-08-10 00:12:22 +00:00
2019-07-31 19:25:30 +00:00
TRIGGER_SCHEMA = vol.Schema(
{
vol.Required(CONF_PLATFORM): mqtt.DOMAIN,
vol.Required(CONF_TOPIC): mqtt.util.valid_subscribe_topic_template,
vol.Optional(CONF_PAYLOAD): cv.template,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
2019-07-31 19:25:30 +00:00
vol.Optional(CONF_ENCODING, default=DEFAULT_ENCODING): cv.string,
vol.Optional(CONF_QOS, default=DEFAULT_QOS): vol.All(
vol.Coerce(int), vol.In([0, 1, 2])
),
2019-07-31 19:25:30 +00:00
}
)
_LOGGER = logging.getLogger(__name__)
2015-08-10 00:12:22 +00:00
async def async_attach_trigger(hass, config, action, automation_info):
2016-03-07 16:14:55 +00:00
"""Listen for state changes based on configuration."""
topic = config[CONF_TOPIC]
wanted_payload = config.get(CONF_PAYLOAD)
value_template = config.get(CONF_VALUE_TEMPLATE)
encoding = config[CONF_ENCODING] or None
qos = config[CONF_QOS]
job = HassJob(action)
variables = None
if automation_info:
variables = automation_info.get("variables")
template.attach(hass, wanted_payload)
if wanted_payload:
wanted_payload = wanted_payload.async_render(
variables, limited=True, parse_result=False
)
template.attach(hass, topic)
if isinstance(topic, template.Template):
topic = topic.async_render(variables, limited=True, parse_result=False)
topic = mqtt.util.valid_subscribe_topic(topic)
2015-08-10 00:12:22 +00:00
template.attach(hass, value_template)
@callback
def mqtt_automation_listener(mqttmsg):
2016-03-07 19:20:07 +00:00
"""Listen for MQTT messages."""
payload = mqttmsg.payload
if value_template is not None:
payload = value_template.async_render_with_possible_json_value(
payload,
error_value=None,
)
if wanted_payload is None or wanted_payload == payload:
data = {
2019-07-31 19:25:30 +00:00
"platform": "mqtt",
"topic": mqttmsg.topic,
"payload": mqttmsg.payload,
"qos": mqttmsg.qos,
"description": f"mqtt topic {mqttmsg.topic}",
}
try:
2019-07-31 19:25:30 +00:00
data["payload_json"] = json.loads(mqttmsg.payload)
except ValueError:
pass
hass.async_run_hass_job(job, {"trigger": data})
2015-08-10 00:12:22 +00:00
_LOGGER.debug(
"Attaching MQTT trigger for topic: '%s', payload: '%s'", topic, wanted_payload
)
remove = await mqtt.async_subscribe(
hass, topic, mqtt_automation_listener, encoding=encoding, qos=qos
2019-07-31 19:25:30 +00:00
)
return remove