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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 176 additions and 298 deletions

View file

@ -1,7 +1,6 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import text_sensor, esp32_ble_tracker from esphome.components import text_sensor, esp32_ble_tracker
from esphome.const import CONF_ID
DEPENDENCIES = ["esp32_ble_tracker"] DEPENDENCIES = ["esp32_ble_tracker"]
@ -14,18 +13,13 @@ BLEScanner = ble_scanner_ns.class_(
) )
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
text_sensor.TEXT_SENSOR_SCHEMA.extend( text_sensor.text_sensor_schema(klass=BLEScanner)
{
cv.GenerateID(): cv.declare_id(BLEScanner),
}
)
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA) .extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA) .extend(cv.COMPONENT_SCHEMA)
) )
async def to_code(config): 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 cg.register_component(var, config)
await esp32_ble_tracker.register_ble_device(var, config) await esp32_ble_tracker.register_ble_device(var, config)
await text_sensor.register_text_sensor(var, config)

View file

@ -1,7 +1,6 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import text_sensor from esphome.components import text_sensor
from esphome.const import CONF_ID, CONF_ICON
from . import BME680BSECComponent, CONF_BME680_BSEC_ID from . import BME680BSECComponent, CONF_BME680_BSEC_ID
DEPENDENCIES = ["bme680_bsec"] DEPENDENCIES = ["bme680_bsec"]
@ -14,11 +13,8 @@ TYPES = [CONF_IAQ_ACCURACY]
CONFIG_SCHEMA = cv.Schema( CONFIG_SCHEMA = cv.Schema(
{ {
cv.GenerateID(CONF_BME680_BSEC_ID): cv.use_id(BME680BSECComponent), cv.GenerateID(CONF_BME680_BSEC_ID): cv.use_id(BME680BSECComponent),
cv.Optional(CONF_IAQ_ACCURACY): text_sensor.TEXT_SENSOR_SCHEMA.extend( cv.Optional(CONF_IAQ_ACCURACY): text_sensor.text_sensor_schema(
{ icon=ICON_ACCURACY
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
cv.Optional(CONF_ICON, default=ICON_ACCURACY): cv.icon,
}
), ),
} }
) )
@ -27,8 +23,7 @@ CONFIG_SCHEMA = cv.Schema(
async def setup_conf(config, key, hub): async def setup_conf(config, key, hub):
if key in config: if key in config:
conf = config[key] conf = config[key]
sens = cg.new_Pvariable(conf[CONF_ID]) sens = await text_sensor.new_text_sensor(conf)
await text_sensor.register_text_sensor(sens, conf)
cg.add(getattr(hub, f"set_{key}_text_sensor")(sens)) cg.add(getattr(hub, f"set_{key}_text_sensor")(sens))

View file

@ -2,7 +2,6 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import i2c, sensor, text_sensor from esphome.components import i2c, sensor, text_sensor
from esphome.const import ( from esphome.const import (
CONF_ICON,
CONF_ID, CONF_ID,
ICON_RADIATOR, ICON_RADIATOR,
ICON_RESTART, ICON_RESTART,
@ -47,11 +46,8 @@ CONFIG_SCHEMA = (
device_class=DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS, device_class=DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
), ),
cv.Optional(CONF_VERSION): text_sensor.TEXT_SENSOR_SCHEMA.extend( cv.Optional(CONF_VERSION): text_sensor.text_sensor_schema(
{ icon=ICON_RESTART
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
cv.Optional(CONF_ICON, default=ICON_RESTART): cv.icon,
}
), ),
cv.Optional(CONF_BASELINE): cv.hex_uint16_t, cv.Optional(CONF_BASELINE): cv.hex_uint16_t,
cv.Optional(CONF_TEMPERATURE): cv.use_id(sensor.Sensor), cv.Optional(CONF_TEMPERATURE): cv.use_id(sensor.Sensor),
@ -74,8 +70,7 @@ async def to_code(config):
cg.add(var.set_tvoc(sens)) cg.add(var.set_tvoc(sens))
if CONF_VERSION in config: if CONF_VERSION in config:
sens = cg.new_Pvariable(config[CONF_VERSION][CONF_ID]) sens = await text_sensor.new_text_sensor(config[CONF_VERSION])
await text_sensor.register_text_sensor(sens, config[CONF_VERSION])
cg.add(var.set_version(sens)) cg.add(var.set_version(sens))
if CONF_BASELINE in config: if CONF_BASELINE in config:

View file

@ -11,11 +11,7 @@ CONFIG_SCHEMA = cv.Schema(
cv.GenerateID(): cv.declare_id(CustomTextSensorConstructor), cv.GenerateID(): cv.declare_id(CustomTextSensorConstructor),
cv.Required(CONF_LAMBDA): cv.returning_lambda, cv.Required(CONF_LAMBDA): cv.returning_lambda,
cv.Required(CONF_TEXT_SENSORS): cv.ensure_list( cv.Required(CONF_TEXT_SENSORS): cv.ensure_list(
text_sensor.TEXT_SENSOR_SCHEMA.extend( text_sensor.text_sensor_schema()
{
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
}
)
), ),
} }
) )

