From 64183c33d2144b40285b4bb2b441e37fac085f51 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Mon, 30 Dec 2024 10:39:19 +0100 Subject: [PATCH] Fixes --- __init__.py | 3 +- marlin2.cpp | 77 ++++++++++++++++++++++++++++++++------------------ marlin2.h | 7 ++--- sensor.py | 25 ++++++---------- text_sensor.py | 13 +++++---- 5 files changed, 68 insertions(+), 57 deletions(-) diff --git a/__init__.py b/__init__.py index 205b8a8..84a4e2e 100644 --- a/__init__.py +++ b/__init__.py @@ -19,11 +19,12 @@ from esphome.const import ( CODEOWNERS = ["@jonatanrek"] DEPENDENCIES = ['uart'] +CONF_MARLIN2_ID = "marlin2_id" Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component) CONFIG_SCHEMA = cv.All( - cv.Schema( { + cv.Schema({ cv.GenerateID(): cv.declare_id(Marlin2), }) .extend(cv.COMPONENT_SCHEMA) diff --git a/marlin2.cpp b/marlin2.cpp index 03d3b79..1826d24 100644 --- a/marlin2.cpp +++ b/marlin2.cpp @@ -4,18 +4,35 @@ namespace esphome { static const char *TAG = "marlin2"; - void Marlin2::add_sensor(const std::string& sName, sensor::Sensor *sens) { - sensors.push_back({sName, sens}); - } - - sensor::Sensor* Marlin2::find_sensor(std::string key) { - for (const auto& pair : sensors) { - if (key == std::string(pair.first)) { // Convert char* to std::string for comparison - return pair.second; - } + #ifdef USE_SENSOR + void Marlin2::add_sensor(const std::string& sName, sensor::Sensor *sens) { + sensors.push_back({sName, sens}); } - return nullptr; // Return nullptr if no match is found - } + + sensor::Sensor* Marlin2::find_sensor(std::string key) { + for (const auto& pair : sensors) { + if (key == std::string(pair.first)) { // Convert char* to std::string for comparison + return pair.second; + } + } + return nullptr; // Return nullptr if no match is found + } + #endif + #ifdef USE_TEXT_SENSOR + void Marlin2::add_text_sensor(const std::string& sName, text_sensor::TextSensor *sens) { + text_sensors.push_back({sName, sens}); + } + + text_sensor::TextSensor* Marlin2::find_text_sensor(std::string key) { + for (const auto& pair : text_sensors) { + if (key == std::string(pair.first)) { // Convert char* to std::string for comparison + return pair.second; + } + } + return nullptr; // Return nullptr if no match is found + } + #endif + void Marlin2::setup() { MarlinOutput.reserve(256); @@ -26,14 +43,14 @@ namespace esphome { ESP_LOGD(TAG, "M155 S10"); - this->uart_device_->write_str("\r\n\r\nM155 S10\r\n"); - this->uart_device_->write_str("\r\n\r\nM117 Hello World!\r\n"); - this->uart_device_->flush(); + write_str("\r\n\r\nM155 S10\r\n"); + write_str("\r\n\r\nM117 Hello World!\r\n"); + flush(); } void Marlin2::update() { - while (this->uart_device_->available()) { - char c = this->uart_device_->read(); + while (available()) { + char c = read(); if( c == '\n' || c == '\r' ) { process_line(); } else { @@ -47,7 +64,7 @@ namespace esphome { ESP_LOGD(TAG, "M27"); ESP_LOGD(TAG, "M31"); - this->uart_device_->write_str("M27\r\nM31\r\n"); + write_str("M27\r\nM31\r\n"); } } @@ -86,16 +103,18 @@ namespace esphome { #endif #ifdef USE_TEXT_SENSOR if(bed_set_temperature==0.0 && ext_set_temperature==0.0) { - if(ext_temperature < 32.0 && bed_temperature < 32.0) //TODO define constants for these - //if (find_text_sensor("printer_status") != nullptr) - //find_text_sensor("printer_status")->publish_state("IDLE"); - else if(ext_temperature < 150.0 && bed_temperature < 55.0) - //if (find_text_sensor("printer_status") != nullptr) - //find_text_sensor("printer_status")->publish_state("COOLING"); + if(ext_temperature < 32.0 && bed_temperature < 32.0){ //TODO define constants for these + if (find_text_sensor("printer_status") != nullptr) + find_text_sensor("printer_status")->publish_state("IDLE"); + } + else if(ext_temperature < 150.0 && bed_temperature < 55.0){ + if (find_text_sensor("printer_status") != nullptr) + find_text_sensor("printer_status")->publish_state("COOLING"); + } } if(bed_set_temperature!=0.0 || ext_set_temperature!=0.0) { - //if (find_text_sensor("printer_status") != nullptr) - //find_text_sensor("printer_status")->publish_state("PREHEATING"); + if (find_text_sensor("printer_status") != nullptr) + find_text_sensor("printer_status")->publish_state("PREHEATING"); } #endif @@ -144,9 +163,11 @@ namespace esphome { } //Print Finished - if(MarlinOutput.find("Done printing") != std::string::npos) { - // //if (find_text_sensor("printer_status") != nullptr) - // //find_text_sensor("printer_status")->publish_state("FINISHED"); + if(MarlinOutput.find("Done printing") != std::string::npos) { + #ifdef USE_TEXT_SENSOR + if (find_text_sensor("printer_status") != nullptr) + find_text_sensor("printer_status")->publish_state("FINISHED"); + #endif ESP_LOGD(TAG, "Print Finished"); print_progress = 100; diff --git a/marlin2.h b/marlin2.h index f44d07d..b722f21 100644 --- a/marlin2.h +++ b/marlin2.h @@ -11,11 +11,10 @@ namespace esphome { -class Marlin2 : public esphome::Component, public uart::UARTDevice { +class Marlin2 : public PollingComponent, public uart::UARTDevice { public: Marlin2() = default; - explicit Marlin2(uart::UARTDevice *uart_device) : uart_device_{uart_device} {} - void set_uart_parent(uart::UARTDevice *parent) { this->uart_device_ = parent; } + #ifdef USE_SENSOR void add_sensor(const std::string& sName, sensor::Sensor *sens); @@ -31,8 +30,6 @@ class Marlin2 : public esphome::Component, public uart::UARTDevice { void update() override; protected: - esphome::uart::UARTDevice *uart_device_{nullptr}; - std::string MarlinOutput; std::string MarlinTime; std::string PrinterState; diff --git a/sensor.py b/sensor.py index 102d091..4dcf265 100644 --- a/sensor.py +++ b/sensor.py @@ -19,6 +19,7 @@ from esphome.const import ( DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_DURATION, ) +from . import Marlin2 CONF_BED_TEMPERATURE = "bed_temperature" CONF_BED_SET_TEMPERATURE = "bed_set_temperature" @@ -30,13 +31,10 @@ CONF_PRINT_PROGRESS = "print_progress" CONF_PRINT_TIME = "print_time" CONF_PRINT_TIME_REMAINING = "print_time_remaining" -# CONF_PRINTER_STATUS = "printer_status" - -Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component, sensor.Sensor, uart.UARTDevice) - -CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend( +CONF_MARLIN = "marlin2" +CONFIG_SCHEMA = cv.Schema( { - cv.GenerateID(): cv.declare_id(Marlin2), + cv.GenerateID(CONF_MARLIN): cv.use_id(Marlin2), cv.Optional(CONF_BED_TEMPERATURE): sensor.sensor_schema( unit_of_measurement=UNIT_CELSIUS, accuracy_decimals=1, @@ -78,20 +76,13 @@ CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend( device_class=DEVICE_CLASS_DURATION, state_class=STATE_CLASS_MEASUREMENT, ), - # cv.Optional(CONF_PRINTER_STATUS): text_sensor.text_sensor_schema(), } -) +).extend(cv.polling_component_schema("15s")) async def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) - await cg.register_component(var, config) - await uart.register_uart_device(var, config) + server = await cg.get_variable(config[CONF_MARLIN]) + for sName in [CONF_BED_TEMPERATURE, CONF_BED_SET_TEMPERATURE, CONF_EXT_TEMPERATURE, CONF_EXT_SET_TEMPERATURE, CONF_PRINT_PROGRESS, CONF_PRINT_TIME, CONF_PRINT_TIME_REMAINING]: if sName in config: sens = await sensor.new_sensor(config[sName]) - cg.add(var.add_sensor(sName,sens)) - - # for sName in [CONF_PRINTER_STATUS]: - # if sName in config: - # sens = await sensor.new_text_sensor(config[sName]) - # cg.add(var.add_text_sensor(sName,sens)) \ No newline at end of file + cg.add(server.add_sensor(sName,sens)) \ No newline at end of file diff --git a/text_sensor.py b/text_sensor.py index b2dc41b..5dacbd6 100644 --- a/text_sensor.py +++ b/text_sensor.py @@ -4,21 +4,22 @@ from esphome.components import text_sensor from esphome.const import ( ENTITY_CATEGORY_DIAGNOSTIC, ) -from . import ns, Marlin2 +from . import Marlin2 CONF_MARLIN = "marlin2" -CONFIG_SCHEMA = cv.Schema( +CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(CONF_MARLIN): cv.use_id(Marlin2), - cv.Optional(CONF_CONNECTED): text_sensor.text_sensor( + cv.Optional("printer_state"): text_sensor.text_sensor_schema( entity_category=ENTITY_CATEGORY_DIAGNOSTIC, ), } -) +).extend(cv.polling_component_schema("15s")) async def to_code(config): server = await cg.get_variable(config[CONF_MARLIN]) + for sName in ["printer_state"]: if sName in config: - sens = await sensor.new_text_sensor(config[sName]) - cg.add(var.add_text_sensor(sName,sens)) \ No newline at end of file + sens = await text_sensor.new_text_sensor(config[sName]) + cg.add(server.add_text_sensor(sName,sens)) \ No newline at end of file