diff --git a/marlin2.cpp b/marlin2.cpp index 67e4f3d..34dc8ec 100644 --- a/marlin2.cpp +++ b/marlin2.cpp @@ -22,6 +22,7 @@ namespace esphome { MarlinOutput = ""; MarlinTime.reserve(32); + PrinterState.reserve(32); ESP_LOGD(TAG, "M155 S10"); @@ -86,6 +87,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'); + } + //reset string for next line MarlinOutput=""; return; @@ -125,6 +139,42 @@ namespace esphome { return; } + //Print Finished + if(MarlinOutput.find("Done printing") != std::string::npos) { + if (find_sensor("printer_status") != nullptr) + find_sensor("printer_status")->publish_state('FINISHED'); + + ESP_LOGD(TAG, "Print Finished"); + + //reset string for next line + MarlinOutput=""; + return; + } + + //Print Paused + if(MarlinOutput.find("Printer halted") != std::string::npos) { + if (find_sensor("printer_status") != nullptr) + find_sensor("printer_status")->publish_state('PAUSED'); + + ESP_LOGD(TAG, "Print Finished"); + + //reset string for next line + MarlinOutput=""; + return; + } + + //Print Stoped + if(MarlinOutput.find("Print Aborted") != std::string::npos) { + if (find_sensor("printer_status") != nullptr) + find_sensor("printer_status")->publish_state('ABOARTED'); + + ESP_LOGD(TAG, "Print Finished"); + + //reset string for next line + MarlinOutput=""; + return; + } + ESP_LOGD(TAG, "#%s#",MarlinOutput.c_str()); MarlinOutput=""; return; diff --git a/marlin2.h b/marlin2.h index 88f0547..a5e278d 100644 --- a/marlin2.h +++ b/marlin2.h @@ -21,6 +21,8 @@ class Marlin2 : public PollingComponent , public uart::UARTDevice { int process_print_time_msg(int* d, int* h, int* m, unsigned long* current, unsigned long* remaining); std::string MarlinOutput; + std::string PrinterState; + std::vector> sensors; private: diff --git a/sensor.py b/sensor.py index 748b067..41de588 100644 --- a/sensor.py +++ b/sensor.py @@ -11,9 +11,10 @@ from esphome.const import ( CONF_MODEL, CONF_PIN, CONF_TEMPERATURE, - STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, + UNIT_SECONDS, + STATE_CLASS_MEASUREMENT, DEVICE_CLASS_TEMPERATURE ) @@ -31,6 +32,8 @@ 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( @@ -66,17 +69,20 @@ CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend( state_class=STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PRINT_TIME): sensor.sensor_schema( - unit_of_measurement=UNIT_PERCENT, + unit_of_measurement=UNIT_SECONDS, accuracy_decimals=1, device_class=DEVICE_CLASS_DURATION, state_class=STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PRINT_TIME_REMAINING): sensor.sensor_schema( - unit_of_measurement=UNIT_PERCENT, + unit_of_measurement=UNIT_SECONDS, accuracy_decimals=1, device_class=DEVICE_CLASS_DURATION, state_class=STATE_CLASS_MEASUREMENT, ), + cv.Optional(CONF_PRINTER_STATUS): sensor.sensor_schema( + state_class=STATE_CLASS_MEASUREMENT, + ), } ).extend(cv.polling_component_schema("15s"))