mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 23:40:25 +00:00
[lightwaverf] Use register_apply_action for lightwaverf.send_raw (#19698)
This commit is contained in:
@@ -11,8 +11,6 @@ from esphome.const import (
|
||||
CONF_REPEAT,
|
||||
CONF_WRITE_PIN,
|
||||
)
|
||||
from esphome.core import ID
|
||||
from esphome.cpp_generator import MockObj, TemplateArgsType
|
||||
from esphome.cpp_helpers import gpio_pin_expression
|
||||
from esphome.types import ConfigType
|
||||
|
||||
@@ -24,7 +22,6 @@ lightwaverf_ns = cg.esphome_ns.namespace("lightwaverf")
|
||||
LIGHTWAVERFComponent = lightwaverf_ns.class_(
|
||||
"LightWaveRF", cg.Component, cg.PollingComponent
|
||||
)
|
||||
LightwaveRawAction = lightwaverf_ns.class_("SendRawAction", automation.Action)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
@@ -36,49 +33,36 @@ CONFIG_SCHEMA = cv.Schema(
|
||||
).extend(cv.polling_component_schema("1s"))
|
||||
|
||||
|
||||
LIGHTWAVE_SEND_SCHEMA = cv.Any(
|
||||
cv.int_range(min=1),
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(LIGHTWAVERFComponent),
|
||||
cv.Required(CONF_NAME): cv.string,
|
||||
cv.Required(CONF_CODE): cv.All(
|
||||
[cv.Any(cv.hex_uint8_t)],
|
||||
cv.Length(min=10),
|
||||
),
|
||||
cv.Optional(CONF_REPEAT, default=10): cv.int_,
|
||||
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
|
||||
cv.Optional(CONF_PULSE_LENGTH, default=330): cv.int_,
|
||||
}
|
||||
LIGHTWAVE_SEND_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(LIGHTWAVERFComponent),
|
||||
cv.Required(CONF_NAME): cv.string,
|
||||
cv.Required(CONF_CODE): cv.All(
|
||||
[cv.Any(cv.hex_uint8_t)],
|
||||
cv.Length(min=10),
|
||||
),
|
||||
cv.Optional(CONF_REPEAT, default=10): cv.int_,
|
||||
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
|
||||
cv.Optional(CONF_PULSE_LENGTH, default=330): cv.int_,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
automation.register_apply_action(
|
||||
"lightwaverf.send_raw",
|
||||
LIGHTWAVE_SEND_SCHEMA,
|
||||
automation.ApplyCall(
|
||||
"send_rx({}, {}, {}, {})",
|
||||
(
|
||||
(CONF_CODE, cg.std_vector.template(cg.uint8)),
|
||||
(CONF_REPEAT, cg.uint8),
|
||||
(CONF_INVERTED, cg.bool_),
|
||||
(CONF_PULSE_LENGTH, cg.int_),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"lightwaverf.send_raw",
|
||||
LightwaveRawAction,
|
||||
LIGHTWAVE_SEND_SCHEMA,
|
||||
synchronous=True,
|
||||
)
|
||||
async def send_raw_to_code(
|
||||
config: ConfigType,
|
||||
action_id: ID,
|
||||
template_arg: cg.TemplateArguments,
|
||||
args: TemplateArgsType,
|
||||
) -> MockObj:
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
|
||||
template_ = await cg.templatable(config[CONF_REPEAT], args, cg.int_)
|
||||
cg.add(var.set_repeat(template_))
|
||||
template_ = await cg.templatable(config[CONF_INVERTED], args, cg.int_)
|
||||
cg.add(var.set_inverted(template_))
|
||||
template_ = await cg.templatable(config[CONF_PULSE_LENGTH], args, cg.int_)
|
||||
cg.add(var.set_pulse_length(template_))
|
||||
cg.add(var.set_code(config[CONF_CODE]))
|
||||
return var
|
||||
|
||||
|
||||
async def to_code(config: ConfigType) -> None:
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/automation.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
@@ -37,28 +36,6 @@ class LightWaveRF final : public PollingComponent {
|
||||
LwTx lwtx_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class SendRawAction final : public Action<Ts...> {
|
||||
public:
|
||||
SendRawAction(LightWaveRF *parent) : parent_(parent){};
|
||||
TEMPLATABLE_VALUE(int, repeat);
|
||||
TEMPLATABLE_VALUE(int, inverted);
|
||||
TEMPLATABLE_VALUE(int, pulse_length);
|
||||
TEMPLATABLE_VALUE(std::vector<uint8_t>, code);
|
||||
void set_code(std::initializer_list<uint8_t> data) { this->code_ = std::vector<uint8_t>(data); }
|
||||
|
||||
void play(const Ts &...x) {
|
||||
int repeats = this->repeat_.value(x...);
|
||||
int inverted = this->inverted_.value(x...);
|
||||
int pulse_length = this->pulse_length_.value(x...);
|
||||
std::vector<uint8_t> msg = this->code_.value(x...);
|
||||
|
||||
this->parent_->send_rx(msg, repeats, inverted, pulse_length);
|
||||
}
|
||||
|
||||
protected:
|
||||
LightWaveRF *parent_;
|
||||
};
|
||||
|
||||
#endif
|
||||
} // namespace esphome::lightwaverf
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user