Fixes for actions

This commit is contained in:
Václav Španinger 2025-03-03 09:46:15 +01:00
parent 7bc36b2a90
commit 176304db46
3 changed files with 49 additions and 25 deletions

View File

@ -22,6 +22,7 @@ DEPENDENCIES = ['uart']
CONF_MARLIN2_ID = "marlin2_id"
Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component)
Marlin2WriteAction = uart_ns.class_("Marlin2WriteAction", automation.Action)
CONFIG_SCHEMA = cv.All(
cv.Schema({
@ -31,10 +32,23 @@ CONFIG_SCHEMA = cv.All(
.extend(uart.UART_DEVICE_SCHEMA),
)
@automation.register_action("marlin2.set_bed_temperature", SetTemperatureAction, SWITCH_ACTION_SCHEMA)
async def marlin2_set_bed_temperature_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(action_id, template_arg, paren)
MARLIN2_ACTION_SCHEMA = maybe_simple_id(
{
cv.Required(CONF_ID): cv.use_id(Marlin2),
}
)
@automation.register_action(
"marlin2.write",
Marlin2WriteAction,
cv.maybe_simple_value(
{
cv.GenerateID(): cv.use_id(UARTComponent),
cv.Required(CONF_DATA): cv.templatable(validate_raw_data),
},
key=CONF_DATA,
),
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])

View File

@ -1,18 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/switch/switch.h"
namespace esphome {
template<typename... Ts> class SetTemperatureAction : public Action<Ts...> {
public:
explicit SetTemperatureAction(Marlin2 *a_marlin2) : switch_(a_marlin2) {}
void play(Ts... x) override { this->switch_->turn_on(); }
protected:
Switch *switch_;
};
} // namespace esphome

View File

@ -1,8 +1,36 @@
#include "automation.h"
#include "esphome/core/log.h"
#pragma once
#include "uart.h"
#include "esphome/core/automation.h"
#include <vector>
namespace esphome {
static const char *const TAG = "switch.automation";
template<typename... Ts> class Marlin2WriteAction : public Action<Ts...>, public Parented<UARTComponent> {
public:
void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) {
this->data_func_ = func;
this->static_ = false;
}
void set_data_static(const std::vector<uint8_t> &data) {
this->data_static_ = data;
this->static_ = true;
}
void play(Ts... x) override {
if (this->static_) {
this->parent_->write_array(this->data_static_);
} else {
auto val = this->data_func_(x...);
this->parent_->write_array(val);
}
}
protected:
bool static_{false};
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
std::vector<uint8_t> data_static_{};
};
} // namespace esphome