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

Nextion upload and sensors (#1464)

Co-authored-by: Senex Crenshaw <senexcrenshaw@gmail.com>
This commit is contained in:
SenexCrenshaw
2021-07-14 20:51:15 -04:00
committed by GitHub
parent 0992609bf4
commit 0651716b96
30 changed files with 3295 additions and 263 deletions

View File

@@ -0,0 +1,38 @@
from esphome.components import text_sensor
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import CONF_ID
from .. import nextion_ns, CONF_NEXTION_ID
from ..base_component import (
setup_component_core_,
CONFIG_TEXT_COMPONENT_SCHEMA,
)
CODEOWNERS = ["@senexcrenshaw"]
NextionTextSensor = nextion_ns.class_(
"NextionTextSensor", text_sensor.TextSensor, cg.PollingComponent
)
CONFIG_SCHEMA = (
text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(NextionTextSensor),
}
)
.extend(CONFIG_TEXT_COMPONENT_SCHEMA)
.extend(cv.polling_component_schema("never"))
)
async def to_code(config):
hub = await cg.get_variable(config[CONF_NEXTION_ID])
var = cg.new_Pvariable(config[CONF_ID], hub)
await cg.register_component(var, config)
await text_sensor.register_text_sensor(var, config)
cg.add(hub.register_textsensor_component(var))
await setup_component_core_(var, config, ".txt")

View File

@@ -0,0 +1,49 @@
#include "nextion_textsensor.h"
#include "esphome/core/util.h"
#include "esphome/core/log.h"
namespace esphome {
namespace nextion {
static const char *const TAG = "nextion_textsensor";
void NextionTextSensor::process_text(const std::string &variable_name, const std::string &text_value) {
if (!this->nextion_->is_setup())
return;
if (this->variable_name_ == variable_name) {
this->publish_state(text_value);
ESP_LOGD(TAG, "Processed text_sensor \"%s\" state \"%s\"", variable_name.c_str(), text_value.c_str());
}
}
void NextionTextSensor::update() {
if (!this->nextion_->is_setup())
return;
this->nextion_->add_to_get_queue(this);
}
void NextionTextSensor::set_state(const std::string &state, bool publish, bool send_to_nextion) {
if (!this->nextion_->is_setup())
return;
if (send_to_nextion) {
if (this->nextion_->is_sleeping() || !this->visible_) {
this->needs_to_send_update_ = true;
} else {
this->nextion_->add_no_result_to_queue_with_set(this, state);
}
}
if (publish) {
this->publish_state(state);
} else {
this->state = state;
this->has_state_ = true;
}
this->update_component_settings();
ESP_LOGN(TAG, "Wrote state for text_sensor \"%s\" state \"%s\"", this->variable_name_.c_str(), state.c_str());
}
} // namespace nextion
} // namespace esphome

View File

@@ -0,0 +1,32 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/text_sensor/text_sensor.h"
#include "../nextion_component.h"
#include "../nextion_base.h"
namespace esphome {
namespace nextion {
class NextionTextSensor;
class NextionTextSensor : public NextionComponent, public text_sensor::TextSensor, public PollingComponent {
public:
NextionTextSensor(NextionBase *nextion) { this->nextion_ = nextion; }
void update() override;
void update_component() override { this->update(); }
void on_state_changed(const std::string &state);
void process_text(const std::string &variable_name, const std::string &text_value) override;
void set_state(const std::string &state, bool publish) override { this->set_state(state, publish, true); }
void set_state(const std::string &state) override { this->set_state(state, true, true); }
void set_state(const std::string &state, bool publish, bool send_to_nextion) override;
void send_state_to_nextion() override { this->set_state(this->state, false, true); };
NextionQueueType get_queue_type() override { return NextionQueueType::TEXT_SENSOR; }
void set_state_from_int(int state_value, bool publish, bool send_to_nextion) override {}
void set_state_from_string(const std::string &state_value, bool publish, bool send_to_nextion) override {
this->set_state(state_value, publish, send_to_nextion);
}
};
} // namespace nextion
} // namespace esphome