Tweaks And Fixes

This commit is contained in:
Václav Španinger 2025-03-05 19:03:06 +01:00
parent dce9c5d67d
commit ebef6ce886
4 changed files with 35 additions and 29 deletions

View File

@ -16,8 +16,10 @@ from esphome.const import (
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_DURATION,
CONF_DATA,
CONF_VALUE
)
from esphome import pins, automation
from esphome.core import CORE, coroutine_with_priority
CODEOWNERS = ["@jonatanrek"]
DEPENDENCIES = ['uart']
@ -43,19 +45,26 @@ def validate_raw_data(value):
return cv.Schema([cv.hex_uint8_t])(value)
raise cv.Invalid("data must either be a string wrapped in quotes or a list of bytes")
@automation.register_action(
"marlin2.write",
Marlin2WriteAction,
cv.maybe_simple_value(
{
cv.GenerateID(): cv.declare_id(Marlin2),
cv.Required(CONF_DATA): cv.templatable(validate_raw_data),
},
key=CONF_DATA,
),
)
@coroutine_with_priority(100.0)
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)
await uart.register_uart_device(var, config)
OPERATION_BASE_SCHEMA = cv.Schema({
cv.GenerateID(): cv.use_id(Marlin2),
cv.Required(CONF_VALUE): cv.templatable(cv.string_strict),
})
@automation.register_action(
"marlin2.write",
Marlin2WriteAction,
OPERATION_BASE_SCHEMA,
)
async def marlin2_write_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_VALUE], args, cg.std_string)
cg.add(var.set_value(template_))
return var

View File

@ -1,22 +1,20 @@
#pragma once
#include "marlin2.h"
#include "esphome/core/automation.h"
#include <vector>
#include "esphome/core/component.h"
#include "marlin2.h"
namespace esphome {
template<typename... Ts> class Marlin2WriteGCodeAction : public Action<Ts...> {
template<typename... Ts> class Marlin2WriteAction : public Action<Ts...> {
public:
explicit Marlin2WriteGCodeAction(Marlin2 *marlin2) : marlin2_(marlin2) {}
TEMPLATABLE_VALUE(std::string, gcode)
explicit Marlin2WriteAction(Marlin2 *marlin2) : marlin2_(marlin2) {}
TEMPLATABLE_VALUE(std::string, value)
void play(Ts... x) override {
this->marlin2_->write_str("\r\n\r\nM117 Action Called!\r\n");;
this->marlin2_->write("M117 Action Called!");
}
protected:
Marlin2 *marlin2_;
};

View File

@ -50,12 +50,12 @@ namespace esphome {
set_printer_state("IDLE");
}
void Marlin2::set_bed_setpoint() {
void Marlin2::write(std::string gcode) {
ESP_LOGD(TAG, "->GCODE: %s", gcode.c_str());
write_str(gcode.c_str());
flush();
}
}
void Marlin2::set_extruder_setpoint() {
}
void Marlin2::update() {
while (available()) {
char c = read();

View File

@ -24,8 +24,7 @@ class Marlin2 : public PollingComponent, public uart::UARTDevice {
void add_text_sensor(const std::string& sName, text_sensor::TextSensor *tSens);
text_sensor::TextSensor* find_text_sensor(std::string key);
#endif
void set_bed_setpoint();
void set_extruder_setpoint();
void write(std::string status);
float get_setup_priority() const override { return setup_priority::LATE; }
void setup() override;