FUb Components

This commit is contained in:
Václav Španinger 2024-12-30 09:42:31 +01:00
parent 88c4396390
commit 5112a91bec
6 changed files with 149 additions and 82 deletions

View File

@ -25,6 +25,7 @@ Notes:
* https://github.com/ssieb/esphome_components/tree/master/components/serial_csv * https://github.com/ssieb/esphome_components/tree/master/components/serial_csv
* https://github.com/esphome/esphome/blob/dev/esphome/components/dht/sensor.py#L34 * https://github.com/esphome/esphome/blob/dev/esphome/components/dht/sensor.py#L34
* https://github.com/mulcmu/esphome-marlin-uart * https://github.com/mulcmu/esphome-marlin-uart
* https://github.com/oxan/esphome-stream-server/blob/master/components/stream_server/stream_server.cpp
Full COnfiguration: Full COnfiguration:
```yaml ```yaml

View File

@ -0,0 +1,36 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import uart
from esphome.const import (
CONF_ID,
CONF_INDEX,
CONF_SENSORS,
CONF_HUMIDITY,
CONF_MODEL,
CONF_PIN,
CONF_TEMPERATURE,
UNIT_CELSIUS,
UNIT_PERCENT,
UNIT_SECOND,
STATE_CLASS_MEASUREMENT,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_DURATION,
)
CODEOWNERS = ["@jonatanrek"]
DEPENDENCIES = ['uart']
Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component)
CONFIG_SCHEMA = cv.All(
cv.Schema( {
cv.GenerateID(): cv.declare_id(Marlin2),
})
.extend(cv.COMPONENT_SCHEMA)
.extend(uart.UART_DEVICE_SCHEMA),
)
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)

View File

