From 6f54afec00ed02dfb8e5fb5b7f9490d450a2f4d2 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sun, 14 Oct 2018 18:45:13 +0200 Subject: [PATCH] Add MQTT Subscribe sensor (#175) --- .../components/sensor/mqtt_subscribe.py | 29 +++++++++++++++++++ tests/test2.yaml | 7 +++-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 esphomeyaml/components/sensor/mqtt_subscribe.py diff --git a/esphomeyaml/components/sensor/mqtt_subscribe.py b/esphomeyaml/components/sensor/mqtt_subscribe.py new file mode 100644 index 0000000000..2bfe71d98d --- /dev/null +++ b/esphomeyaml/components/sensor/mqtt_subscribe.py @@ -0,0 +1,29 @@ +import voluptuous as vol + +from esphomeyaml.components import sensor +import esphomeyaml.config_validation as cv +from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_QOS, CONF_TOPIC +from esphomeyaml.helpers import App, Application, add, variable + +DEPENDENCIES = ['mqtt'] + +MakeMQTTSubscribeSensor = Application.MakeMQTTSubscribeSensor + +PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ + cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeMQTTSubscribeSensor), + vol.Required(CONF_TOPIC): cv.subscribe_topic, + vol.Optional(CONF_QOS): cv.mqtt_qos, +})) + + +def to_code(config): + rhs = App.make_mqtt_subscribe_sensor(config[CONF_NAME], config[CONF_TOPIC]) + make = variable(config[CONF_MAKE_ID], rhs) + + if CONF_QOS in config: + add(make.Psensor.set_qos(config[CONF_QOS])) + + sensor.setup_sensor(make.Psensor, make.Pmqtt, config) + + +BUILD_FLAGS = '-DUSE_MQTT_SUBSCRIBE_SENSOR' diff --git a/tests/test2.yaml b/tests/test2.yaml index a1ac571483..379b7eba5f 100644 --- a/tests/test2.yaml +++ b/tests/test2.yaml @@ -1,5 +1,5 @@ esphomeyaml: - name: test1 + name: test2 platform: ESP32 board: nodemcu-32s # Use latest upstream esphomelib git version. @@ -73,7 +73,10 @@ sensor: name: "Xiaomi MiJia Humidity" battery_level: name: "Xiaomi MiJia Battery Level" - + - platform: mqtt_subscribe + name: "MQTT Subscribe Sensor 1" + topic: "mqtt/topic" + qos: 2 esp32_touch: setup_mode: True