diff --git a/example.yaml b/example.yaml index f317036..f880918 100644 --- a/example.yaml +++ b/example.yaml @@ -31,3 +31,8 @@ sensor: name: "Projector signal status" illuminance: name: "Projector luminance level" + +text_sensor: + - platform: tw7100 + serial: + name: "Serial number" diff --git a/text_sensor.py b/text_sensor.py new file mode 100644 index 0000000..ec4122f --- /dev/null +++ b/text_sensor.py @@ -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)) diff --git a/tw7100.cpp b/tw7100.cpp index ddc4779..5465a02 100644 --- a/tw7100.cpp +++ b/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 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()); }