FUb Components
This commit is contained in:
parent
88c4396390
commit
5112a91bec
@ -25,6 +25,7 @@ Notes:
|
||||
* 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/mulcmu/esphome-marlin-uart
|
||||
* https://github.com/oxan/esphome-stream-server/blob/master/components/stream_server/stream_server.cpp
|
||||
|
||||
Full COnfiguration:
|
||||
```yaml
|
||||
|
36
__init__.py
36
__init__.py
@ -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)
|
112
marlin2.cpp
112
marlin2.cpp
@ -17,19 +17,6 @@ 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 = "";
|
||||
@ -39,14 +26,14 @@ namespace esphome {
|
||||
|
||||
ESP_LOGD(TAG, "M155 S10");
|
||||
|
||||
write_str("\r\n\r\nM155 S10\r\n");
|
||||
write_str("\r\n\r\nM117 Hello World!\r\n");
|
||||
flush();
|
||||
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();
|
||||
}
|
||||
|
||||
void Marlin2::update() {
|
||||
while (available()) {
|
||||
char c = read();
|
||||
void Marlin2::update() {
|
||||
while (this->uart_device_->available()) {
|
||||
char c = this->uart_device_->read();
|
||||
if( c == '\n' || c == '\r' ) {
|
||||
process_line();
|
||||
} else {
|
||||
@ -60,7 +47,7 @@ namespace esphome {
|
||||
ESP_LOGD(TAG, "M27");
|
||||
ESP_LOGD(TAG, "M31");
|
||||
|
||||
write_str("M27\r\nM31\r\n");
|
||||
this->uart_device_->write_str("M27\r\nM31\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,36 +71,37 @@ namespace esphome {
|
||||
) {
|
||||
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 (find_sensor("bed_temperature") != nullptr)
|
||||
find_sensor("bed_temperature")->publish_state(bed_temperature);
|
||||
|
||||
if (find_sensor("bed_set_temperature") != nullptr)
|
||||
find_sensor("bed_set_temperature")->publish_state(bed_set_temperature);
|
||||
|
||||
if (find_sensor("ext_temperature") != nullptr)
|
||||
find_sensor("ext_temperature")->publish_state(ext_temperature);
|
||||
|
||||
if (find_sensor("ext_set_temperature") != nullptr)
|
||||
find_sensor("ext_set_temperature")->publish_state(ext_set_temperature);
|
||||
#ifdef USE_SENSOR
|
||||
if (find_sensor("bed_temperature") != nullptr)
|
||||
find_sensor("bed_temperature")->publish_state(bed_temperature);
|
||||
|
||||
if (find_sensor("bed_set_temperature") != nullptr)
|
||||
find_sensor("bed_set_temperature")->publish_state(bed_set_temperature);
|
||||
|
||||
if (find_sensor("ext_temperature") != nullptr)
|
||||
find_sensor("ext_temperature")->publish_state(ext_temperature);
|
||||
|
||||
if (find_sensor("ext_set_temperature") != nullptr)
|
||||
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);
|
||||
}
|
||||
|
||||
// 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="";
|
||||
return;
|
||||
@ -122,11 +110,12 @@ namespace esphome {
|
||||
//Parse Progress of the print
|
||||
if(MarlinOutput.find("SD printing byte") == 0 ) {
|
||||
print_progress = process_progress_msg();
|
||||
#ifdef USE_SENSOR
|
||||
if (find_sensor("print_progress") != nullptr)
|
||||
find_sensor("print_progress")->publish_state(print_progress);
|
||||
|
||||
if (find_sensor("print_progress") != nullptr)
|
||||
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
|
||||
MarlinOutput="";
|
||||
@ -137,13 +126,14 @@ namespace esphome {
|
||||
if(MarlinOutput.find("echo:Print time: ") == 0) {
|
||||
double current=0;
|
||||
double remaining=0;
|
||||
|
||||
if (process_print_time_msg(¤t, &remaining, print_progress) != 0) {
|
||||
if (find_sensor("print_time") != nullptr)
|
||||
find_sensor("print_time")->publish_state(current);
|
||||
#ifdef USE_SENSOR
|
||||
if (find_sensor("print_time") != nullptr)
|
||||
find_sensor("print_time")->publish_state(current);
|
||||
|
||||
if (find_sensor("print_time_remaining") != nullptr)
|
||||
find_sensor("print_time_remaining")->publish_state(remaining);
|
||||
if (find_sensor("print_time_remaining") != nullptr)
|
||||
find_sensor("print_time_remaining")->publish_state(remaining);
|
||||
#endif
|
||||
|
||||
ESP_LOGD(TAG, "time=%f remaining=%f", current, remaining);
|
||||
}
|
||||
@ -160,12 +150,14 @@ namespace esphome {
|
||||
|
||||
ESP_LOGD(TAG, "Print Finished");
|
||||
print_progress = 100;
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
if (find_sensor("print_progress") != nullptr)
|
||||
find_sensor("print_progress")->publish_state(print_progress);
|
||||
|
||||
if (find_sensor("print_progress") != nullptr)
|
||||
find_sensor("print_progress")->publish_state(print_progress);
|
||||
|
||||
if (find_sensor("print_time_remaining") != nullptr)
|
||||
find_sensor("print_time_remaining")->publish_state(0);
|
||||
if (find_sensor("print_time_remaining") != nullptr)
|
||||
find_sensor("print_time_remaining")->publish_state(0);
|
||||
#endif
|
||||
|
||||
//reset string for next line
|
||||
MarlinOutput="";
|
||||
|
52
marlin2.h
52
marlin2.h
@ -1,28 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#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"
|
||||
#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 {
|
||||
|
||||
class Marlin2 : public PollingComponent, /*public text_sensor::TextSensor,*/ public uart::UARTDevice {
|
||||
class Marlin2 : public esphome::Component, 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);
|
||||
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);
|
||||
sensor::Sensor* find_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;
|
||||
|
||||
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(double* current, double* remaining, float progress);
|
||||
esphome::uart::UARTDevice *uart_device_{nullptr};
|
||||
|
||||
std::string MarlinOutput;
|
||||
std::string MarlinTime;
|
||||
@ -30,8 +39,17 @@ class Marlin2 : public PollingComponent, /*public text_sensor::TextSensor,*/ pub
|
||||
|
||||
float print_progress = 0;
|
||||
|
||||
std::vector<std::pair<std::string, sensor::Sensor *>> sensors;
|
||||
// std::vector<std::pair<std::string, text_sensor::TextSensor *>> text_sensors;
|
||||
#ifdef USE_SENSOR
|
||||
std::vector<std::pair<std::string, sensor::Sensor *>> 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:
|
||||
unsigned long millisProgress=0;
|
||||
|
@ -20,10 +20,6 @@ from esphome.const import (
|
||||
DEVICE_CLASS_DURATION,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@jonatanrek"]
|
||||
|
||||
DEPENDENCIES = ['uart']
|
||||
|
||||
CONF_BED_TEMPERATURE = "bed_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(),
|
||||
}
|
||||
).extend(cv.polling_component_schema("15s"))
|
||||
)
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
|
24
text_sensor.py
Normal file
24
text_sensor.py
Normal 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))
|
Loading…
Reference in New Issue
Block a user