From 7c28eca6dee9f68de0fc8f2302aba5e66254e238 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sun, 10 Feb 2019 16:30:17 +0100 Subject: [PATCH] Print error when mqtt.publish used without MQTT enabled (#408) Fixes https://github.com/esphome/issues/issues/40 --- esphomeyaml/components/mqtt.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/esphomeyaml/components/mqtt.py b/esphomeyaml/components/mqtt.py index 8e067f3ada..43f2000230 100644 --- a/esphomeyaml/components/mqtt.py +++ b/esphomeyaml/components/mqtt.py @@ -16,7 +16,7 @@ from esphomeyaml.const import CONF_AVAILABILITY, CONF_BIRTH_MESSAGE, CONF_BROKER CONF_TOPIC_PREFIX, CONF_TRIGGER_ID, CONF_USERNAME, CONF_WILL_MESSAGE from esphomeyaml.core import EsphomeyamlError from esphomeyaml.cpp_generator import Pvariable, RawExpression, StructInitializer, \ - TemplateArguments, add, process_lambda, templatable + TemplateArguments, add, process_lambda, templatable, get_variable from esphomeyaml.cpp_types import Action, App, Component, JsonObjectConstRef, JsonObjectRef, \ Trigger, bool_, esphomelib_ns, optional, std_string, uint8, void @@ -194,6 +194,7 @@ def to_code(config): CONF_MQTT_PUBLISH = 'mqtt.publish' MQTT_PUBLISH_ACTION_SCHEMA = vol.Schema({ + cv.GenerateID(): cv.use_variable_id(MQTTClientComponent), vol.Required(CONF_TOPIC): cv.templatable(cv.publish_topic), vol.Required(CONF_PAYLOAD): cv.templatable(cv.mqtt_payload), vol.Optional(CONF_QOS): cv.templatable(cv.mqtt_qos), @@ -203,7 +204,9 @@ MQTT_PUBLISH_ACTION_SCHEMA = vol.Schema({ @ACTION_REGISTRY.register(CONF_MQTT_PUBLISH, MQTT_PUBLISH_ACTION_SCHEMA) def mqtt_publish_action_to_code(config, action_id, arg_type, template_arg): - rhs = App.Pget_mqtt_client().Pmake_publish_action(template_arg) + for var in get_variable(config[CONF_ID]): + yield None + rhs = var.make_publish_action(template_arg) type = MQTTPublishAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config[CONF_TOPIC], arg_type, std_string): @@ -226,6 +229,7 @@ def mqtt_publish_action_to_code(config, action_id, arg_type, template_arg): CONF_MQTT_PUBLISH_JSON = 'mqtt.publish_json' MQTT_PUBLISH_JSON_ACTION_SCHEMA = vol.Schema({ + cv.GenerateID(): cv.use_variable_id(MQTTClientComponent), vol.Required(CONF_TOPIC): cv.templatable(cv.publish_topic), vol.Required(CONF_PAYLOAD): cv.lambda_, vol.Optional(CONF_QOS): cv.mqtt_qos, @@ -235,7 +239,9 @@ MQTT_PUBLISH_JSON_ACTION_SCHEMA = vol.Schema({ @ACTION_REGISTRY.register(CONF_MQTT_PUBLISH_JSON, MQTT_PUBLISH_JSON_ACTION_SCHEMA) def mqtt_publish_json_action_to_code(config, action_id, arg_type, template_arg): - rhs = App.Pget_mqtt_client().Pmake_publish_json_action(template_arg) + for var in get_variable(config[CONF_ID]): + yield None + rhs = var.make_publish_json_action(template_arg) type = MQTTPublishJsonAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config[CONF_TOPIC], arg_type, std_string):