2018-10-17 21:24:02 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from esphomeyaml import automation
|
2018-11-03 14:08:31 +01:00
|
|
|
from esphomeyaml.components import mqtt
|
2018-11-19 22:12:24 +01:00
|
|
|
from esphomeyaml.components.mqtt import setup_mqtt_component
|
2018-10-17 21:24:02 +02:00
|
|
|
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
|
2018-11-19 22:12:24 +01:00
|
|
|
from esphomeyaml.core import CORE
|
|
|
|
from esphomeyaml.cpp_generator import Pvariable, add
|
|
|
|
from esphomeyaml.cpp_types import esphomelib_ns, Nameable, Trigger, std_string, App
|
2018-10-17 21:24:02 +02:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
text_sensor_ns = esphomelib_ns.namespace('text_sensor')
|
2018-11-12 23:30:31 +01:00
|
|
|
TextSensor = text_sensor_ns.class_('TextSensor', Nameable)
|
2018-11-19 22:12:24 +01:00
|
|
|
TextSensorPtr = TextSensor.operator('ptr')
|
2018-11-12 23:30:31 +01:00
|
|
|
MQTTTextSensor = text_sensor_ns.class_('MQTTTextSensor', mqtt.MQTTComponent)
|
2018-10-17 21:24:02 +02:00
|
|
|
|
2018-11-12 23:30:31 +01:00
|
|
|
TextSensorStateTrigger = text_sensor_ns.class_('TextSensorStateTrigger',
|
|
|
|
Trigger.template(std_string))
|
2018-10-17 21:24:02 +02:00
|
|
|
|
|
|
|
TEXT_SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend({
|
|
|
|
cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTTextSensor),
|
|
|
|
vol.Optional(CONF_ICON): cv.icon,
|
2018-10-20 13:15:09 +02:00
|
|
|
vol.Optional(CONF_ON_VALUE): automation.validate_automation({
|
2018-10-20 15:16:58 +02:00
|
|
|
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(TextSensorStateTrigger),
|
2018-10-20 13:15:09 +02:00
|
|
|
}),
|
2018-10-17 21:24:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
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, []):
|
2018-10-20 15:16:58 +02:00
|
|
|
rhs = text_sensor_var.make_state_trigger()
|
2018-10-17 21:24:02 +02:00
|
|
|
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)
|
2018-11-19 22:12:24 +01:00
|
|
|
CORE.add_job(setup_text_sensor_core_, sensor_var, mqtt_var, config)
|
2018-10-17 21:24:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2018-11-19 22:12:24 +01:00
|
|
|
CORE.add_job(setup_text_sensor_core_, text_sensor_var, mqtt_var, config)
|
2018-10-17 21:24:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
BUILD_FLAGS = '-DUSE_TEXT_SENSOR'
|
2018-11-03 14:08:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def core_to_hass_config(data, config):
|
|
|
|
ret = mqtt.build_hass_config(data, 'sensor', config, include_state=True, include_command=False)
|
|
|
|
if ret is None:
|
|
|
|
return None
|
|
|
|
if CONF_ICON in config:
|
|
|
|
ret['icon'] = config[CONF_ICON]
|
|
|
|
return ret
|