mirror of
https://github.com/esphome/esphome.git
synced 2026-09-27 15:00:24 +00:00
[switch] Use register_apply_action for the switch actions (#19498)
This commit is contained in:
@@ -54,12 +54,6 @@ RESTORE_MODES = {
|
||||
}
|
||||
|
||||
|
||||
ControlAction = switch_ns.class_("ControlAction", automation.Action)
|
||||
ToggleAction = switch_ns.class_("ToggleAction", automation.Action)
|
||||
TurnOffAction = switch_ns.class_("TurnOffAction", automation.Action)
|
||||
TurnOnAction = switch_ns.class_("TurnOnAction", automation.Action)
|
||||
SwitchPublishAction = switch_ns.class_("SwitchPublishAction", automation.Action)
|
||||
|
||||
SwitchCondition = switch_ns.class_("SwitchCondition", Condition)
|
||||
validate_device_class = cv.one_of(*DEVICE_CLASSES, lower=True)
|
||||
|
||||
@@ -193,29 +187,19 @@ SWITCH_CONTROL_ACTION_SCHEMA = automation.maybe_simple_id(
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"switch.control", ControlAction, SWITCH_CONTROL_ACTION_SCHEMA, synchronous=True
|
||||
automation.register_apply_action(
|
||||
"switch.control",
|
||||
SWITCH_CONTROL_ACTION_SCHEMA,
|
||||
automation.ApplyField(CONF_STATE, "control", cg.bool_),
|
||||
)
|
||||
async def switch_control_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_STATE], args, cg.bool_)
|
||||
cg.add(var.set_state(template_))
|
||||
return var
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"switch.toggle", ToggleAction, SWITCH_ACTION_SCHEMA, synchronous=True
|
||||
)
|
||||
@automation.register_action(
|
||||
"switch.turn_off", TurnOffAction, SWITCH_ACTION_SCHEMA, synchronous=True
|
||||
)
|
||||
@automation.register_action(
|
||||
"switch.turn_on", TurnOnAction, SWITCH_ACTION_SCHEMA, synchronous=True
|
||||
)
|
||||
async def switch_toggle_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)
|
||||
for _name, _call in (
|
||||
("switch.toggle", "toggle()"),
|
||||
("switch.turn_off", "turn_off()"),
|
||||
("switch.turn_on", "turn_on()"),
|
||||
):
|
||||
automation.register_apply_action(
|
||||
_name, SWITCH_ACTION_SCHEMA, automation.ApplyCall(_call)
|
||||
)
|
||||
|
||||
|
||||
@automation.register_condition("switch.is_on", SwitchCondition, SWITCH_ACTION_SCHEMA)
|
||||
|
||||
@@ -6,53 +6,6 @@
|
||||
|
||||
namespace esphome::switch_ {
|
||||
|
||||
template<typename... Ts> class TurnOnAction final : public Action<Ts...> {
|
||||
public:
|
||||
explicit TurnOnAction(Switch *a_switch) : switch_(a_switch) {}
|
||||
|
||||
void play(const Ts &...x) override { this->switch_->turn_on(); }
|
||||
|
||||
protected:
|
||||
Switch *switch_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class TurnOffAction final : public Action<Ts...> {
|
||||
public:
|
||||
explicit TurnOffAction(Switch *a_switch) : switch_(a_switch) {}
|
||||
|
||||
void play(const Ts &...x) override { this->switch_->turn_off(); }
|
||||
|
||||
protected:
|
||||
Switch *switch_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class ToggleAction final : public Action<Ts...> {
|
||||
public:
|
||||
explicit ToggleAction(Switch *a_switch) : switch_(a_switch) {}
|
||||
|
||||
void play(const Ts &...x) override { this->switch_->toggle(); }
|
||||
|
||||
protected:
|
||||
Switch *switch_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class ControlAction final : public Action<Ts...> {
|
||||
public:
|
||||
explicit ControlAction(Switch *a_switch) : switch_(a_switch) {}
|
||||
|
||||
TEMPLATABLE_VALUE(bool, state)
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
auto state = this->state_.optional_value(x...);
|
||||
if (state.has_value()) {
|
||||
this->switch_->control(*state);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
Switch *switch_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class SwitchCondition final : public Condition<Ts...> {
|
||||
public:
|
||||
SwitchCondition(Switch *parent, bool state) : parent_(parent), state_(state) {}
|
||||
@@ -92,15 +45,4 @@ class SwitchTurnOffTrigger final : public Trigger<> {
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts> class SwitchPublishAction final : public Action<Ts...> {
|
||||
public:
|
||||
SwitchPublishAction(Switch *a_switch) : switch_(a_switch) {}
|
||||
TEMPLATABLE_VALUE(bool, state)
|
||||
|
||||
void play(const Ts &...x) override { this->switch_->publish_state(this->state_.value(x...)); }
|
||||
|
||||
protected:
|
||||
Switch *switch_;
|
||||
};
|
||||
|
||||
} // namespace esphome::switch_
|
||||
|
||||
@@ -79,20 +79,13 @@ async def to_code(config):
|
||||
cg.add(var.set_assumed_state(True))
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"switch.template.publish",
|
||||
switch.SwitchPublishAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(switch.Switch),
|
||||
cv.Required(CONF_STATE): cv.templatable(cv.boolean),
|
||||
}
|
||||
),
|
||||
synchronous=True,
|
||||
automation.ApplyField(CONF_STATE, "publish_state", cg.bool_),
|
||||
)
|
||||
async def switch_template_publish_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_STATE], args, cg.bool_)
|
||||
cg.add(var.set_state(template_))
|
||||
return var
|
||||
|
||||
@@ -257,8 +257,14 @@ switch:
|
||||
return false;
|
||||
turn_on_action:
|
||||
- logger.log: "turn_on_action"
|
||||
- switch.template.publish:
|
||||
id: test_switch
|
||||
state: true
|
||||
turn_off_action:
|
||||
- logger.log: "turn_off_action"
|
||||
- switch.template.publish:
|
||||
id: test_switch
|
||||
state: !lambda return false;
|
||||
|
||||
button:
|
||||
- platform: template
|
||||
|
||||
Reference in New Issue
Block a user