mirror of
https://github.com/esphome/esphome.git
synced 2025-08-09 03:47:48 +00:00
[switch] Add switch.control
automation action (#10105)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
parent
14bc83342f
commit
c4d1b1317a
@ -13,6 +13,7 @@ from esphome.const import (
|
|||||||
CONF_ON_TURN_OFF,
|
CONF_ON_TURN_OFF,
|
||||||
CONF_ON_TURN_ON,
|
CONF_ON_TURN_ON,
|
||||||
CONF_RESTORE_MODE,
|
CONF_RESTORE_MODE,
|
||||||
|
CONF_STATE,
|
||||||
CONF_TRIGGER_ID,
|
CONF_TRIGGER_ID,
|
||||||
CONF_WEB_SERVER,
|
CONF_WEB_SERVER,
|
||||||
DEVICE_CLASS_EMPTY,
|
DEVICE_CLASS_EMPTY,
|
||||||
@ -48,6 +49,7 @@ RESTORE_MODES = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ControlAction = switch_ns.class_("ControlAction", automation.Action)
|
||||||
ToggleAction = switch_ns.class_("ToggleAction", automation.Action)
|
ToggleAction = switch_ns.class_("ToggleAction", automation.Action)
|
||||||
TurnOffAction = switch_ns.class_("TurnOffAction", automation.Action)
|
TurnOffAction = switch_ns.class_("TurnOffAction", automation.Action)
|
||||||
TurnOnAction = switch_ns.class_("TurnOnAction", automation.Action)
|
TurnOnAction = switch_ns.class_("TurnOnAction", automation.Action)
|
||||||
@ -177,6 +179,23 @@ SWITCH_ACTION_SCHEMA = maybe_simple_id(
|
|||||||
cv.Required(CONF_ID): cv.use_id(Switch),
|
cv.Required(CONF_ID): cv.use_id(Switch),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
SWITCH_CONTROL_ACTION_SCHEMA = automation.maybe_simple_id(
|
||||||
|
{
|
||||||
|
cv.Required(CONF_ID): cv.use_id(Switch),
|
||||||
|
cv.Required(CONF_STATE): cv.templatable(cv.boolean),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@automation.register_action(
|
||||||
|
"switch.control", ControlAction, SWITCH_CONTROL_ACTION_SCHEMA
|
||||||
|
)
|
||||||
|
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, bool)
|
||||||
|
cg.add(var.set_state(template_))
|
||||||
|
return var
|
||||||
|
|
||||||
|
|
||||||
@automation.register_action("switch.toggle", ToggleAction, SWITCH_ACTION_SCHEMA)
|
@automation.register_action("switch.toggle", ToggleAction, SWITCH_ACTION_SCHEMA)
|
||||||
|
@ -37,6 +37,27 @@ template<typename... Ts> class ToggleAction : public Action<Ts...> {
|
|||||||
Switch *switch_;
|
Switch *switch_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename... Ts> class ControlAction : public Action<Ts...> {
|
||||||
|
public:
|
||||||
|
explicit ControlAction(Switch *a_switch) : switch_(a_switch) {}
|
||||||
|
|
||||||
|
TEMPLATABLE_VALUE(bool, state)
|
||||||
|
|
||||||
|
void play(Ts... x) override {
|
||||||
|
auto state = this->state_.optional_value(x...);
|
||||||
|
if (state.has_value()) {
|
||||||
|
if (*state) {
|
||||||
|
this->switch_->turn_on();
|
||||||
|
} else {
|
||||||
|
this->switch_->turn_off();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Switch *switch_;
|
||||||
|
};
|
||||||
|
|
||||||
template<typename... Ts> class SwitchCondition : public Condition<Ts...> {
|
template<typename... Ts> class SwitchCondition : public Condition<Ts...> {
|
||||||
public:
|
public:
|
||||||
SwitchCondition(Switch *parent, bool state) : parent_(parent), state_(state) {}
|
SwitchCondition(Switch *parent, bool state) : parent_(parent), state_(state) {}
|
||||||
|
@ -9,3 +9,11 @@ switch:
|
|||||||
name: "Template Switch"
|
name: "Template Switch"
|
||||||
id: the_switch
|
id: the_switch
|
||||||
optimistic: true
|
optimistic: true
|
||||||
|
|
||||||
|
esphome:
|
||||||
|
on_boot:
|
||||||
|
- switch.turn_on: the_switch
|
||||||
|
- switch.turn_off: the_switch
|
||||||
|
- switch.control:
|
||||||
|
id: the_switch
|
||||||
|
state: !lambda return (1 > 2);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user