Files
esphome-dev/esphome/components/switch/automation.h
T
Andrew Zaborowski a62b6548d2 Make some Action methods protected
Apparently play()/stop() etc. are not meant to be called directly by
users of the class and if they're called directly that would not give
the expected result for the classes that have an empty play().

Make all methods except play_complex, stop_comples and is_running
protected.  While there also make RemoteTransmitterActionBase::encode
protected.
2020-05-01 12:44:30 +02:00

84 lines
2.0 KiB
C++

#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/switch/switch.h"
namespace esphome {
namespace switch_ {
template<typename... Ts> class TurnOnAction : public Action<Ts...> {
public:
explicit TurnOnAction(Switch *a_switch) : switch_(a_switch) {}
protected:
void play_(Ts... x) override { this->switch_->turn_on(); }
Switch *switch_;
};
template<typename... Ts> class TurnOffAction : public Action<Ts...> {
public:
explicit TurnOffAction(Switch *a_switch) : switch_(a_switch) {}
protected:
void play_(Ts... x) override { this->switch_->turn_off(); }
Switch *switch_;
};
template<typename... Ts> class ToggleAction : public Action<Ts...> {
public:
explicit ToggleAction(Switch *a_switch) : switch_(a_switch) {}
protected:
void play_(Ts... x) override { this->switch_->toggle(); }
Switch *switch_;
};
template<typename... Ts> class SwitchCondition : public Condition<Ts...> {
public:
SwitchCondition(Switch *parent, bool state) : parent_(parent), state_(state) {}
bool check(Ts... x) override { return this->parent_->state == this->state_; }
protected:
Switch *parent_;
bool state_;
};
class SwitchTurnOnTrigger : public Trigger<> {
public:
SwitchTurnOnTrigger(Switch *a_switch) {
a_switch->add_on_state_callback([this](bool state) {
if (state) {
this->trigger();
}
});
}
};
class SwitchTurnOffTrigger : public Trigger<> {
public:
SwitchTurnOffTrigger(Switch *a_switch) {
a_switch->add_on_state_callback([this](bool state) {
if (!state) {
this->trigger();
}
});
}
};
template<typename... Ts> class SwitchPublishAction : public Action<Ts...> {
public:
SwitchPublishAction(Switch *a_switch) : switch_(a_switch) {}
TEMPLATABLE_VALUE(bool, state)
protected:
void play_(Ts... x) override { this->switch_->publish_state(this->state_.value(x...)); }
Switch *switch_;
};
} // namespace switch_
} // namespace esphome