ESPHome  2022.8.0
copy_switch.cpp
Go to the documentation of this file.
1 #include "copy_switch.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace copy {
6 
7 static const char *const TAG = "copy.switch";
8 
10  source_->add_on_state_callback([this](float value) { this->publish_state(value); });
11 
12  this->publish_state(source_->state);
13 }
14 
15 void CopySwitch::dump_config() { LOG_SWITCH("", "Copy Switch", this); }
16 
18  if (state) {
19  source_->turn_on();
20  } else {
21  source_->turn_off();
22  }
23 }
24 
25 } // namespace copy
26 } // namespace esphome
switch_::Switch * source_
Definition: copy_switch.h:19
void dump_config() override
Definition: copy_switch.cpp:15
void setup() override
Definition: copy_switch.cpp:9
void write_state(bool state) override
Definition: copy_switch.cpp:17
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition: switch.cpp:42
void turn_on()
Turn this switch on.
Definition: switch.cpp:12
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
void turn_off()
Turn this switch off.
Definition: switch.cpp:16