Fixes for actions
This commit is contained in:
parent
7bc36b2a90
commit
176304db46
22
__init__.py
22
__init__.py
@ -22,6 +22,7 @@ DEPENDENCIES = ['uart']
|
|||||||
CONF_MARLIN2_ID = "marlin2_id"
|
CONF_MARLIN2_ID = "marlin2_id"
|
||||||
|
|
||||||
Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component)
|
Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component)
|
||||||
|
Marlin2WriteAction = uart_ns.class_("Marlin2WriteAction", automation.Action)
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
CONFIG_SCHEMA = cv.All(
|
||||||
cv.Schema({
|
cv.Schema({
|
||||||
@ -31,10 +32,23 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
.extend(uart.UART_DEVICE_SCHEMA),
|
.extend(uart.UART_DEVICE_SCHEMA),
|
||||||
)
|
)
|
||||||
|
|
||||||
@automation.register_action("marlin2.set_bed_temperature", SetTemperatureAction, SWITCH_ACTION_SCHEMA)
|
MARLIN2_ACTION_SCHEMA = maybe_simple_id(
|
||||||
async def marlin2_set_bed_temperature_to_code(config, action_id, template_arg, args):
|
{
|
||||||
paren = await cg.get_variable(config[CONF_ID])
|
cv.Required(CONF_ID): cv.use_id(Marlin2),
|
||||||
return cg.new_Pvariable(action_id, template_arg, paren)
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
@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):
|
async def to_code(config):
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
|
@ -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
|
|
34
automation.h
34
automation.h
@ -1,8 +1,36 @@
|
|||||||
#include "automation.h"
|
#pragma once
|
||||||
#include "esphome/core/log.h"
|
|
||||||
|
#include "uart.h"
|
||||||
|
#include "esphome/core/automation.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace esphome {
|
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
|
} // namespace esphome
|
Loading…
x
Reference in New Issue
Block a user