@ -17,19 +17,6 @@ namespace esphome {
return nullptr; // Return nullptr if no match is found 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() { void Marlin2::setup() {
MarlinOutput.reserve(256); MarlinOutput.reserve(256);
MarlinOutput = ""; MarlinOutput = "";
@ -39,14 +26,14 @@ namespace esphome {
ESP_LOGD(TAG, "M155 S10"); ESP_LOGD(TAG, "M155 S10");
write_str("\r\n\r\nM155 S10\r\n"); this->uart_device_->write_str("\r\n\r\nM155 S10\r\n");
write_str("\r\n\r\nM117 Hello World!\r\n"); this->uart_device_->write_str("\r\n\r\nM117 Hello World!\r\n");
flush(); this->uart_device_->flush();
} }
void Marlin2::update() { void Marlin2::update() {
while (available()) { while (this->uart_device_->available()) {
char c = read(); char c = this->uart_device_->read();
if( c == '\n' || c == '\r' ) { if( c == '\n' || c == '\r' ) {
process_line(); process_line();
} else { } else {
@ -60,7 +47,7 @@ namespace esphome {
ESP_LOGD(TAG, "M27"); ESP_LOGD(TAG, "M27");
ESP_LOGD(TAG, "M31"); ESP_LOGD(TAG, "M31");
write_str("M27\r\nM31\r\n"); this->uart_device_->write_str("M27\r\nM31\r\n");
} }
} }
@ -84,7 +71,7 @@ namespace esphome {
) { ) {
float ext_temperature, ext_set_temperature, bed_temperature, bed_set_temperature; float ext_temperature, ext_set_temperature, bed_temperature, bed_set_temperature;
if (process_temp_msg(&ext_temperature, &ext_set_temperature, &bed_temperature, &bed_set_temperature) != 0) { if (process_temp_msg(&ext_temperature, &ext_set_temperature, &bed_temperature, &bed_set_temperature) != 0) {
#ifdef USE_SENSOR
if (find_sensor("bed_temperature") != nullptr) if (find_sensor("bed_temperature") != nullptr)
find_sensor("bed_temperature")->publish_state(bed_temperature); find_sensor("bed_temperature")->publish_state(bed_temperature);
@ -96,24 +83,25 @@ namespace esphome {
if (find_sensor("ext_set_temperature") != nullptr) if (find_sensor("ext_set_temperature") != nullptr)
find_sensor("ext_set_temperature")->publish_state(ext_set_temperature); find_sensor("ext_set_temperature")->publish_state(ext_set_temperature);
#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(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");
}
#endif
ESP_LOGD(TAG, "Bed Temperature=%.1f°C Ext Temperature=%.1f°C ", bed_temperature, ext_temperature); 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_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 //reset string for next line
MarlinOutput=""; MarlinOutput="";
return; return;
@ -122,11 +110,12 @@ namespace esphome {
//Parse Progress of the print //Parse Progress of the print
if(MarlinOutput.find("SD printing byte") == 0 ) { if(MarlinOutput.find("SD printing byte") == 0 ) {
print_progress = process_progress_msg(); print_progress = process_progress_msg();
#ifdef USE_SENSOR
if (find_sensor("print_progress") != nullptr) if (find_sensor("print_progress") != nullptr)
find_sensor("print_progress")->publish_state(print_progress); find_sensor("print_progress")->publish_state(print_progress);
ESP_LOGD(TAG, "progress=%.1f", print_progress); ESP_LOGD(TAG, "progress=%.1f", print_progress);
#endif
//reset string for next line //reset string for next line
MarlinOutput=""; MarlinOutput="";
@ -137,13 +126,14 @@ namespace esphome {
if(MarlinOutput.find("echo:Print time: ") == 0) { if(MarlinOutput.find("echo:Print time: ") == 0) {
double current=0; double current=0;
double remaining=0; double remaining=0;
if (process_print_time_msg(&current, &remaining, print_progress) != 0) { if (process_print_time_msg(&current, &remaining, print_progress) != 0) {
#ifdef USE_SENSOR
if (find_sensor("print_time") != nullptr) if (find_sensor("print_time") != nullptr)
find_sensor("print_time")->publish_state(current); find_sensor("print_time")->publish_state(current);
if (find_sensor("print_time_remaining") != nullptr) if (find_sensor("print_time_remaining") != nullptr)
find_sensor("print_time_remaining")->publish_state(remaining); find_sensor("print_time_remaining")->publish_state(remaining);
#endif
ESP_LOGD(TAG, "time=%f remaining=%f", current, remaining); ESP_LOGD(TAG, "time=%f remaining=%f", current, remaining);
} }
@ -161,11 +151,13 @@ namespace esphome {
ESP_LOGD(TAG, "Print Finished"); ESP_LOGD(TAG, "Print Finished");
print_progress = 100; print_progress = 100;
#ifdef USE_SENSOR
if (find_sensor("print_progress") != nullptr) if (find_sensor("print_progress") != nullptr)
find_sensor("print_progress")->publish_state(print_progress); find_sensor("print_progress")->publish_state(print_progress);
if (find_sensor("print_time_remaining") != nullptr) if (find_sensor("print_time_remaining") != nullptr)
find_sensor("print_time_remaining")->publish_state(0); find_sensor("print_time_remaining")->publish_state(0);
#endif
//reset string for next line //reset string for next line
MarlinOutput=""; MarlinOutput="";

View File

@ -1,28 +1,37 @@
#pragma once #pragma once
#include "esphome/core/component.h" #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" #include "esphome/components/uart/uart.h"
#ifdef USE_SENSOR
#include "esphome/components/sensor/sensor.h"
#endif
#ifdef USE_TEXT_SENSOR
#include "esphome/components/text_sensor/text_sensor.h"
#endif
namespace esphome { namespace esphome {
class Marlin2 : public PollingComponent, /*public text_sensor::TextSensor,*/ public uart::UARTDevice { class Marlin2 : public esphome::Component, public uart::UARTDevice {
public: public:
void setup() override; Marlin2() = default;
float get_setup_priority() const override { return setup_priority::LATE; } explicit Marlin2(uart::UARTDevice *uart_device) : uart_device_{uart_device} {}
void update() override; void set_uart_parent(uart::UARTDevice *parent) { this->uart_device_ = parent; }
void add_sensor(const std::string& sName, sensor::Sensor *sens);
//void add_text_sensor(const std::string& sName, text_sensor::TextSensor *sens);
#ifdef USE_SENSOR
void add_sensor(const std::string& sName, sensor::Sensor *sens);
sensor::Sensor* find_sensor(std::string key); sensor::Sensor* find_sensor(std::string key);
// text_sensor::TextSensor* find_text_sensor(std::string key); #endif
#ifdef USE_TEXT_SENSOR
void add_text_sensor(const std::string& sName, text_sensor::TextSensor *tSens);
text_sensor::TextSensor* find_text_sensor(std::string key);
#endif
float get_setup_priority() const override { return setup_priority::LATE; }
void setup() override;
void update() override;
protected: protected:
void process_line(); esphome::uart::UARTDevice *uart_device_{nullptr};
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(double* current, double* remaining, float progress);
std::string MarlinOutput; std::string MarlinOutput;
std::string MarlinTime; std::string MarlinTime;
@ -30,8 +39,17 @@ class Marlin2 : public PollingComponent, /*public text_sensor::TextSensor,*/ pub
float print_progress = 0; float print_progress = 0;
#ifdef USE_SENSOR
std::vector<std::pair<std::string, sensor::Sensor *>> sensors; std::vector<std::pair<std::string, sensor::Sensor *>> sensors;
// std::vector<std::pair<std::string, text_sensor::TextSensor *>> text_sensors; #endif
#ifdef USE_TEXT_SENSOR
std::vector<std::pair<std::string, text_sensor::TextSensor *>> text_sensors;
#endif
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(double* current, double* remaining, float progress);
private: private:
unsigned long millisProgress=0; unsigned long millisProgress=0;

View File

@ -20,10 +20,6 @@ from esphome.const import (
DEVICE_CLASS_DURATION, DEVICE_CLASS_DURATION,
) )
CODEOWNERS = ["@jonatanrek"]
DEPENDENCIES = ['uart']
CONF_BED_TEMPERATURE = "bed_temperature" CONF_BED_TEMPERATURE = "bed_temperature"
CONF_BED_SET_TEMPERATURE = "bed_set_temperature" CONF_BED_SET_TEMPERATURE = "bed_set_temperature"
@ -84,7 +80,7 @@ CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend(
), ),
# cv.Optional(CONF_PRINTER_STATUS): text_sensor.text_sensor_schema(), # 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]) var = cg.new_Pvariable(config[CONF_ID])

24
text_sensor.py Normal file
View File

@ -0,0 +1,24 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import text_sensor
from esphome.const import (
ENTITY_CATEGORY_DIAGNOSTIC,
)
from . import ns, Marlin2
CONF_MARLIN = "marlin2"
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_MARLIN): cv.use_id(Marlin2),
cv.Optional(CONF_CONNECTED): text_sensor.text_sensor(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
}
)
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))