ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
template_switch.cpp
Go to the documentation of this file.
1#include "template_switch.h"
2#include "esphome/core/log.h"
3
4namespace esphome::template_ {
5
6ESPHOME_LOG_TAG(TAG, "template.switch");
7
9
11 auto s = this->f_();
12 if (s.has_value()) {
13 this->publish_state(*s);
14 }
15}
17 if (this->prev_trigger_ != nullptr) {
19 }
20
21 if (state) {
22 this->prev_trigger_ = &this->turn_on_trigger_;
24 } else {
25 this->prev_trigger_ = &this->turn_off_trigger_;
27 }
28
29 if (this->optimistic_)
30 this->publish_state(state);
31}
37 if (!this->f_.has_value())
38 this->disable_loop();
39
40 this->restore();
41}
43 LOG_SWITCH("", "Template Switch", this);
44 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
45}
46
47} // namespace esphome::template_
void disable_loop()
Disable this component's loop.
bool has_value() const
Check if a lambda is set.
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:564
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:556
void restore()
Drive the switch to the state its restore mode yields, through control(), so inversion and write_stat...
Definition switch.cpp:63
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:70
float get_setup_priority() const override
void write_state(bool state) override
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "alarm_control_panel")
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43