[sim800l] Use register_apply_action for the five sim800l actions (#19645)

This commit is contained in:
J. Nick Koston
2026-09-25 19:12:51 +01:00
committed by GitHub
parent 15d756d559
commit 857a519946
2 changed files with 17 additions and 117 deletions
+17 -54
View File
@@ -11,15 +11,6 @@ MULTI_CONF = True
sim800l_ns = cg.esphome_ns.namespace("sim800l")
Sim800LComponent = sim800l_ns.class_("Sim800LComponent", cg.Component)
# Actions
Sim800LSendSmsAction = sim800l_ns.class_("Sim800LSendSmsAction", automation.Action)
Sim800LSendUssdAction = sim800l_ns.class_("Sim800LSendUssdAction", automation.Action)
Sim800LDialAction = sim800l_ns.class_("Sim800LDialAction", automation.Action)
Sim800LConnectAction = sim800l_ns.class_("Sim800LConnectAction", automation.Action)
Sim800LDisconnectAction = sim800l_ns.class_(
"Sim800LDisconnectAction", automation.Action
)
CONF_SIM800L_ID = "sim800l_id"
CONF_ON_SMS_RECEIVED = "on_sms_received"
CONF_ON_USSD_RECEIVED = "on_ussd_received"
@@ -90,21 +81,14 @@ SIM800L_SEND_SMS_SCHEMA = cv.Schema(
)
@automation.register_action(
automation.register_apply_action(
"sim800l.send_sms",
Sim800LSendSmsAction,
SIM800L_SEND_SMS_SCHEMA,
synchronous=True,
automation.ApplyCall(
"send_sms({}, {})",
((CONF_RECIPIENT, cg.std_string), (CONF_MESSAGE, cg.std_string)),
),
)
async def sim800l_send_sms_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_RECIPIENT], args, cg.std_string)
cg.add(var.set_recipient(template_))
template_ = await cg.templatable(config[CONF_MESSAGE], args, cg.std_string)
cg.add(var.set_message(template_))
return var
SIM800L_DIAL_SCHEMA = cv.Schema(
{
@@ -113,25 +97,11 @@ SIM800L_DIAL_SCHEMA = cv.Schema(
}
)
@automation.register_action(
"sim800l.dial", Sim800LDialAction, SIM800L_DIAL_SCHEMA, synchronous=True
automation.register_apply_action(
"sim800l.dial",
SIM800L_DIAL_SCHEMA,
automation.ApplyField(CONF_RECIPIENT, "dial", cg.std_string),
)
async def sim800l_dial_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_RECIPIENT], args, cg.std_string)
cg.add(var.set_recipient(template_))
return var
automation.register_simple_action(
"sim800l.connect",
Sim800LConnectAction,
cv.Schema({cv.GenerateID(): cv.use_id(Sim800LComponent)}),
synchronous=True,
)
SIM800L_SEND_USSD_SCHEMA = cv.Schema(
{
@@ -140,24 +110,17 @@ SIM800L_SEND_USSD_SCHEMA = cv.Schema(
}
)
@automation.register_action(
automation.register_apply_action(
"sim800l.send_ussd",
Sim800LSendUssdAction,
SIM800L_SEND_USSD_SCHEMA,
synchronous=True,
automation.ApplyField(CONF_USSD, "send_ussd", cg.std_string),
)
async def sim800l_send_ussd_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_USSD], args, cg.std_string)
cg.add(var.set_ussd(template_))
return var
SIM800L_ID_SCHEMA = cv.Schema({cv.GenerateID(): cv.use_id(Sim800LComponent)})
automation.register_simple_action(
"sim800l.disconnect",
Sim800LDisconnectAction,
cv.Schema({cv.GenerateID(): cv.use_id(Sim800LComponent)}),
synchronous=True,
automation.register_apply_action(
"sim800l.connect", SIM800L_ID_SCHEMA, automation.ApplyCall("connect()")
)
automation.register_apply_action(
"sim800l.disconnect", SIM800L_ID_SCHEMA, automation.ApplyCall("disconnect()")
)
-63
View File
@@ -120,67 +120,4 @@ class Sim800LComponent final : public uart::UARTDevice, public PollingComponent
CallbackManager<void(std::string)> ussd_received_callback_;
};
template<typename... Ts> class Sim800LSendSmsAction final : public Action<Ts...> {
public:
Sim800LSendSmsAction(Sim800LComponent *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(std::string, recipient)
TEMPLATABLE_VALUE(std::string, message)
void play(const Ts &...x) {
auto recipient = this->recipient_.value(x...);
auto message = this->message_.value(x...);
this->parent_->send_sms(recipient, message);
}
protected:
Sim800LComponent *parent_;
};
template<typename... Ts> class Sim800LSendUssdAction final : public Action<Ts...> {
public:
Sim800LSendUssdAction(Sim800LComponent *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(std::string, ussd)
void play(const Ts &...x) {
auto ussd_code = this->ussd_.value(x...);
this->parent_->send_ussd(ussd_code);
}
protected:
Sim800LComponent *parent_;
};
template<typename... Ts> class Sim800LDialAction final : public Action<Ts...> {
public:
Sim800LDialAction(Sim800LComponent *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(std::string, recipient)
void play(const Ts &...x) {
auto recipient = this->recipient_.value(x...);
this->parent_->dial(recipient);
}
protected:
Sim800LComponent *parent_;
};
template<typename... Ts> class Sim800LConnectAction final : public Action<Ts...> {
public:
Sim800LConnectAction(Sim800LComponent *parent) : parent_(parent) {}
void play(const Ts &...x) { this->parent_->connect(); }
protected:
Sim800LComponent *parent_;
};
template<typename... Ts> class Sim800LDisconnectAction final : public Action<Ts...> {
public:
Sim800LDisconnectAction(Sim800LComponent *parent) : parent_(parent) {}
void play(const Ts &...x) { this->parent_->disconnect(); }
protected:
Sim800LComponent *parent_;
};
} // namespace esphome::sim800l