Adding MQTT automation rule

This commit is contained in:
Paulus Schoutsen 2015-08-09 17:12:22 -07:00
parent ae06267072
commit 044d43b7c2
2 changed files with 36 additions and 2 deletions

View File

@ -6,7 +6,7 @@ Allows to setup simple automation rules via the config file.
"""
import logging
from homeassistant.loader import get_component
from homeassistant.bootstrap import prepare_setup_platform
from homeassistant.helpers import config_per_platform
from homeassistant.util import split_entity_id
from homeassistant.const import ATTR_ENTITY_ID
@ -27,7 +27,7 @@ def setup(hass, config):
""" Sets up automation. """
for p_type, p_config in config_per_platform(config, DOMAIN, _LOGGER):
platform = get_component('automation.{}'.format(p_type))
platform = prepare_setup_platform(hass, config, DOMAIN, p_type)
if platform is None:
_LOGGER.error("Unknown automation platform specified: %s", p_type)

View File

@ -0,0 +1,34 @@
"""
homeassistant.components.automation.mqtt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Offers MQTT listening automation rules.
"""
import logging
import homeassistant.components.mqtt as mqtt
DEPENDENCIES = ['mqtt']
CONF_TOPIC = 'mqtt_topic'
CONF_PAYLOAD = 'mqtt_payload'
def register(hass, config, action):
""" 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