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

@@ -13,7 +13,7 @@ CONF_PIPSOLAR_ID = "pipsolar_id"
pipsolar_ns = cg.esphome_ns.namespace("pipsolar")
PipsolarComponent = pipsolar_ns.class_("Pipsolar", cg.Component)
PIPSOLAR_COMPONENT_SCHEMA = cv.COMPONENT_SCHEMA.extend(
PIPSOLAR_COMPONENT_SCHEMA = cv.Schema(
{
cv.Required(CONF_PIPSOLAR_ID): cv.use_id(PipsolarComponent),
}

View File

@@ -1,8 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import text_sensor
from esphome.const import CONF_ID
from .. import CONF_PIPSOLAR_ID, PIPSOLAR_COMPONENT_SCHEMA, pipsolar_ns
from .. import CONF_PIPSOLAR_ID, PIPSOLAR_COMPONENT_SCHEMA
DEPENDENCIES = ["uart"]
@@ -15,10 +14,6 @@ CONF_LAST_QPIWS = "last_qpiws"
CONF_LAST_QT = "last_qt"
CONF_LAST_QMN = "last_qmn"
PipsolarTextSensor = pipsolar_ns.class_(
"PipsolarTextSensor", text_sensor.TextSensor, cg.Component
)
TYPES = [
CONF_DEVICE_MODE,
CONF_LAST_QPIGS,
@@ -31,12 +26,7 @@ TYPES = [
]
CONFIG_SCHEMA = PIPSOLAR_COMPONENT_SCHEMA.extend(
{
cv.Optional(type): text_sensor.TEXT_SENSOR_SCHEMA.extend(
{cv.GenerateID(): cv.declare_id(PipsolarTextSensor)}
)
for type in TYPES
}
{cv.Optional(type): text_sensor.text_sensor_schema() for type in TYPES}
)
@@ -46,7 +36,5 @@ async def to_code(config):
for type in TYPES:
if type in config:
conf = config[type]
var = cg.new_Pvariable(conf[CONF_ID])
await text_sensor.register_text_sensor(var, conf)
await cg.register_component(var, conf)
var = await text_sensor.new_text_sensor(conf)
cg.add(getattr(paren, f"set_{type}")(var))

View File

@@ -1,13 +0,0 @@
#include "pipsolar_textsensor.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
namespace esphome {
namespace pipsolar {
static const char *const TAG = "pipsolar.text_sensor";
void PipsolarTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Pipsolar TextSensor", this); }
} // namespace pipsolar
} // namespace esphome

View File

@@ -1,20 +0,0 @@
#pragma once
#include "../pipsolar.h"
#include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/core/component.h"
namespace esphome {
namespace pipsolar {
class Pipsolar;
class PipsolarTextSensor : public Component, public text_sensor::TextSensor {
public:
void set_parent(Pipsolar *parent) { this->parent_ = parent; };
void dump_config() override;
protected:
Pipsolar *parent_;
};
} // namespace pipsolar
} // namespace esphome