mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Adding MQTT automation rule
This commit is contained in:
parent
ae06267072
commit
044d43b7c2
@ -6,7 +6,7 @@ Allows to setup simple automation rules via the config file.
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.loader import get_component
|
from homeassistant.bootstrap import prepare_setup_platform
|
||||||
from homeassistant.helpers import config_per_platform
|
from homeassistant.helpers import config_per_platform
|
||||||
from homeassistant.util import split_entity_id
|
from homeassistant.util import split_entity_id
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
from homeassistant.const import ATTR_ENTITY_ID
|
||||||
@ -27,7 +27,7 @@ def setup(hass, config):
|
|||||||
""" Sets up automation. """
|
""" Sets up automation. """
|
||||||
|
|
||||||
for p_type, p_config in config_per_platform(config, DOMAIN, _LOGGER):
|
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:
|
if platform is None:
|
||||||
_LOGGER.error("Unknown automation platform specified: %s", p_type)
|
_LOGGER.error("Unknown automation platform specified: %s", p_type)
|
||||||
|
34
homeassistant/components/automation/mqtt.py
Normal file
34
homeassistant/components/automation/mqtt.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user