From 75628b96a13a89530046e7cf2e289224f59227f6 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Wed, 17 Oct 2018 21:24:02 +0200 Subject: [PATCH] Add Text Sensors (#166) * Text Sensors * Add version text sensor * Fix * Fixes * Add tests * Add template text sensor * Lint * Fix test --- .../components/text_sensor/__init__.py | 60 +++++++++++++++++++ .../components/text_sensor/mqtt_subscribe.py | 27 +++++++++ .../components/text_sensor/template.py | 30 ++++++++++ esphomeyaml/components/text_sensor/version.py | 19 ++++++ tests/test2.yaml | 17 ++++++ 5 files changed, 153 insertions(+) create mode 100644 esphomeyaml/components/text_sensor/__init__.py create mode 100644 esphomeyaml/components/text_sensor/mqtt_subscribe.py create mode 100644 esphomeyaml/components/text_sensor/template.py create mode 100644 esphomeyaml/components/text_sensor/version.py diff --git a/esphomeyaml/components/text_sensor/__init__.py b/esphomeyaml/components/text_sensor/__init__.py new file mode 100644 index 0000000000..f4d57e64e4 --- /dev/null +++ b/esphomeyaml/components/text_sensor/__init__.py @@ -0,0 +1,60 @@ +import voluptuous as vol + +from esphomeyaml import automation +import esphomeyaml.config_validation as cv +from esphomeyaml.const import CONF_ICON, CONF_ID, CONF_INTERNAL, CONF_MQTT_ID, CONF_ON_VALUE, \ + CONF_TRIGGER_ID +from esphomeyaml.helpers import App, Pvariable, add, add_job, esphomelib_ns, setup_mqtt_component, \ + std_string + +PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ + +}) + +# pylint: disable=invalid-name +text_sensor_ns = esphomelib_ns.namespace('text_sensor') +TextSensor = text_sensor_ns.TextSensor +MQTTTextSensor = text_sensor_ns.MQTTTextSensor + +TextSensorValueTrigger = text_sensor_ns.TextSensorValueTrigger + +TEXT_SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend({ + cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTTextSensor), + cv.GenerateID(): cv.declare_variable_id(TextSensor), + vol.Optional(CONF_ICON): cv.icon, + vol.Optional(CONF_ON_VALUE): vol.All(cv.ensure_list, [automation.validate_automation({ + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(TextSensorValueTrigger), + })]), +}) + +TEXT_SENSOR_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(TEXT_SENSOR_SCHEMA.schema) + + +def setup_text_sensor_core_(text_sensor_var, mqtt_var, config): + if CONF_INTERNAL in config: + add(text_sensor_var.set_internal(config[CONF_INTERNAL])) + if CONF_ICON in config: + add(text_sensor_var.set_icon(config[CONF_ICON])) + + for conf in config.get(CONF_ON_VALUE, []): + rhs = text_sensor_var.make_value_trigger() + trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) + automation.build_automation(trigger, std_string, conf) + + setup_mqtt_component(mqtt_var, config) + + +def setup_text_sensor(text_sensor_obj, mqtt_obj, config): + sensor_var = Pvariable(config[CONF_ID], text_sensor_obj, has_side_effects=False) + mqtt_var = Pvariable(config[CONF_MQTT_ID], mqtt_obj, has_side_effects=False) + add_job(setup_text_sensor_core_, sensor_var, mqtt_var, config) + + +def register_text_sensor(var, config): + text_sensor_var = Pvariable(config[CONF_ID], var, has_side_effects=True) + rhs = App.register_text_sensor(text_sensor_var) + mqtt_var = Pvariable(config[CONF_MQTT_ID], rhs, has_side_effects=True) + add_job(setup_text_sensor_core_, text_sensor_var, mqtt_var, config) + + +BUILD_FLAGS = '-DUSE_TEXT_SENSOR' diff --git a/esphomeyaml/components/text_sensor/mqtt_subscribe.py b/esphomeyaml/components/text_sensor/mqtt_subscribe.py new file mode 100644 index 0000000000..18082d1f6e --- /dev/null +++ b/esphomeyaml/components/text_sensor/mqtt_subscribe.py @@ -0,0 +1,27 @@ +import voluptuous as vol + +from esphomeyaml.components import text_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'] + +MakeMQTTSubscribeTextSensor = Application.MakeMQTTSubscribeTextSensor + +PLATFORM_SCHEMA = cv.nameable(text_sensor.TEXT_SENSOR_PLATFORM_SCHEMA.extend({ + cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeMQTTSubscribeTextSensor), + vol.Required(CONF_TOPIC): cv.subscribe_topic, + vol.Optional(CONF_QOS): cv.mqtt_qos, +})) + + +def to_code(config): + rhs = App.make_mqtt_subscribe_text_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])) + text_sensor.setup_text_sensor(make.Psensor, make.Pmqtt, config) + + +BUILD_FLAGS = '-DUSE_MQTT_SUBSCRIBE_TEXT_SENSOR' diff --git a/esphomeyaml/components/text_sensor/template.py b/esphomeyaml/components/text_sensor/template.py new file mode 100644 index 0000000000..8e440b3fa5 --- /dev/null +++ b/esphomeyaml/components/text_sensor/template.py @@ -0,0 +1,30 @@ +import voluptuous as vol + +from esphomeyaml.components import text_sensor +import esphomeyaml.config_validation as cv +from esphomeyaml.const import CONF_LAMBDA, CONF_MAKE_ID, CONF_NAME, CONF_UPDATE_INTERVAL +from esphomeyaml.helpers import App, Application, add, optional, process_lambda, std_string, \ + variable + +MakeTemplateTextSensor = Application.MakeTemplateTextSensor + +PLATFORM_SCHEMA = cv.nameable(text_sensor.TEXT_SENSOR_PLATFORM_SCHEMA.extend({ + cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeTemplateTextSensor), + vol.Required(CONF_LAMBDA): cv.lambda_, + vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, +})) + + +def to_code(config): + rhs = App.make_template_text_sensor(config[CONF_NAME], config.get(CONF_UPDATE_INTERVAL)) + make = variable(config[CONF_MAKE_ID], rhs) + text_sensor.setup_text_sensor(make.Ptemplate_, make.Pmqtt, config) + + template_ = None + for template_ in process_lambda(config[CONF_LAMBDA], [], + return_type=optional.template(std_string)): + yield + add(make.Ptemplate_.set_template(template_)) + + +BUILD_FLAGS = '-DUSE_TEMPLATE_TEXT_SENSOR' diff --git a/esphomeyaml/components/text_sensor/version.py b/esphomeyaml/components/text_sensor/version.py new file mode 100644 index 0000000000..593211c715 --- /dev/null +++ b/esphomeyaml/components/text_sensor/version.py @@ -0,0 +1,19 @@ +from esphomeyaml.components import text_sensor +import esphomeyaml.config_validation as cv +from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME +from esphomeyaml.helpers import App, Application, variable + +MakeVersionTextSensor = Application.MakeVersionTextSensor + +PLATFORM_SCHEMA = cv.nameable(text_sensor.TEXT_SENSOR_PLATFORM_SCHEMA.extend({ + cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeVersionTextSensor), +})) + + +def to_code(config): + rhs = App.make_version_text_sensor(config[CONF_NAME]) + make = variable(config[CONF_MAKE_ID], rhs) + text_sensor.setup_text_sensor(make.Psensor, make.Pmqtt, config) + + +BUILD_FLAGS = '-DUSE_VERSION_TEXT_SENSOR' diff --git a/tests/test2.yaml b/tests/test2.yaml index 65a89e893b..4bfeb73c9f 100644 --- a/tests/test2.yaml +++ b/tests/test2.yaml @@ -110,3 +110,20 @@ esp32_ble_beacon: status_led: pin: GPIO2 + +text_sensor: + - platform: version + name: "Esphomelib Version" + icon: mdi:icon + id: version_sensor + on_value: + lambda: |- + ESP_LOGD("main", "The value is %s=%s", x.c_str(), id(version_sensor).value.c_str()); + - platform: mqtt_subscribe + name: "MQTT Subscribe Text" + topic: "the/topic" + qos: 2 + - platform: template + name: "Template Text Sensor" + lambda: |- + return {"Hello World"};