mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-26 07:38:30 +02:00
Nextion upload and sensors (#1464)
Co-authored-by: Senex Crenshaw <senexcrenshaw@gmail.com>
This commit is contained in:
committed by
Jesse Hills
parent
f30b8f6b3c
commit
4dbf1c521e
@@ -0,0 +1,54 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import binary_sensor
|
||||
|
||||
from esphome.const import CONF_COMPONENT_ID, CONF_PAGE_ID, CONF_ID
|
||||
from .. import nextion_ns, CONF_NEXTION_ID
|
||||
|
||||
|
||||
from ..base_component import (
|
||||
setup_component_core_,
|
||||
CONFIG_BINARY_SENSOR_SCHEMA,
|
||||
CONF_VARIABLE_NAME,
|
||||
CONF_COMPONENT_NAME,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@senexcrenshaw"]
|
||||
|
||||
NextionBinarySensor = nextion_ns.class_(
|
||||
"NextionBinarySensor", binary_sensor.BinarySensor, cg.PollingComponent
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
binary_sensor.BINARY_SENSOR_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(NextionBinarySensor),
|
||||
cv.Optional(CONF_PAGE_ID): cv.uint8_t,
|
||||
cv.Optional(CONF_COMPONENT_ID): cv.uint8_t,
|
||||
}
|
||||
)
|
||||
.extend(CONFIG_BINARY_SENSOR_SCHEMA)
|
||||
.extend(cv.polling_component_schema("never")),
|
||||
cv.has_at_least_one_key(
|
||||
CONF_PAGE_ID,
|
||||
CONF_COMPONENT_ID,
|
||||
CONF_COMPONENT_NAME,
|
||||
CONF_VARIABLE_NAME,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
hub = await cg.get_variable(config[CONF_NEXTION_ID])
|
||||
var = cg.new_Pvariable(config[CONF_ID], hub)
|
||||
await binary_sensor.register_binary_sensor(var, config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if config.keys() >= {CONF_PAGE_ID, CONF_COMPONENT_ID}:
|
||||
cg.add(hub.register_touch_component(var))
|
||||
cg.add(var.set_component_id(config[CONF_COMPONENT_ID]))
|
||||
cg.add(var.set_page_id(config[CONF_PAGE_ID]))
|
||||
|
||||
if CONF_COMPONENT_NAME in config or CONF_VARIABLE_NAME in config:
|
||||
await setup_component_core_(var, config, ".val")
|
||||
cg.add(hub.register_binarysensor_component(var))
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "nextion_binarysensor.h"
|
||||
#include "esphome/core/util.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace nextion {
|
||||
|
||||
static const char *const TAG = "nextion_binarysensor";
|
||||
|
||||
void NextionBinarySensor::process_bool(const std::string &variable_name, bool state) {
|
||||
if (!this->nextion_->is_setup())
|
||||
return;
|
||||
|
||||
if (this->variable_name_.empty()) // This is a touch component
|
||||
return;
|
||||
|
||||
if (this->variable_name_ == variable_name) {
|
||||
this->publish_state(state);
|
||||
ESP_LOGD(TAG, "Processed binarysensor \"%s\" state %s", variable_name.c_str(), state ? "ON" : "OFF");
|
||||
}
|
||||
}
|
||||
|
||||
void NextionBinarySensor::process_touch(uint8_t page_id, uint8_t component_id, bool state) {
|
||||
if (this->page_id_ == page_id && this->component_id_ == component_id) {
|
||||
this->publish_state(state);
|
||||
}
|
||||
}
|
||||
|
||||
void NextionBinarySensor::update() {
|
||||
if (!this->nextion_->is_setup())
|
||||
return;
|
||||
|
||||
if (this->variable_name_.empty()) // This is a touch component
|
||||
return;
|
||||
|
||||
this->nextion_->add_to_get_queue(this);
|
||||
}
|
||||
|
||||
void NextionBinarySensor::set_state(bool state, bool publish, bool send_to_nextion) {
|
||||
if (!this->nextion_->is_setup())
|
||||
return;
|
||||
|
||||
if (this->component_id_ == 0) // This is a legacy touch component
|
||||
return;
|
||||
|
||||
if (send_to_nextion) {
|
||||
if (this->nextion_->is_sleeping() || !this->visible_) {
|
||||
this->needs_to_send_update_ = true;
|
||||
} else {
|
||||
this->needs_to_send_update_ = false;
|
||||
this->nextion_->add_no_result_to_queue_with_set(this, (int) 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 sensor \"%s\" state %s", this->variable_name_.c_str(),
|
||||
ONOFF(this->variable_name_.c_str()));
|
||||
}
|
||||
|
||||
} // namespace nextion
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#include "../nextion_component.h"
|
||||
#include "../nextion_base.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace nextion {
|
||||
class NextionBinarySensor;
|
||||
|
||||
class NextionBinarySensor : public NextionComponent,
|
||||
public binary_sensor::BinarySensorInitiallyOff,
|
||||
public PollingComponent {
|
||||
public:
|
||||
NextionBinarySensor(NextionBase *nextion) { this->nextion_ = nextion; }
|
||||
|
||||
void update_component() override { this->update(); }
|
||||
void update() override;
|
||||
void send_state_to_nextion() override { this->set_state(this->state, false); };
|
||||
void process_bool(const std::string &variable_name, bool state) override;
|
||||
void process_touch(uint8_t page_id, uint8_t component_id, bool state) override;
|
||||
|
||||
// Set the components page id for Nextion Touch Component
|
||||
void set_page_id(uint8_t page_id) { page_id_ = page_id; }
|
||||
// Set the components component id for Nextion Touch Component
|
||||
void set_component_id(uint8_t component_id) { component_id_ = component_id; }
|
||||
|
||||
void set_state(bool state) override { this->set_state(state, true, true); }
|
||||
void set_state(bool state, bool publish) override { this->set_state(state, publish, true); }
|
||||
void set_state(bool state, bool publish, bool send_to_nextion) override;
|
||||
|
||||
NextionQueueType get_queue_type() override { return NextionQueueType::BINARY_SENSOR; }
|
||||
void set_state_from_string(const std::string &state_value, bool publish, bool send_to_nextion) override {}
|
||||
void set_state_from_int(int state_value, bool publish, bool send_to_nextion) override {
|
||||
this->set_state(state_value != 0, publish, send_to_nextion);
|
||||
}
|
||||
|
||||
protected:
|
||||
uint8_t page_id_;
|
||||
};
|
||||
} // namespace nextion
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user