mirror of
https://github.com/esphome/esphome.git
synced 2025-09-06 13:22:19 +01: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:
@@ -37,6 +37,27 @@ template<typename... Ts> class ToggleAction : public Action<Ts...> {
|
||||
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...> {
|
||||
public:
|
||||
SwitchCondition(Switch *parent, bool state) : parent_(parent), state_(state) {}
|
||||
|
Reference in New Issue
Block a user