2019-02-13 20:21:14 +00:00
|
|
|
"""Offer MQTT listening automation rules."""
|
2016-12-04 17:53:05 +00:00
|
|
|
import json
|
|
|
|
|
2016-04-07 01:12:51 +00:00
|
|
|
import voluptuous as vol
|
2015-08-10 00:12:22 +00:00
|
|
|
|
2018-07-18 09:54:27 +00:00
|
|
|
from homeassistant.components import mqtt
|
2019-12-08 16:29:39 +00:00
|
|
|
from homeassistant.const import CONF_PAYLOAD, CONF_PLATFORM
|
2020-10-08 07:44:34 +00:00
|
|
|
from homeassistant.core import HassJob, callback
|
2016-04-07 01:12:51 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-08-10 00:12:22 +00:00
|
|
|
|
2019-08-12 03:38:18 +00:00
|
|
|
# mypy: allow-untyped-defs
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ENCODING = "encoding"
|
2020-02-18 21:51:10 +00:00
|
|
|
CONF_QOS = "qos"
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_TOPIC = "topic"
|
|
|
|
DEFAULT_ENCODING = "utf-8"
|
2020-02-18 21:51:10 +00:00
|
|
|
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,
|
2020-06-23 00:49:01 +00:00
|
|
|
vol.Required(CONF_TOPIC): mqtt.util.valid_subscribe_topic,
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional(CONF_PAYLOAD): cv.string,
|
|
|
|
vol.Optional(CONF_ENCODING, default=DEFAULT_ENCODING): cv.string,
|
2020-02-18 21:51:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
)
|
2016-04-07 01:12:51 +00:00
|
|
|
|
2015-08-10 00:12:22 +00:00
|
|
|
|
2019-09-24 21:57:05 +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."""
|
2019-01-25 06:43:56 +00:00
|
|
|
topic = config[CONF_TOPIC]
|
2015-08-10 00:12:22 +00:00
|
|
|
payload = config.get(CONF_PAYLOAD)
|
2019-01-25 06:43:56 +00:00
|
|
|
encoding = config[CONF_ENCODING] or None
|
2020-02-18 21:51:10 +00:00
|
|
|
qos = config[CONF_QOS]
|
2020-10-08 07:44:34 +00:00
|
|
|
job = HassJob(action)
|
2015-08-10 00:12:22 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
@callback
|
2019-03-14 17:58:32 +00:00
|
|
|
def mqtt_automation_listener(mqttmsg):
|
2016-03-07 19:20:07 +00:00
|
|
|
"""Listen for MQTT messages."""
|
2019-03-14 17:58:32 +00:00
|
|
|
if payload is None or payload == mqttmsg.payload:
|
2016-12-04 17:53:05 +00:00
|
|
|
data = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"platform": "mqtt",
|
|
|
|
"topic": mqttmsg.topic,
|
|
|
|
"payload": mqttmsg.payload,
|
|
|
|
"qos": mqttmsg.qos,
|
2020-08-28 15:02:12 +00:00
|
|
|
"description": f"mqtt topic {mqttmsg.topic}",
|
2016-12-04 17:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["payload_json"] = json.loads(mqttmsg.payload)
|
2016-12-04 17:53:05 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2020-10-08 07:44:34 +00:00
|
|
|
hass.async_run_hass_job(job, {"trigger": data})
|
2015-08-10 00:12:22 +00:00
|
|
|
|
2018-10-01 06:49:19 +00:00
|
|
|
remove = await mqtt.async_subscribe(
|
2020-02-18 21:51:10 +00:00
|
|
|
hass, topic, mqtt_automation_listener, encoding=encoding, qos=qos
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-02-18 22:17:18 +00:00
|
|
|
return remove
|