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

37 lines
976 B
Python
Raw Normal View History

2015-08-09 17:12:22 -07:00
"""
homeassistant.components.automation.mqtt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Offers 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 logging
import homeassistant.components.mqtt as mqtt
DEPENDENCIES = ['mqtt']
2015-09-14 22:05:40 -07:00
CONF_TOPIC = 'topic'
CONF_PAYLOAD = 'payload'
2015-08-09 17:12:22 -07:00
2015-09-13 22:25:42 -07:00
def trigger(hass, config, action):
2015-08-09 17:12:22 -07:00
""" Listen for state changes based on `config`. """
topic = config.get(CONF_TOPIC)
payload = config.get(CONF_PAYLOAD)
if topic is None:
logging.getLogger(__name__).error(
"Missing configuration key %s", CONF_TOPIC)
return False
def mqtt_automation_listener(msg_topic, msg_payload, qos):
""" Listens for MQTT messages. """
if payload is None or payload == msg_payload:
action()
mqtt.subscribe(hass, topic, mqtt_automation_listener)
return True