ESPHome  2024.11.1
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  ESP_LOGCONFIG(TAG, "Setting up Output Switch '%s'...", this->name_.c_str());
12 
13  bool initial_state = this->get_initial_state_with_restore_mode().value_or(false);
14 
15  if (initial_state) {
16  this->turn_on();
17  } else {
18  this->turn_off();
19  }
20 }
22  if (state) {
23  this->output_->turn_on();
24  } else {
25  this->output_->turn_off();
26  }
27  this->publish_state(state);
28 }
29 
30 } // namespace output
31 } // namespace esphome
virtual void turn_on()
Enable this binary output.
Definition: binary_output.h:43
output::BinaryOutput * output_
Definition: output_switch.h:21
virtual void turn_off()
Disable this binary output.
Definition: binary_output.h:51
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition: switch.cpp:33
void write_state(bool state) override
void turn_on()
Turn this switch on.
Definition: switch.cpp:11
constexpr const char * c_str() const
Definition: string_ref.h:68
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition: switch.cpp:47
bool state
The current reported state of the binary sensor.
Definition: switch.h:53
value_type value_or(U const &v) const
Definition: optional.h:93
void turn_off()
Turn this switch off.
Definition: switch.cpp:15