1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-29 22:24:26 +00:00

Text sensor schema generator similar to sensor (#3172)

This commit is contained in:
Jesse Hills
2022-02-08 17:23:45 +13:00
committed by GitHub
parent ad43d6a5bc
commit 69856286e8
25 changed files with 176 additions and 298 deletions

View File

@@ -1,7 +1,8 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import text_sensor, mqtt
from esphome.const import CONF_ID, CONF_QOS, CONF_TOPIC
from esphome.const import CONF_QOS, CONF_TOPIC
from .. import mqtt_subscribe_ns
DEPENDENCIES = ["mqtt"]
@@ -11,20 +12,23 @@ MQTTSubscribeTextSensor = mqtt_subscribe_ns.class_(
"MQTTSubscribeTextSensor", text_sensor.TextSensor, cg.Component
)
CONFIG_SCHEMA = text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(MQTTSubscribeTextSensor),
cv.GenerateID(CONF_MQTT_PARENT_ID): cv.use_id(mqtt.MQTTClientComponent),
cv.Required(CONF_TOPIC): cv.subscribe_topic,
cv.Optional(CONF_QOS, default=0): cv.mqtt_qos,
}
).extend(cv.COMPONENT_SCHEMA)
CONFIG_SCHEMA = (
text_sensor.text_sensor_schema()
.extend(
{
cv.GenerateID(): cv.declare_id(MQTTSubscribeTextSensor),
cv.GenerateID(CONF_MQTT_PARENT_ID): cv.use_id(mqtt.MQTTClientComponent),
cv.Required(CONF_TOPIC): cv.subscribe_topic,
cv.Optional(CONF_QOS, default=0): cv.mqtt_qos,
}
)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
var = await text_sensor.new_text_sensor(config)
await cg.register_component(var, config)
await text_sensor.register_text_sensor(var, config)
parent = await cg.get_variable(config[CONF_MQTT_PARENT_ID])
cg.add(var.set_parent(parent))