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/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
|
||||||
|
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)
|
104
marlin2.cpp
104
marlin2.cpp
@ -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,36 +71,37 @@ 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)
|
||||||
|
find_sensor("bed_temperature")->publish_state(bed_temperature);
|
||||||
|
|
||||||
if (find_sensor("bed_temperature") != nullptr)
|
if (find_sensor("bed_set_temperature") != nullptr)
|
||||||
find_sensor("bed_temperature")->publish_state(bed_temperature);
|
find_sensor("bed_set_temperature")->publish_state(bed_set_temperature);
|
||||||
|
|
||||||
if (find_sensor("bed_set_temperature") != nullptr)
|
if (find_sensor("ext_temperature") != nullptr)
|
||||||
find_sensor("bed_set_temperature")->publish_state(bed_set_temperature);
|
find_sensor("ext_temperature")->publish_state(ext_temperature);
|
||||||
|
|
||||||
if (find_sensor("ext_temperature") != nullptr)
|
if (find_sensor("ext_set_temperature") != nullptr)
|
||||||
find_sensor("ext_temperature")->publish_state(ext_temperature);
|
find_sensor("ext_set_temperature")->publish_state(ext_set_temperature);
|
||||||
|
#endif
|
||||||
if (find_sensor("ext_set_temperature") != nullptr)
|
#ifdef USE_TEXT_SENSOR
|
||||||
find_sensor("ext_set_temperature")->publish_state(ext_set_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");
|
||||||
|
}
|
||||||
|
#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)
|
||||||
|
find_sensor("print_progress")->publish_state(print_progress);
|
||||||
|
|
||||||
if (find_sensor("print_progress") != nullptr)
|
ESP_LOGD(TAG, "progress=%.1f", print_progress);
|
||||||
find_sensor("print_progress")->publish_state(print_progress);
|
#endif
|
||||||
|
|
||||||
ESP_LOGD(TAG, "progress=%.1f", print_progress);
|
|
||||||
|
|
||||||
//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(¤t, &remaining, print_progress) != 0) {
|
if (process_print_time_msg(¤t, &remaining, print_progress) != 0) {
|
||||||
if (find_sensor("print_time") != nullptr)
|
#ifdef USE_SENSOR
|
||||||
find_sensor("print_time")->publish_state(current);
|
if (find_sensor("print_time") != nullptr)
|
||||||
|
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;
|
||||||
|
|
||||||
if (find_sensor("print_progress") != nullptr)
|
#ifdef USE_SENSOR
|
||||||
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)
|
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="";
|
||||||
|
50
marlin2.h
50
marlin2.h
@ -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);
|
|
||||||
|
|
||||||
sensor::Sensor* find_sensor(std::string key);
|
#ifdef USE_SENSOR
|
||||||
// text_sensor::TextSensor* find_text_sensor(std::string key);
|
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;
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
std::vector<std::pair<std::string, sensor::Sensor *>> sensors;
|
#ifdef USE_SENSOR
|
||||||
// std::vector<std::pair<std::string, text_sensor::TextSensor *>> text_sensors;
|
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:
|
private:
|
||||||
unsigned long millisProgress=0;
|
unsigned long millisProgress=0;
|
||||||
|
@ -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
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