This commit is contained in:
Václav Španinger 2024-12-30 10:39:19 +01:00
parent 5112a91bec
commit 64183c33d2
5 changed files with 68 additions and 57 deletions

View File

@ -19,11 +19,12 @@ from esphome.const import (
CODEOWNERS = ["@jonatanrek"] CODEOWNERS = ["@jonatanrek"]
DEPENDENCIES = ['uart'] DEPENDENCIES = ['uart']
CONF_MARLIN2_ID = "marlin2_id"
Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component) Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component)
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
cv.Schema( { cv.Schema({
cv.GenerateID(): cv.declare_id(Marlin2), cv.GenerateID(): cv.declare_id(Marlin2),
}) })
.extend(cv.COMPONENT_SCHEMA) .extend(cv.COMPONENT_SCHEMA)

View File

@ -4,18 +4,35 @@
namespace esphome { namespace esphome {
static const char *TAG = "marlin2"; static const char *TAG = "marlin2";
void Marlin2::add_sensor(const std::string& sName, sensor::Sensor *sens) { #ifdef USE_SENSOR
sensors.push_back({sName, sens}); 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;
}
} }
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() { void Marlin2::setup() {
MarlinOutput.reserve(256); MarlinOutput.reserve(256);
@ -26,14 +43,14 @@ namespace esphome {
ESP_LOGD(TAG, "M155 S10"); ESP_LOGD(TAG, "M155 S10");
this->uart_device_->write_str("\r\n\r\nM155 S10\r\n"); write_str("\r\n\r\nM155 S10\r\n");
this->uart_device_->write_str("\r\n\r\nM117 Hello World!\r\n"); write_str("\r\n\r\nM117 Hello World!\r\n");
this->uart_device_->flush(); flush();
} }
void Marlin2::update() { void Marlin2::update() {
while (this->uart_device_->available()) { while (available()) {
char c = this->uart_device_->read(); char c = read();
if( c == '\n' || c == '\r' ) { if( c == '\n' || c == '\r' ) {
process_line(); process_line();
} else { } else {
@ -47,7 +64,7 @@ namespace esphome {
ESP_LOGD(TAG, "M27"); ESP_LOGD(TAG, "M27");
ESP_LOGD(TAG, "M31"); 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 #endif
#ifdef USE_TEXT_SENSOR #ifdef USE_TEXT_SENSOR
if(bed_set_temperature==0.0 && ext_set_temperature==0.0) { 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(ext_temperature < 32.0 && bed_temperature < 32.0){ //TODO define constants for these
//if (find_text_sensor("printer_status") != nullptr) if (find_text_sensor("printer_status") != nullptr)
//find_text_sensor("printer_status")->publish_state("IDLE"); 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) else if(ext_temperature < 150.0 && bed_temperature < 55.0){
//find_text_sensor("printer_status")->publish_state("COOLING"); 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(bed_set_temperature!=0.0 || ext_set_temperature!=0.0) {
//if (find_text_sensor("printer_status") != nullptr) if (find_text_sensor("printer_status") != nullptr)
//find_text_sensor("printer_status")->publish_state("PREHEATING"); find_text_sensor("printer_status")->publish_state("PREHEATING");
} }
#endif #endif
@ -144,9 +163,11 @@ namespace esphome {
} }
//Print Finished //Print Finished
if(MarlinOutput.find("Done printing") != std::string::npos) { if(MarlinOutput.find("Done printing") != std::string::npos) {
// //if (find_text_sensor("printer_status") != nullptr) #ifdef USE_TEXT_SENSOR
// //find_text_sensor("printer_status")->publish_state("FINISHED"); if (find_text_sensor("printer_status") != nullptr)
find_text_sensor("printer_status")->publish_state("FINISHED");
#endif
ESP_LOGD(TAG, "Print Finished"); ESP_LOGD(TAG, "Print Finished");
print_progress = 100; print_progress = 100;

View File

@ -11,11 +11,10 @@
namespace esphome { namespace esphome {
class Marlin2 : public esphome::Component, public uart::UARTDevice { class Marlin2 : public PollingComponent, public uart::UARTDevice {
public: public:
Marlin2() = default; 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 #ifdef USE_SENSOR
void add_sensor(const std::string& sName, sensor::Sensor *sens); 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; void update() override;
protected: protected:
esphome::uart::UARTDevice *uart_device_{nullptr};
std::string MarlinOutput; std::string MarlinOutput;
std::string MarlinTime; std::string MarlinTime;
std::string PrinterState; std::string PrinterState;

View File

@ -19,6 +19,7 @@ from esphome.const import (
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_DURATION, DEVICE_CLASS_DURATION,
) )
from . import Marlin2
CONF_BED_TEMPERATURE = "bed_temperature" CONF_BED_TEMPERATURE = "bed_temperature"
CONF_BED_SET_TEMPERATURE = "bed_set_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 = "print_time"
CONF_PRINT_TIME_REMAINING = "print_time_remaining" CONF_PRINT_TIME_REMAINING = "print_time_remaining"
# CONF_PRINTER_STATUS = "printer_status" CONF_MARLIN = "marlin2"
CONFIG_SCHEMA = cv.Schema(
Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component, sensor.Sensor, uart.UARTDevice)
CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend(
{ {
cv.GenerateID(): cv.declare_id(Marlin2), cv.GenerateID(CONF_MARLIN): cv.use_id(Marlin2),
cv.Optional(CONF_BED_TEMPERATURE): sensor.sensor_schema( cv.Optional(CONF_BED_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS, unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1, accuracy_decimals=1,
@ -78,20 +76,13 @@ CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend(
device_class=DEVICE_CLASS_DURATION, device_class=DEVICE_CLASS_DURATION,
state_class=STATE_CLASS_MEASUREMENT, 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): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) server = await cg.get_variable(config[CONF_MARLIN])
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
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]: 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: if sName in config:
sens = await sensor.new_sensor(config[sName]) sens = await sensor.new_sensor(config[sName])
cg.add(var.add_sensor(sName,sens)) cg.add(server.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))

View File

@ -4,21 +4,22 @@ from esphome.components import text_sensor
from esphome.const import ( from esphome.const import (
ENTITY_CATEGORY_DIAGNOSTIC, ENTITY_CATEGORY_DIAGNOSTIC,
) )
from . import ns, Marlin2 from . import Marlin2
CONF_MARLIN = "marlin2" CONF_MARLIN = "marlin2"
CONFIG_SCHEMA = cv.Schema( CONFIG_SCHEMA = cv.Schema(
{ {
cv.GenerateID(CONF_MARLIN): cv.use_id(Marlin2), 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, entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
} }
) ).extend(cv.polling_component_schema("15s"))
async def to_code(config): async def to_code(config):
server = await cg.get_variable(config[CONF_MARLIN]) server = await cg.get_variable(config[CONF_MARLIN])
for sName in ["printer_state"]: for sName in ["printer_state"]:
if sName in config: if sName in config:
sens = await sensor.new_text_sensor(config[sName]) sens = await text_sensor.new_text_sensor(config[sName])
cg.add(var.add_text_sensor(sName,sens)) cg.add(server.add_text_sensor(sName,sens))