diff --git a/marlin2.cpp b/marlin2.cpp index e072d0f..9b88d2b 100644 --- a/marlin2.cpp +++ b/marlin2.cpp @@ -17,6 +17,19 @@ namespace esphome { return nullptr; // Return nullptr if no match is found } + // void Marlin2::add_text_sensor(const std::string& sName, text_sensor *sens) { + // text_sensors.push_back({sName, sens}); + // } + + // text_sensor* 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 + // } + void Marlin2::setup() { MarlinOutput.reserve(256); MarlinOutput = ""; @@ -87,18 +100,19 @@ namespace esphome { ESP_LOGD(TAG, "Bed Temperature=%.1f°C Ext Temperature=%.1f°C ", bed_temperature, ext_temperature); } - 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_sensor("printer_status") != nullptr) - find_sensor("printer_status")->publish_state("IDLE"); - else if(ext_temperature < 150.0 && bed_temperature < 55.0) - if (find_sensor("printer_status") != nullptr) - find_sensor("printer_status")->publish_state("COOLING"); - } - if(bed_set_temperature!=0.0 || ext_set_temperature!=0.0) { - if (find_sensor("printer_status") != nullptr) - find_sensor("printer_status")->publish_state("PREHEATING"); - } + // 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(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"); + // } + //reset string for next line MarlinOutput=""; @@ -121,17 +135,17 @@ namespace esphome { //Parse Printitme if(MarlinOutput.find("echo:Print time: ") == 0) { - int d=0, h=0, m=0, s=0; + int* d=0, h=0, m=0, s=0; unsigned long current=0, remaining=0; - if (process_print_time_msg(&d, &h, &m, ¤t, &remaining) != 0) { + if (process_print_time_msg(&d, &h, &m, &s, ¤t, &remaining) != 0) { if (find_sensor("print_time") != nullptr) - find_sensor("print_time")->publish_state(print_time) + find_sensor("print_time")->publish_state(current); if (find_sensor("print_time_remaining") != nullptr) - find_sensor("print_time_remaining")->publish_state(print_time_remaining) + find_sensor("print_time_remaining")->publish_state(remaining); - ESP_LOGD(TAG, "time=%.1f remaining=%.1f", current, remaining); + ESP_LOGD(TAG, "time=%lu remaining=%lu", current, remaining); } //reset string for next line @@ -141,8 +155,8 @@ namespace esphome { //Print Finished if(MarlinOutput.find("Done printing") != std::string::npos) { - if (find_sensor("printer_status") != nullptr) - find_sensor("printer_status")->publish_state("FINISHED"); + //if (find_text_sensor("printer_status") != nullptr) + //find_text_sensor("printer_status")->publish_state("FINISHED"); ESP_LOGD(TAG, "Print Finished"); @@ -153,8 +167,8 @@ namespace esphome { //Print Paused if(MarlinOutput.find("Printer halted") != std::string::npos) { - if (find_sensor("printer_status") != nullptr) - find_sensor("printer_status")->publish_state("PAUSED"); + //if (find_text_sensor("printer_status") != nullptr) + //find_text_sensor("printer_status")->publish_state("PAUSED"); ESP_LOGD(TAG, "Print Finished"); @@ -165,8 +179,8 @@ namespace esphome { //Print Stoped if(MarlinOutput.find("Print Aborted") != std::string::npos) { - if (find_sensor("printer_status") != nullptr) - find_sensor("printer_status")->publish_state("ABOARTED"); + //if (find_text_sensor("printer_status") != nullptr) + //find_text_sensor("printer_status")->publish_state("ABOARTED"); ESP_LOGD(TAG, "Print Finished"); @@ -215,7 +229,7 @@ namespace esphome { return ((float) current / (float) total) * 100.0; } - int Marlin2::process_print_time_msg(int* d, int* h, int* m, unsigned long* current, unsigned long* remaining){ + int Marlin2::process_print_time_msg(int d, int h, int m, int s, unsigned long* current, unsigned long* remaining){ MarlinTime = MarlinOutput.substr(16); ESP_LOGD(TAG,MarlinTime.c_str()); @@ -234,7 +248,7 @@ namespace esphome { } } - current = d*24*60*60 + h*60*60 + m*60 + s; + current = static_cast((d*24*60*60) + (h*60*60) + (m*60) + (s)); return 0; diff --git a/marlin2.h b/marlin2.h index a4a9c77..d8040c6 100644 --- a/marlin2.h +++ b/marlin2.h @@ -2,29 +2,34 @@ #include "esphome/core/component.h" #include "esphome/components/sensor/sensor.h" +//#include "esphome/components/text_sensor/text_sensor.h" #include "esphome/components/uart/uart.h" namespace esphome { -class Marlin2 : public PollingComponent , public uart::UARTDevice { +class Marlin2 : public PollingComponent, /*public text_sensor::TextSensor,*/ public uart::UARTDevice { public: void setup() override; float get_setup_priority() const override { return setup_priority::LATE; } void update() override; void add_sensor(const std::string& sName, sensor::Sensor *sens); + //void add_text_sensor(const std::string& sName, text_sensor::TextSensor *sens); + sensor::Sensor* find_sensor(std::string key); + // text_sensor::TextSensor* find_text_sensor(std::string key); protected: void process_line(); int process_temp_msg(float* ext_temperature, float* ext_set_temperature, float* bed_temperature, float* bed_set_temperature); float process_progress_msg(); - int process_print_time_msg(int* d, int* h, int* m, unsigned long* current, unsigned long* remaining); + int process_print_time_msg(int d, int h, int m, int s, unsigned long* current, unsigned long* remaining); std::string MarlinOutput; std::string MarlinTime; std::string PrinterState; std::vector> sensors; + // std::vector> text_sensors; private: unsigned long millisProgress=0; diff --git a/sensor.py b/sensor.py index 1bdaf77..2188d10 100644 --- a/sensor.py +++ b/sensor.py @@ -3,6 +3,7 @@ import esphome.config_validation as cv from esphome import automation from esphome.components import uart from esphome.components import sensor +# from esphome.components import text_sensor from esphome.const import ( CONF_ID, CONF_INDEX, @@ -33,7 +34,7 @@ CONF_PRINT_PROGRESS = "print_progress" CONF_PRINT_TIME = "print_time" CONF_PRINT_TIME_REMAINING = "print_time_remaining" -CONF_PRINTER_STATUS = "printer_status" +# CONF_PRINTER_STATUS = "printer_status" Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component, sensor.Sensor, uart.UARTDevice) @@ -81,9 +82,7 @@ CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend( device_class=DEVICE_CLASS_DURATION, state_class=STATE_CLASS_MEASUREMENT, ), - cv.Optional(CONF_PRINTER_STATUS): sensor.sensor_schema( - state_class=STATE_CLASS_MEASUREMENT, - ), + # cv.Optional(CONF_PRINTER_STATUS): text_sensor.text_sensor_schema(), } ).extend(cv.polling_component_schema("15s")) @@ -96,7 +95,7 @@ async def to_code(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_textsensor(sName,sens)) \ No newline at end of file + # 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