ESPHome  2022.8.0
output_switch.cpp
Go to the documentation of this file.
1 #include "output_switch.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace output {
6 
7 static const char *const TAG = "output.switch";
8 
9 void OutputSwitch::dump_config() { LOG_SWITCH("", "Output Switch", this); }
11  bool initial_state = false;
12  switch (this->restore_mode_) {
14  initial_state = this->get_initial_state().value_or(false);
15  break;
17  initial_state = this->get_initial_state().value_or(true);
18  break;
20  initial_state = !this->get_initial_state().value_or(true);
21  break;
23  initial_state = !this->get_initial_state().value_or(false);
24  break;
26  initial_state = false;
27  break;
29  initial_state = true;
30  break;
31  }
32 
33  if (initial_state) {
34  this->turn_on();
35  } else {
36  this->turn_off();
37  }
38 }
40  if (state) {
41  this->output_->turn_on();
42  } else {
43  this->output_->turn_off();
44  }
45  this->publish_state(state);
46 }
47 
48 } // namespace output
49 } // namespace esphome
virtual void turn_on()
Enable this binary output.
Definition: binary_output.h:43
output::BinaryOutput * output_
Definition: output_switch.h:32
virtual void turn_off()
Disable this binary output.
Definition: binary_output.h:51
optional< bool > get_initial_state()
Definition: switch.cpp:24
void write_state(bool state) override
void turn_on()
Turn this switch on.
Definition: switch.cpp:12
OutputSwitchRestoreMode restore_mode_
Definition: output_switch.h:33
Definition: a4988.cpp:4
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition: switch.cpp:31
bool state
The current reported state of the binary sensor.
Definition: switch.h:48
value_type value_or(U const &v) const
Definition: optional.h:93
void turn_off()
Turn this switch off.
Definition: switch.cpp:16