mirror of
https://github.com/esphome/esphome.git
synced 2026-09-27 06:50:22 +00:00
[rf_bridge] Use register_apply_action for the actions (#19497)
This commit is contained in:
@@ -25,29 +25,6 @@ RFBridgeComponent = rf_bridge_ns.class_(
|
||||
RFBridgeData = rf_bridge_ns.struct("RFBridgeData")
|
||||
RFBridgeAdvancedData = rf_bridge_ns.struct("RFBridgeAdvancedData")
|
||||
|
||||
RFBridgeSendCodeAction = rf_bridge_ns.class_(
|
||||
"RFBridgeSendCodeAction", automation.Action
|
||||
)
|
||||
RFBridgeSendAdvancedCodeAction = rf_bridge_ns.class_(
|
||||
"RFBridgeSendAdvancedCodeAction", automation.Action
|
||||
)
|
||||
|
||||
RFBridgeLearnAction = rf_bridge_ns.class_("RFBridgeLearnAction", automation.Action)
|
||||
|
||||
RFBridgeStartAdvancedSniffingAction = rf_bridge_ns.class_(
|
||||
"RFBridgeStartAdvancedSniffingAction", automation.Action
|
||||
)
|
||||
RFBridgeStopAdvancedSniffingAction = rf_bridge_ns.class_(
|
||||
"RFBridgeStopAdvancedSniffingAction", automation.Action
|
||||
)
|
||||
|
||||
RFBridgeStartBucketSniffingAction = rf_bridge_ns.class_(
|
||||
"RFBridgeStartBucketSniffingAction", automation.Action
|
||||
)
|
||||
|
||||
RFBridgeBeepAction = rf_bridge_ns.class_("RFBridgeBeepAction", automation.Action)
|
||||
|
||||
RFBridgeSendRawAction = rf_bridge_ns.class_("RFBridgeSendRawAction", automation.Action)
|
||||
|
||||
CONF_ON_CODE_RECEIVED = "on_code_received"
|
||||
CONF_ON_ADVANCED_CODE_RECEIVED = "on_advanced_code_received"
|
||||
@@ -110,74 +87,48 @@ RFBRIDGE_SEND_CODE_SCHEMA = cv.Schema(
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"rf_bridge.send_code",
|
||||
RFBridgeSendCodeAction,
|
||||
RFBRIDGE_SEND_CODE_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyCall(
|
||||
"send_code(rf_bridge::RFBridgeData{{.sync = {}, .low = {}, .high = {}, .code = {}}})",
|
||||
(
|
||||
(CONF_SYNC, cg.uint16),
|
||||
(CONF_LOW, cg.uint16),
|
||||
(CONF_HIGH, cg.uint16),
|
||||
(CONF_CODE, cg.uint32),
|
||||
),
|
||||
),
|
||||
)
|
||||
async def rf_bridge_send_code_to_code(config, action_id, template_args, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_args, paren)
|
||||
template_ = await cg.templatable(config[CONF_SYNC], args, cg.uint16)
|
||||
cg.add(var.set_sync(template_))
|
||||
template_ = await cg.templatable(config[CONF_LOW], args, cg.uint16)
|
||||
cg.add(var.set_low(template_))
|
||||
template_ = await cg.templatable(config[CONF_HIGH], args, cg.uint16)
|
||||
cg.add(var.set_high(template_))
|
||||
template_ = await cg.templatable(config[CONF_CODE], args, cg.uint32)
|
||||
cg.add(var.set_code(template_))
|
||||
return var
|
||||
|
||||
|
||||
RFBRIDGE_ID_SCHEMA = cv.Schema({cv.GenerateID(): cv.use_id(RFBridgeComponent)})
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"rf_bridge.learn", RFBridgeLearnAction, RFBRIDGE_ID_SCHEMA, synchronous=True
|
||||
automation.register_apply_action(
|
||||
"rf_bridge.learn", RFBRIDGE_ID_SCHEMA, automation.ApplyCall("learn()")
|
||||
)
|
||||
async def rf_bridge_learnx_to_code(config, action_id, template_args, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(action_id, template_args, paren)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"rf_bridge.start_advanced_sniffing",
|
||||
RFBridgeStartAdvancedSniffingAction,
|
||||
RFBRIDGE_ID_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyCall("start_advanced_sniffing()"),
|
||||
)
|
||||
async def rf_bridge_start_advanced_sniffing_to_code(
|
||||
config, action_id, template_args, args
|
||||
):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(action_id, template_args, paren)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"rf_bridge.stop_advanced_sniffing",
|
||||
RFBridgeStopAdvancedSniffingAction,
|
||||
RFBRIDGE_ID_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyCall("stop_advanced_sniffing()"),
|
||||
)
|
||||
async def rf_bridge_stop_advanced_sniffing_to_code(
|
||||
config, action_id, template_args, args
|
||||
):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(action_id, template_args, paren)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"rf_bridge.start_bucket_sniffing",
|
||||
RFBridgeStartBucketSniffingAction,
|
||||
RFBRIDGE_ID_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyCall("start_bucket_sniffing()"),
|
||||
)
|
||||
async def rf_bridge_start_bucket_sniffing_to_code(
|
||||
config, action_id, template_args, args
|
||||
):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(action_id, template_args, paren)
|
||||
|
||||
|
||||
RFBRIDGE_SEND_ADVANCED_CODE_SCHEMA = cv.Schema(
|
||||
@@ -190,22 +141,18 @@ RFBRIDGE_SEND_ADVANCED_CODE_SCHEMA = cv.Schema(
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"rf_bridge.send_advanced_code",
|
||||
RFBridgeSendAdvancedCodeAction,
|
||||
RFBRIDGE_SEND_ADVANCED_CODE_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyCall(
|
||||
"send_advanced_code(rf_bridge::RFBridgeAdvancedData{{.length = {}, .protocol = {}, .code = {}}})",
|
||||
(
|
||||
(CONF_LENGTH, cg.uint8),
|
||||
(CONF_PROTOCOL, cg.uint8),
|
||||
(CONF_CODE, cg.std_string),
|
||||
),
|
||||
),
|
||||
)
|
||||
async def rf_bridge_send_advanced_code_to_code(config, action_id, template_args, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_args, paren)
|
||||
template_ = await cg.templatable(config[CONF_LENGTH], args, cg.uint8)
|
||||
cg.add(var.set_length(template_))
|
||||
template_ = await cg.templatable(config[CONF_PROTOCOL], args, cg.uint8)
|
||||
cg.add(var.set_protocol(template_))
|
||||
template_ = await cg.templatable(config[CONF_CODE], args, cg.std_string)
|
||||
cg.add(var.set_code(template_))
|
||||
return var
|
||||
|
||||
|
||||
RFBRIDGE_SEND_RAW_SCHEMA = cv.Schema(
|
||||
@@ -216,18 +163,11 @@ RFBRIDGE_SEND_RAW_SCHEMA = cv.Schema(
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"rf_bridge.send_raw",
|
||||
RFBridgeSendRawAction,
|
||||
RFBRIDGE_SEND_RAW_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyField(CONF_RAW, "send_raw", cg.std_string),
|
||||
)
|
||||
async def rf_bridge_send_raw_to_code(config, action_id, template_args, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_args, paren)
|
||||
template_ = await cg.templatable(config[CONF_RAW], args, cg.std_string)
|
||||
cg.add(var.set_raw(template_))
|
||||
return var
|
||||
|
||||
|
||||
RFBRIDGE_BEEP_SCHEMA = cv.Schema(
|
||||
@@ -238,12 +178,8 @@ RFBRIDGE_BEEP_SCHEMA = cv.Schema(
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"rf_bridge.beep", RFBridgeBeepAction, RFBRIDGE_BEEP_SCHEMA, synchronous=True
|
||||
automation.register_apply_action(
|
||||
"rf_bridge.beep",
|
||||
RFBRIDGE_BEEP_SCHEMA,
|
||||
automation.ApplyField(CONF_DURATION, "beep", cg.uint16),
|
||||
)
|
||||
async def rf_bridge_beep_to_code(config, action_id, template_args, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_args, paren)
|
||||
template_ = await cg.templatable(config[CONF_DURATION], args, cg.uint16)
|
||||
cg.add(var.set_duration(template_))
|
||||
return var
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
#include "esphome/core/automation.h"
|
||||
|
||||
namespace esphome::rf_bridge {
|
||||
|
||||
@@ -89,106 +88,4 @@ class RFBridgeComponent final : public uart::UARTDevice, public Component {
|
||||
CallbackManager<void(RFBridgeAdvancedData)> advanced_data_callback_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class RFBridgeSendCodeAction final : public Action<Ts...> {
|
||||
public:
|
||||
RFBridgeSendCodeAction(RFBridgeComponent *parent) : parent_(parent) {}
|
||||
TEMPLATABLE_VALUE(uint16_t, sync)
|
||||
TEMPLATABLE_VALUE(uint16_t, low)
|
||||
TEMPLATABLE_VALUE(uint16_t, high)
|
||||
TEMPLATABLE_VALUE(uint32_t, code)
|
||||
|
||||
void play(const Ts &...x) {
|
||||
RFBridgeData data{};
|
||||
data.sync = this->sync_.value(x...);
|
||||
data.low = this->low_.value(x...);
|
||||
data.high = this->high_.value(x...);
|
||||
data.code = this->code_.value(x...);
|
||||
this->parent_->send_code(data);
|
||||
}
|
||||
|
||||
protected:
|
||||
RFBridgeComponent *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class RFBridgeSendAdvancedCodeAction final : public Action<Ts...> {
|
||||
public:
|
||||
RFBridgeSendAdvancedCodeAction(RFBridgeComponent *parent) : parent_(parent) {}
|
||||
TEMPLATABLE_VALUE(uint8_t, length)
|
||||
TEMPLATABLE_VALUE(uint8_t, protocol)
|
||||
TEMPLATABLE_VALUE(std::string, code)
|
||||
|
||||
void play(const Ts &...x) {
|
||||
RFBridgeAdvancedData data{};
|
||||
data.length = this->length_.value(x...);
|
||||
data.protocol = this->protocol_.value(x...);
|
||||
data.code = this->code_.value(x...);
|
||||
this->parent_->send_advanced_code(data);
|
||||
}
|
||||
|
||||
protected:
|
||||
RFBridgeComponent *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class RFBridgeLearnAction final : public Action<Ts...> {
|
||||
public:
|
||||
RFBridgeLearnAction(RFBridgeComponent *parent) : parent_(parent) {}
|
||||
|
||||
void play(const Ts &...x) { this->parent_->learn(); }
|
||||
|
||||
protected:
|
||||
RFBridgeComponent *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class RFBridgeStartAdvancedSniffingAction final : public Action<Ts...> {
|
||||
public:
|
||||
RFBridgeStartAdvancedSniffingAction(RFBridgeComponent *parent) : parent_(parent) {}
|
||||
|
||||
void play(const Ts &...x) { this->parent_->start_advanced_sniffing(); }
|
||||
|
||||
protected:
|
||||
RFBridgeComponent *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class RFBridgeStopAdvancedSniffingAction final : public Action<Ts...> {
|
||||
public:
|
||||
RFBridgeStopAdvancedSniffingAction(RFBridgeComponent *parent) : parent_(parent) {}
|
||||
|
||||
void play(const Ts &...x) { this->parent_->stop_advanced_sniffing(); }
|
||||
|
||||
protected:
|
||||
RFBridgeComponent *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class RFBridgeStartBucketSniffingAction final : public Action<Ts...> {
|
||||
public:
|
||||
RFBridgeStartBucketSniffingAction(RFBridgeComponent *parent) : parent_(parent) {}
|
||||
|
||||
void play(const Ts &...x) { this->parent_->start_bucket_sniffing(); }
|
||||
|
||||
protected:
|
||||
RFBridgeComponent *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class RFBridgeSendRawAction final : public Action<Ts...> {
|
||||
public:
|
||||
RFBridgeSendRawAction(RFBridgeComponent *parent) : parent_(parent) {}
|
||||
TEMPLATABLE_VALUE(std::string, raw)
|
||||
|
||||
void play(const Ts &...x) { this->parent_->send_raw(this->raw_.value(x...)); }
|
||||
|
||||
protected:
|
||||
RFBridgeComponent *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class RFBridgeBeepAction final : public Action<Ts...> {
|
||||
public:
|
||||
RFBridgeBeepAction(RFBridgeComponent *parent) : parent_(parent) {}
|
||||
TEMPLATABLE_VALUE(uint16_t, duration)
|
||||
|
||||
void play(const Ts &...x) { this->parent_->beep(this->duration_.value(x...)); }
|
||||
|
||||
protected:
|
||||
RFBridgeComponent *parent_;
|
||||
};
|
||||
|
||||
} // namespace esphome::rf_bridge
|
||||
|
||||
@@ -27,3 +27,6 @@ rf_bridge:
|
||||
code: "ABC123"
|
||||
- rf_bridge.send_raw:
|
||||
raw: "AAA5070008001000ABC12355"
|
||||
- rf_bridge.start_bucket_sniffing:
|
||||
- rf_bridge.beep:
|
||||
duration: !lambda return 100;
|
||||
|
||||
Reference in New Issue
Block a user