View file

@ -1,7 +1,7 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import text_sensor from esphome.components import text_sensor
from esphome.const import CONF_ICON, CONF_ID, CONF_STATUS from esphome.const import CONF_STATUS
from . import DalyBmsComponent, CONF_BMS_DALY_ID from . import DalyBmsComponent, CONF_BMS_DALY_ID
ICON_CAR_BATTERY = "mdi:car-battery" ICON_CAR_BATTERY = "mdi:car-battery"
@ -14,11 +14,8 @@ CONFIG_SCHEMA = cv.All(
cv.Schema( cv.Schema(
{ {
cv.GenerateID(CONF_BMS_DALY_ID): cv.use_id(DalyBmsComponent), cv.GenerateID(CONF_BMS_DALY_ID): cv.use_id(DalyBmsComponent),
cv.Optional(CONF_STATUS): text_sensor.TEXT_SENSOR_SCHEMA.extend( cv.Optional(CONF_STATUS): text_sensor.text_sensor_schema(
{ icon=ICON_CAR_BATTERY
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
cv.Optional(CONF_ICON, default=ICON_CAR_BATTERY): cv.icon,
}
), ),
} }
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)
@ -28,8 +25,7 @@ CONFIG_SCHEMA = cv.All(
async def setup_conf(config, key, hub): async def setup_conf(config, key, hub):
if key in config: if key in config:
conf = config[key] conf = config[key]
sens = cg.new_Pvariable(conf[CONF_ID]) sens = await text_sensor.new_text_sensor(conf)
await text_sensor.register_text_sensor(sens, conf)
cg.add(getattr(hub, f"set_{key}_text_sensor")(sens)) cg.add(getattr(hub, f"set_{key}_text_sensor")(sens))

View file

@ -1,10 +1,8 @@
from esphome.components import text_sensor from esphome.components import text_sensor
import esphome.config_validation as cv import esphome.config_validation as cv
import esphome.codegen as cg import esphome.codegen as cg
from esphome.const import ( from esphome.const import CONF_DEVICE
CONF_ID,
CONF_DEVICE,
)
from . import CONF_DEBUG_ID, DebugComponent from . import CONF_DEBUG_ID, DebugComponent
DEPENDENCIES = ["debug"] DEPENDENCIES = ["debug"]
@ -13,9 +11,7 @@ DEPENDENCIES = ["debug"]
CONFIG_SCHEMA = cv.Schema( CONFIG_SCHEMA = cv.Schema(
{ {
cv.GenerateID(CONF_DEBUG_ID): cv.use_id(DebugComponent), cv.GenerateID(CONF_DEBUG_ID): cv.use_id(DebugComponent),
cv.Optional(CONF_DEVICE): text_sensor.TEXT_SENSOR_SCHEMA.extend( cv.Optional(CONF_DEVICE): text_sensor.text_sensor_schema(),
{cv.GenerateID(): cv.declare_id(text_sensor.TextSensor)}
),
} }
) )
@ -24,6 +20,5 @@ async def to_code(config):
debug_component = await cg.get_variable(config[CONF_DEBUG_ID]) debug_component = await cg.get_variable(config[CONF_DEBUG_ID])
if CONF_DEVICE in config: if CONF_DEVICE in config:
sens = cg.new_Pvariable(config[CONF_DEVICE][CONF_ID]) sens = await text_sensor.new_text_sensor(config[CONF_DEVICE])
await text_sensor.register_text_sensor(sens, config[CONF_DEVICE])
cg.add(debug_component.set_device_info_sensor(sens)) cg.add(debug_component.set_device_info_sensor(sens))

View file

@ -37,12 +37,10 @@ from esphome.const import (
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TEMPERATURE,
ICON_BLUETOOTH, ICON_BLUETOOTH,
ICON_BLUR, ICON_BLUR,
ICON_EMPTY,
ICON_THERMOMETER, ICON_THERMOMETER,
STATE_CLASS_MEASUREMENT, STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING, STATE_CLASS_TOTAL_INCREASING,
UNIT_CELSIUS, UNIT_CELSIUS,
UNIT_EMPTY,
UNIT_PERCENT, UNIT_PERCENT,
UNIT_WATT_HOURS, UNIT_WATT_HOURS,
) )
@ -339,7 +337,7 @@ CONFIG_SCHEMA = cv.Schema(
}, },
], ],
): [ ): [
sensor.sensor_schema(UNIT_EMPTY, ICON_EMPTY, 0) sensor.sensor_schema(accuracy_decimals=0)
.extend(cv.polling_component_schema("60s")) .extend(cv.polling_component_schema("60s"))
.extend( .extend(
{ {
@ -378,12 +376,8 @@ CONFIG_SCHEMA = cv.Schema(
}, },
], ],
): [ ): [
text_sensor.TEXT_SENSOR_SCHEMA.extend( text_sensor.text_sensor_schema(klass=DemoTextSensor).extend(
cv.polling_component_schema("60s") cv.polling_component_schema("60s")
).extend(
{
cv.GenerateID(): cv.declare_id(DemoTextSensor),
}
) )
], ],
} }
@ -443,6 +437,5 @@ async def to_code(config):
await switch.register_switch(var, conf) await switch.register_switch(var, conf)
for conf in config[CONF_TEXT_SENSORS]: for conf in config[CONF_TEXT_SENSORS]:
var = cg.new_Pvariable(conf[CONF_ID]) var = await text_sensor.new_text_sensor(conf)
await cg.register_component(var, conf) await cg.register_component(var, conf)
await text_sensor.register_text_sensor(var, conf)

