Add serial text sensor
This commit is contained in:
parent
d1728318c4
commit
6877f64812
|
@ -31,3 +31,8 @@ sensor:
|
|||
name: "Projector signal status"
|
||||
illuminance:
|
||||
name: "Projector luminance level"
|
||||
|
||||
text_sensor:
|
||||
- platform: tw7100
|
||||
serial:
|
||||
name: "Serial number"
|
||||
|
|
29
text_sensor.py
Normal file
29
text_sensor.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import text_sensor
|
||||
from esphome.const import (
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
CONF_ID,
|
||||
)
|
||||
|
||||
from . import tw7100
|
||||
|
||||
DEPENDENCIES = ["tw7100"]
|
||||
CONF_SERIAL = "serial"
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(tw7100),
|
||||
cv.Optional(CONF_SERIAL): text_sensor.text_sensor_schema(
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
parent = await cg.get_variable(config[CONF_ID])
|
||||
|
||||
if CONF_SERIAL in config:
|
||||
sens = await text_sensor.new_text_sensor(config[CONF_SERIAL])
|
||||
cg.add(parent.set_serial(sens))
|
14
tw7100.cpp
14
tw7100.cpp
|
@ -101,9 +101,9 @@ void tw7100Component::update() {
|
|||
cmd_queue.push_back(cmd);
|
||||
}
|
||||
if ((powered_->state)) {
|
||||
//if (this->serial_->state.length() < 1) {
|
||||
// cmd_queue.push_back("SNO?");
|
||||
//}
|
||||
if (this->serial_->state.length() < 1) {
|
||||
cmd_queue.push_back("SNO?");
|
||||
}
|
||||
for (auto &cmd : pwr_on_query_cmds) {
|
||||
cmd_queue.push_back(cmd);
|
||||
}
|
||||
|
@ -178,9 +178,11 @@ void tw7100Component::update_sensor(std::pair<std::string, std::string> data) {
|
|||
int value = std::stoi(value_string);
|
||||
luminance_level_->publish_state(value);
|
||||
} else if (cmd == "SOURCELIST") {
|
||||
for(char& c : value_string) {
|
||||
ESP_LOGV(TAG, "%c (%02x)", c, c);
|
||||
}
|
||||
//for(char& c : value_string) {
|
||||
// ESP_LOGV(TAG, "%c (%02x)", c, c);
|
||||
//}
|
||||
} else if (cmd == "SNO") {
|
||||
serial_->publish_state(value_string);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Command %s unknown, skipping updating sensor with value", cmd.c_str());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue