ESPHome  2022.8.0
gpio_switch.cpp
Go to the documentation of this file.
1 #include "gpio_switch.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace gpio {
6 
7 static const char *const TAG = "switch.gpio";
8 
11  ESP_LOGCONFIG(TAG, "Setting up GPIO Switch '%s'...", this->name_.c_str());
12 
13  bool initial_state = false;
14  switch (this->restore_mode_) {
16  initial_state = this->get_initial_state().value_or(false);
17  break;
19  initial_state = this->get_initial_state().value_or(true);
20  break;
22  initial_state = !this->get_initial_state().value_or(true);
23  break;
25  initial_state = !this->get_initial_state().value_or(false);
26  break;
28  initial_state = false;
29  break;
31  initial_state = true;
32  break;
33  }
34 
35  // write state before setup
36  if (initial_state) {
37  this->turn_on();
38  } else {
39  this->turn_off();
40  }
41  this->pin_->setup();
42  // write after setup again for other IOs
43  if (initial_state) {
44  this->turn_on();
45  } else {
46  this->turn_off();
47  }
48 }
50  LOG_SWITCH("", "GPIO Switch", this);
51  LOG_PIN(" Pin: ", this->pin_);
52  const LogString *restore_mode = LOG_STR("");
53  switch (this->restore_mode_) {
55  restore_mode = LOG_STR("Restore (Defaults to OFF)");
56  break;
58  restore_mode = LOG_STR("Restore (Defaults to ON)");
59  break;
61  restore_mode = LOG_STR("Restore inverted (Defaults to ON)");
62  break;
64  restore_mode = LOG_STR("Restore inverted (Defaults to OFF)");
65  break;
67  restore_mode = LOG_STR("Always OFF");
68  break;
70  restore_mode = LOG_STR("Always ON");
71  break;
72  }
73  ESP_LOGCONFIG(TAG, " Restore Mode: %s", LOG_STR_ARG(restore_mode));
74  if (!this->interlock_.empty()) {
75  ESP_LOGCONFIG(TAG, " Interlocks:");
76  for (auto *lock : this->interlock_) {
77  if (lock == this)
78  continue;
79  ESP_LOGCONFIG(TAG, " %s", lock->get_name().c_str());
80  }
81  }
82 }
84  if (state != this->inverted_) {
85  // Turning ON, check interlocking
86 
87  bool found = false;
88  for (auto *lock : this->interlock_) {
89  if (lock == this)
90  continue;
91 
92  if (lock->state) {
93  lock->turn_off();
94  found = true;
95  }
96  }
97  if (found && this->interlock_wait_time_ != 0) {
98  this->set_timeout("interlock", this->interlock_wait_time_, [this, state] {
99  // Don't write directly, call the function again
100  // (some other switch may have changed state while we were waiting)
101  this->write_state(state);
102  });
103  return;
104  }
105  } else if (this->interlock_wait_time_ != 0) {
106  // If we are switched off during the interlock wait time, cancel any pending
107  // re-activations
108  this->cancel_timeout("interlock");
109  }
110 
111  this->pin_->digital_write(state);
112  this->publish_state(state);
113 }
114 void GPIOSwitch::set_restore_mode(GPIOSwitchRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
115 void GPIOSwitch::set_interlock(const std::vector<Switch *> &interlock) { this->interlock_ = interlock; }
116 
117 } // namespace gpio
118 } // namespace esphome
virtual void digital_write(bool value)=0
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
Definition: component.cpp:71
void dump_config() override
Definition: gpio_switch.cpp:49
std::string name_
Definition: entity_base.h:54
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:67
virtual void setup()=0
float get_setup_priority() const override
Definition: gpio_switch.cpp:9
optional< bool > get_initial_state()
Definition: switch.cpp:24
void set_restore_mode(GPIOSwitchRestoreMode restore_mode)
void turn_on()
Turn this switch on.
Definition: switch.cpp:12
void set_interlock(const std::vector< Switch *> &interlock)
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
Definition: a4988.cpp:4
void write_state(bool state) override
Definition: gpio_switch.cpp:83
GPIOSwitchRestoreMode restore_mode_
Definition: gpio_switch.h:38
std::vector< Switch * > interlock_
Definition: gpio_switch.h:39
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