View file

@ -1,9 +1,7 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import text_sensor from esphome.components import text_sensor
from esphome.const import (
CONF_ID,
)
from . import Dsmr, CONF_DSMR_ID from . import Dsmr, CONF_DSMR_ID
AUTO_LOAD = ["dsmr"] AUTO_LOAD = ["dsmr"]
@ -11,71 +9,19 @@ AUTO_LOAD = ["dsmr"]
CONFIG_SCHEMA = cv.Schema( CONFIG_SCHEMA = cv.Schema(
{ {
cv.GenerateID(CONF_DSMR_ID): cv.use_id(Dsmr), cv.GenerateID(CONF_DSMR_ID): cv.use_id(Dsmr),
cv.Optional("identification"): text_sensor.TEXT_SENSOR_SCHEMA.extend( cv.Optional("identification"): text_sensor.text_sensor_schema(),
{ cv.Optional("p1_version"): text_sensor.text_sensor_schema(),
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor), cv.Optional("p1_version_be"): text_sensor.text_sensor_schema(),
} cv.Optional("timestamp"): text_sensor.text_sensor_schema(),
), cv.Optional("electricity_tariff"): text_sensor.text_sensor_schema(),
cv.Optional("p1_version"): text_sensor.TEXT_SENSOR_SCHEMA.extend( cv.Optional("electricity_failure_log"): text_sensor.text_sensor_schema(),
{ cv.Optional("message_short"): text_sensor.text_sensor_schema(),
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor), cv.Optional("message_long"): text_sensor.text_sensor_schema(),
} cv.Optional("gas_equipment_id"): text_sensor.text_sensor_schema(),
), cv.Optional("thermal_equipment_id"): text_sensor.text_sensor_schema(),
cv.Optional("p1_version_be"): text_sensor.TEXT_SENSOR_SCHEMA.extend( cv.Optional("water_equipment_id"): text_sensor.text_sensor_schema(),
{ cv.Optional("sub_equipment_id"): text_sensor.text_sensor_schema(),
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor), cv.Optional("gas_delivered_text"): text_sensor.text_sensor_schema(),
}
),
cv.Optional("timestamp"): text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
}
),
cv.Optional("electricity_tariff"): text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
}
),
cv.Optional("electricity_failure_log"): text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
}
),
cv.Optional("message_short"): text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
}
),
cv.Optional("message_long"): text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
}
),
cv.Optional("gas_equipment_id"): text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
}
),
cv.Optional("thermal_equipment_id"): text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
}
),
cv.Optional("water_equipment_id"): text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
}
),
cv.Optional("sub_equipment_id"): text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
}
),
cv.Optional("gas_delivered_text"): text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
}
),
} }
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)
@ -89,8 +35,7 @@ async def to_code(config):
continue continue
id = conf.get("id") id = conf.get("id")
if id and id.type == text_sensor.TextSensor: if id and id.type == text_sensor.TextSensor:
var = cg.new_Pvariable(conf[CONF_ID]) var = await text_sensor.new_text_sensor(conf)
await text_sensor.register_text_sensor(var, conf)
cg.add(getattr(hub, f"set_{key}")(var)) cg.add(getattr(hub, f"set_{key}")(var))
text_sensors.append(f"F({key})") text_sensors.append(f"F({key})")

