mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 22:48:10 +01:00
Add Text Sensors (#166)
* Text Sensors * Add version text sensor * Fix * Fixes * Add tests * Add template text sensor * Lint * Fix test
This commit is contained in:
parent
b1f7ed4fdc
commit
75628b96a1
5 changed files with 153 additions and 0 deletions
60
esphomeyaml/components/text_sensor/__init__.py
Normal file
60
esphomeyaml/components/text_sensor/__init__.py
Normal file
|
@ -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'
|
27
esphomeyaml/components/text_sensor/mqtt_subscribe.py
Normal file
27
esphomeyaml/components/text_sensor/mqtt_subscribe.py
Normal file
|
@ -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'
|
30
esphomeyaml/components/text_sensor/template.py
Normal file
30
esphomeyaml/components/text_sensor/template.py
Normal file
|
@ -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'
|
19
esphomeyaml/components/text_sensor/version.py
Normal file
19
esphomeyaml/components/text_sensor/version.py
Normal file
|
@ -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'
|
|
@ -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"};
|
||||
|
|
Loading…
Reference in a new issue