hass-core/homeassistant/components/automation/mqtt.py

44 lines
1.3 KiB
Python
Raw Normal View History

2015-08-09 17:12:22 -07:00
"""
2016-03-07 20:20:07 +01:00
Offer MQTT listening automation rules.
2015-10-13 21:07:40 +02:00
For more details about this automation rule, please refer to the documentation
2015-11-09 13:12:18 +01:00
at https://home-assistant.io/components/automation/#mqtt-trigger
2015-08-09 17:12:22 -07:00
"""
import voluptuous as vol
2015-08-09 17:12:22 -07:00
from homeassistant.core import callback
2015-08-09 17:12:22 -07:00
import homeassistant.components.mqtt as mqtt
from homeassistant.const import (CONF_PLATFORM, CONF_PAYLOAD)
import homeassistant.helpers.config_validation as cv
2015-08-09 17:12:22 -07:00
DEPENDENCIES = ['mqtt']
2015-09-14 22:05:40 -07:00
CONF_TOPIC = 'topic'
2015-08-09 17:12:22 -07:00
TRIGGER_SCHEMA = vol.Schema({
vol.Required(CONF_PLATFORM): mqtt.DOMAIN,
vol.Required(CONF_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_PAYLOAD): cv.string,
})
2015-08-09 17:12:22 -07:00
2016-10-01 01:22:13 -07:00
def async_trigger(hass, config, action):
2016-03-07 17:14:55 +01:00
"""Listen for state changes based on configuration."""
topic = config.get(CONF_TOPIC)
2015-08-09 17:12:22 -07:00
payload = config.get(CONF_PAYLOAD)
@callback
2015-08-09 17:12:22 -07:00
def mqtt_automation_listener(msg_topic, msg_payload, qos):
2016-03-07 20:20:07 +01:00
"""Listen for MQTT messages."""
2015-08-09 17:12:22 -07:00
if payload is None or payload == msg_payload:
hass.async_run_job(action, {
'trigger': {
'platform': 'mqtt',
'topic': msg_topic,
'payload': msg_payload,
'qos': qos,
}
})
2015-08-09 17:12:22 -07:00
2016-10-01 01:22:13 -07:00
return mqtt.async_subscribe(hass, topic, mqtt_automation_listener)