View file

@ -1,7 +1,8 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import text_sensor from esphome.components import text_sensor
from esphome.const import CONF_ATTRIBUTE, CONF_ENTITY_ID, CONF_ID from esphome.const import CONF_ATTRIBUTE, CONF_ENTITY_ID
from .. import homeassistant_ns from .. import homeassistant_ns
DEPENDENCIES = ["api"] DEPENDENCIES = ["api"]
@ -10,7 +11,7 @@ HomeassistantTextSensor = homeassistant_ns.class_(
"HomeassistantTextSensor", text_sensor.TextSensor, cg.Component "HomeassistantTextSensor", text_sensor.TextSensor, cg.Component
) )
CONFIG_SCHEMA = text_sensor.TEXT_SENSOR_SCHEMA.extend( CONFIG_SCHEMA = text_sensor.text_sensor_schema().extend(
{ {
cv.GenerateID(): cv.declare_id(HomeassistantTextSensor), cv.GenerateID(): cv.declare_id(HomeassistantTextSensor),
cv.Required(CONF_ENTITY_ID): cv.entity_id, cv.Required(CONF_ENTITY_ID): cv.entity_id,
@ -20,9 +21,8 @@ CONFIG_SCHEMA = text_sensor.TEXT_SENSOR_SCHEMA.extend(
async def to_code(config): 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 cg.register_component(var, config)
await text_sensor.register_text_sensor(var, config)
cg.add(var.set_entity_id(config[CONF_ENTITY_ID])) cg.add(var.set_entity_id(config[CONF_ENTITY_ID]))
if CONF_ATTRIBUTE in config: if CONF_ATTRIBUTE in config:

View file

@ -40,7 +40,8 @@ RAW_ENCODING = {
} }
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
text_sensor.TEXT_SENSOR_SCHEMA.extend(cv.COMPONENT_SCHEMA) text_sensor.text_sensor_schema()
.extend(cv.COMPONENT_SCHEMA)
.extend(ModbusItemBaseSchema) .extend(ModbusItemBaseSchema)
.extend( .extend(
{ {

View file

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

View file

@ -17,11 +17,7 @@ NextionTextSensor = nextion_ns.class_(
) )
CONFIG_SCHEMA = ( CONFIG_SCHEMA = (
text_sensor.TEXT_SENSOR_SCHEMA.extend( text_sensor.text_sensor_schema(klass=NextionTextSensor)
{
cv.GenerateID(): cv.declare_id(NextionTextSensor),
}
)
.extend(CONFIG_TEXT_COMPONENT_SCHEMA) .extend(CONFIG_TEXT_COMPONENT_SCHEMA)
.extend(cv.polling_component_schema("never")) .extend(cv.polling_component_schema("never"))
) )
@ -30,8 +26,8 @@ CONFIG_SCHEMA = (
async def to_code(config): async def to_code(config):
hub = await cg.get_variable(config[CONF_NEXTION_ID]) hub = await cg.get_variable(config[CONF_NEXTION_ID])
var = cg.new_Pvariable(config[CONF_ID], hub) var = cg.new_Pvariable(config[CONF_ID], hub)
await cg.register_component(var, config)
await text_sensor.register_text_sensor(var, config) await text_sensor.register_text_sensor(var, config)
await cg.register_component(var, config)
cg.add(hub.register_textsensor_component(var)) cg.add(hub.register_textsensor_component(var))

View file

@ -13,7 +13,7 @@ CONF_PIPSOLAR_ID = "pipsolar_id"
pipsolar_ns = cg.esphome_ns.namespace("pipsolar") pipsolar_ns = cg.esphome_ns.namespace("pipsolar")
PipsolarComponent = pipsolar_ns.class_("Pipsolar", cg.Component) 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), cv.Required(CONF_PIPSOLAR_ID): cv.use_id(PipsolarComponent),
} }

View file

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

View file

@ -6,7 +6,6 @@ from esphome.const import (
ICON_WEATHER_SUNSET_DOWN, ICON_WEATHER_SUNSET_DOWN,
ICON_WEATHER_SUNSET_UP, ICON_WEATHER_SUNSET_UP,
CONF_TYPE, CONF_TYPE,
CONF_ID,
CONF_FORMAT, CONF_FORMAT,
) )
from .. import sun_ns, CONF_SUN_ID, Sun, CONF_ELEVATION, elevation, DEFAULT_ELEVATION from .. import sun_ns, CONF_SUN_ID, Sun, CONF_ELEVATION, elevation, DEFAULT_ELEVATION
@ -33,7 +32,8 @@ def validate_optional_icon(config):
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
text_sensor.TEXT_SENSOR_SCHEMA.extend( text_sensor.text_sensor_schema()
.extend(
{ {
cv.GenerateID(): cv.declare_id(SunTextSensor), cv.GenerateID(): cv.declare_id(SunTextSensor),
cv.GenerateID(CONF_SUN_ID): cv.use_id(Sun), cv.GenerateID(CONF_SUN_ID): cv.use_id(Sun),
@ -41,15 +41,15 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_ELEVATION, default=DEFAULT_ELEVATION): elevation, cv.Optional(CONF_ELEVATION, default=DEFAULT_ELEVATION): elevation,
cv.Optional(CONF_FORMAT, default="%X"): cv.string_strict, cv.Optional(CONF_FORMAT, default="%X"): cv.string_strict,
} }
).extend(cv.polling_component_schema("60s")), )
.extend(cv.polling_component_schema("60s")),
validate_optional_icon, validate_optional_icon,
) )
async def to_code(config): 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 cg.register_component(var, config)
await text_sensor.register_text_sensor(var, config)
paren = await cg.get_variable(config[CONF_SUN_ID]) paren = await cg.get_variable(config[CONF_SUN_ID])
cg.add(var.set_parent(paren)) cg.add(var.set_parent(paren))

View file

@ -7,9 +7,20 @@ CODEOWNERS = ["@0hax"]
teleinfo_ns = cg.esphome_ns.namespace("teleinfo") teleinfo_ns = cg.esphome_ns.namespace("teleinfo")
TeleInfo = teleinfo_ns.class_("TeleInfo", cg.PollingComponent, uart.UARTDevice) TeleInfo = teleinfo_ns.class_("TeleInfo", cg.PollingComponent, uart.UARTDevice)
CONF_TELEINFO_ID = "teleinfo_id"
CONF_TELEINFO_ID = "teleinfo_id"
CONF_TAG_NAME = "tag_name"
CONF_HISTORICAL_MODE = "historical_mode" CONF_HISTORICAL_MODE = "historical_mode"
TELEINFO_LISTENER_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_TELEINFO_ID): cv.use_id(TeleInfo),
cv.Required(CONF_TAG_NAME): cv.string,
}
)
CONFIG_SCHEMA = ( CONFIG_SCHEMA = (
cv.Schema( cv.Schema(
{ {

View file

@ -3,20 +3,22 @@ import esphome.config_validation as cv
from esphome.components import sensor from esphome.components import sensor
from esphome.const import CONF_ID, ICON_FLASH, UNIT_WATT_HOURS from esphome.const import CONF_ID, ICON_FLASH, UNIT_WATT_HOURS
from .. import teleinfo_ns, TeleInfo, CONF_TELEINFO_ID from .. import (
CONF_TAG_NAME,
TELEINFO_LISTENER_SCHEMA,
teleinfo_ns,
CONF_TELEINFO_ID,
)
CONF_TAG_NAME = "tag_name"
TeleInfoSensor = teleinfo_ns.class_("TeleInfoSensor", sensor.Sensor, cg.Component) TeleInfoSensor = teleinfo_ns.class_("TeleInfoSensor", sensor.Sensor, cg.Component)
CONFIG_SCHEMA = sensor.sensor_schema( CONFIG_SCHEMA = (
unit_of_measurement=UNIT_WATT_HOURS, icon=ICON_FLASH, accuracy_decimals=0 sensor.sensor_schema(
).extend( unit_of_measurement=UNIT_WATT_HOURS, icon=ICON_FLASH, accuracy_decimals=0
{ )
cv.GenerateID(): cv.declare_id(TeleInfoSensor), .extend({cv.GenerateID(): cv.declare_id(TeleInfoSensor)})
cv.GenerateID(CONF_TELEINFO_ID): cv.use_id(TeleInfo), .extend(TELEINFO_LISTENER_SCHEMA)
cv.Required(CONF_TAG_NAME): cv.string,
}
) )

View file

@ -1,22 +1,15 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import text_sensor from esphome.components import text_sensor
from esphome.const import CONF_ID from esphome.const import CONF_ID
from .. import teleinfo_ns, TeleInfo, CONF_TELEINFO_ID from .. import CONF_TAG_NAME, TELEINFO_LISTENER_SCHEMA, teleinfo_ns, CONF_TELEINFO_ID
CONF_TAG_NAME = "tag_name"
TeleInfoTextSensor = teleinfo_ns.class_( TeleInfoTextSensor = teleinfo_ns.class_(
"TeleInfoTextSensor", text_sensor.TextSensor, cg.Component "TeleInfoTextSensor", text_sensor.TextSensor, cg.Component
) )
CONFIG_SCHEMA = text_sensor.TEXT_SENSOR_SCHEMA.extend( CONFIG_SCHEMA = text_sensor.text_sensor_schema(klass=TeleInfoTextSensor).extend(
{ TELEINFO_LISTENER_SCHEMA
cv.GenerateID(): cv.declare_id(TeleInfoTextSensor),
cv.GenerateID(CONF_TELEINFO_ID): cv.use_id(TeleInfo),
cv.Required(CONF_TAG_NAME): cv.string,
}
) )

View file

@ -10,18 +10,21 @@ TemplateTextSensor = template_ns.class_(
"TemplateTextSensor", text_sensor.TextSensor, cg.PollingComponent "TemplateTextSensor", text_sensor.TextSensor, cg.PollingComponent
) )
CONFIG_SCHEMA = text_sensor.TEXT_SENSOR_SCHEMA.extend( CONFIG_SCHEMA = (
{ text_sensor.text_sensor_schema()
cv.GenerateID(): cv.declare_id(TemplateTextSensor), .extend(
cv.Optional(CONF_LAMBDA): cv.returning_lambda, {
} cv.GenerateID(): cv.declare_id(TemplateTextSensor),
).extend(cv.polling_component_schema("60s")) cv.Optional(CONF_LAMBDA): cv.returning_lambda,
}
)
.extend(cv.polling_component_schema("60s"))
)
async def to_code(config): 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 cg.register_component(var, config)
await text_sensor.register_text_sensor(var, config)
if CONF_LAMBDA in config: if CONF_LAMBDA in config:
template_ = await cg.process_lambda( template_ = await cg.process_lambda(

View file

@ -3,7 +3,9 @@ import esphome.config_validation as cv
from esphome import automation from esphome import automation
from esphome.components import mqtt from esphome.components import mqtt
from esphome.const import ( from esphome.const import (
CONF_ENTITY_CATEGORY,
CONF_FILTERS, CONF_FILTERS,
CONF_ICON,
CONF_ID, CONF_ID,
CONF_ON_VALUE, CONF_ON_VALUE,
CONF_ON_RAW_VALUE, CONF_ON_RAW_VALUE,
@ -14,6 +16,7 @@ from esphome.const import (
CONF_TO, CONF_TO,
) )
from esphome.core import CORE, coroutine_with_priority from esphome.core import CORE, coroutine_with_priority
from esphome.cpp_generator import MockObjClass
from esphome.cpp_helpers import setup_entity from esphome.cpp_helpers import setup_entity
from esphome.util import Registry from esphome.util import Registry
@ -110,12 +113,10 @@ async def map_filter_to_code(config, filter_id):
) )
icon = cv.icon
TEXT_SENSOR_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMPONENT_SCHEMA).extend( TEXT_SENSOR_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMPONENT_SCHEMA).extend(
{ {
cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTTextSensor), cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTTextSensor),
cv.GenerateID(): cv.declare_id(TextSensor),
cv.Optional(CONF_FILTERS): validate_filters, cv.Optional(CONF_FILTERS): validate_filters,
cv.Optional(CONF_ON_VALUE): automation.validate_automation( cv.Optional(CONF_ON_VALUE): automation.validate_automation(
{ {
@ -132,6 +133,29 @@ TEXT_SENSOR_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMPONENT_SCHEMA).exte
} }
) )
_UNDEF = object()
def text_sensor_schema(
klass: MockObjClass = _UNDEF,
icon: str = _UNDEF,
entity_category: str = _UNDEF,
) -> cv.Schema:
schema = TEXT_SENSOR_SCHEMA
if klass is not _UNDEF:
schema = schema.extend({cv.GenerateID(): cv.declare_id(klass)})
if icon is not _UNDEF:
schema = schema.extend({cv.Optional(CONF_ICON, default=icon): cv.icon})
if entity_category is not _UNDEF:
schema = schema.extend(
{
cv.Optional(
CONF_ENTITY_CATEGORY, default=entity_category
): cv.entity_category
}
)
return schema
async def build_filters(config): async def build_filters(config):
return await cg.build_registry_list(FILTER_REGISTRY, config) return await cg.build_registry_list(FILTER_REGISTRY, config)
@ -164,6 +188,12 @@ async def register_text_sensor(var, config):
await setup_text_sensor_core_(var, config) await setup_text_sensor_core_(var, config)
async def new_text_sensor(config):
var = cg.new_Pvariable(config[CONF_ID])
await register_text_sensor(var, config)
return var
@coroutine_with_priority(100.0) @coroutine_with_priority(100.0)
async def to_code(config): async def to_code(config):
cg.add_define("USE_TEXT_SENSOR") cg.add_define("USE_TEXT_SENSOR")

View file

@ -1,7 +1,7 @@
from esphome.components import text_sensor from esphome.components import text_sensor
import esphome.config_validation as cv import esphome.config_validation as cv
import esphome.codegen as cg import esphome.codegen as cg
from esphome.const import CONF_ID, CONF_SENSOR_DATAPOINT from esphome.const import CONF_SENSOR_DATAPOINT
from .. import tuya_ns, CONF_TUYA_ID, Tuya from .. import tuya_ns, CONF_TUYA_ID, Tuya
DEPENDENCIES = ["tuya"] DEPENDENCIES = ["tuya"]
@ -9,19 +9,22 @@ CODEOWNERS = ["@dentra"]
TuyaTextSensor = tuya_ns.class_("TuyaTextSensor", text_sensor.TextSensor, cg.Component) TuyaTextSensor = tuya_ns.class_("TuyaTextSensor", text_sensor.TextSensor, cg.Component)
CONFIG_SCHEMA = text_sensor.TEXT_SENSOR_SCHEMA.extend( CONFIG_SCHEMA = (
{ text_sensor.text_sensor_schema()
cv.GenerateID(): cv.declare_id(TuyaTextSensor), .extend(
cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya), {
cv.Required(CONF_SENSOR_DATAPOINT): cv.uint8_t, cv.GenerateID(): cv.declare_id(TuyaTextSensor),
} cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya),
).extend(cv.COMPONENT_SCHEMA) cv.Required(CONF_SENSOR_DATAPOINT): cv.uint8_t,
}
)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config): 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 cg.register_component(var, config)
await text_sensor.register_text_sensor(var, config)
paren = await cg.get_variable(config[CONF_TUYA_ID]) paren = await cg.get_variable(config[CONF_TUYA_ID])
cg.add(var.set_tuya_parent(paren)) cg.add(var.set_tuya_parent(paren))

View file

@ -2,9 +2,6 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import text_sensor from esphome.components import text_sensor
from esphome.const import ( from esphome.const import (
CONF_ENTITY_CATEGORY,
CONF_ID,
CONF_ICON,
ENTITY_CATEGORY_DIAGNOSTIC, ENTITY_CATEGORY_DIAGNOSTIC,
ICON_NEW_BOX, ICON_NEW_BOX,
CONF_HIDE_TIMESTAMP, CONF_HIDE_TIMESTAMP,
@ -15,20 +12,22 @@ VersionTextSensor = version_ns.class_(
"VersionTextSensor", text_sensor.TextSensor, cg.Component "VersionTextSensor", text_sensor.TextSensor, cg.Component
) )
CONFIG_SCHEMA = text_sensor.TEXT_SENSOR_SCHEMA.extend( CONFIG_SCHEMA = (
{ text_sensor.text_sensor_schema(
cv.GenerateID(): cv.declare_id(VersionTextSensor), icon=ICON_NEW_BOX,
cv.Optional(CONF_ICON, default=ICON_NEW_BOX): text_sensor.icon, entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
cv.Optional(CONF_HIDE_TIMESTAMP, default=False): cv.boolean, )
cv.Optional( .extend(
CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_DIAGNOSTIC {
): cv.entity_category, cv.GenerateID(): cv.declare_id(VersionTextSensor),
} cv.Optional(CONF_HIDE_TIMESTAMP, default=False): cv.boolean,
).extend(cv.COMPONENT_SCHEMA) }
)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = await text_sensor.new_text_sensor(config)
await text_sensor.register_text_sensor(var, config)
await cg.register_component(var, config) await cg.register_component(var, config)
cg.add(var.set_hide_timestamp(config[CONF_HIDE_TIMESTAMP])) cg.add(var.set_hide_timestamp(config[CONF_HIDE_TIMESTAMP]))

View file

@ -3,8 +3,6 @@ import esphome.config_validation as cv
from esphome.components import text_sensor from esphome.components import text_sensor
from esphome.const import ( from esphome.const import (
CONF_BSSID, CONF_BSSID,
CONF_ENTITY_CATEGORY,
CONF_ID,
CONF_IP_ADDRESS, CONF_IP_ADDRESS,
CONF_SCAN_RESULTS, CONF_SCAN_RESULTS,
CONF_SSID, CONF_SSID,
@ -31,45 +29,20 @@ MacAddressWifiInfo = wifi_info_ns.class_(
CONFIG_SCHEMA = cv.Schema( CONFIG_SCHEMA = cv.Schema(
{ {
cv.Optional(CONF_IP_ADDRESS): text_sensor.TEXT_SENSOR_SCHEMA.extend( cv.Optional(CONF_IP_ADDRESS): text_sensor.text_sensor_schema(
{ klass=IPAddressWiFiInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC
cv.GenerateID(): cv.declare_id(IPAddressWiFiInfo),
cv.Optional(
CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_DIAGNOSTIC
): cv.entity_category,
}
), ),
cv.Optional(CONF_SCAN_RESULTS): text_sensor.TEXT_SENSOR_SCHEMA.extend( cv.Optional(CONF_SCAN_RESULTS): text_sensor.text_sensor_schema(
{ klass=ScanResultsWiFiInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC
cv.GenerateID(): cv.declare_id(ScanResultsWiFiInfo),
cv.Optional(
CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_DIAGNOSTIC
): cv.entity_category,
}
).extend(cv.polling_component_schema("60s")), ).extend(cv.polling_component_schema("60s")),
cv.Optional(CONF_SSID): text_sensor.TEXT_SENSOR_SCHEMA.extend( cv.Optional(CONF_SSID): text_sensor.text_sensor_schema(
{ klass=SSIDWiFiInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC
cv.GenerateID(): cv.declare_id(SSIDWiFiInfo),
cv.Optional(
CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_DIAGNOSTIC
): cv.entity_category,
}
), ),
cv.Optional(CONF_BSSID): text_sensor.TEXT_SENSOR_SCHEMA.extend( cv.Optional(CONF_BSSID): text_sensor.text_sensor_schema(
{ klass=BSSIDWiFiInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC
cv.GenerateID(): cv.declare_id(BSSIDWiFiInfo),
cv.Optional(
CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_DIAGNOSTIC
): cv.entity_category,
}
), ),
cv.Optional(CONF_MAC_ADDRESS): text_sensor.TEXT_SENSOR_SCHEMA.extend( cv.Optional(CONF_MAC_ADDRESS): text_sensor.text_sensor_schema(
{ klass=MacAddressWifiInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC
cv.GenerateID(): cv.declare_id(MacAddressWifiInfo),
cv.Optional(
CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_DIAGNOSTIC
): cv.entity_category,
}
), ),
} }
) )
@ -78,9 +51,8 @@ CONFIG_SCHEMA = cv.Schema(
async def setup_conf(config, key): async def setup_conf(config, key):
if key in config: if key in config:
conf = config[key] conf = config[key]
var = cg.new_Pvariable(conf[CONF_ID]) var = await text_sensor.new_text_sensor(conf)
await cg.register_component(var, conf) await cg.register_component(var, conf)
await text_sensor.register_text_sensor(var, conf)
async def to_code(config): async def to_code(config):