1
0
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:
Edward Firmo
2025-08-08 00:55:54 +02:00
committed by GitHub
parent 14bc83342f
commit c4d1b1317a
3 changed files with 48 additions and 0 deletions

View File

@@ -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) {}