ESPHome  2024.11.1
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 = this->get_initial_state_with_restore_mode().value_or(false);
14 
15  // write state before setup
16  if (initial_state) {
17  this->turn_on();
18  } else {
19  this->turn_off();
20  }
21  this->pin_->setup();
22  // write after setup again for other IOs
23  if (initial_state) {
24  this->turn_on();
25  } else {
26  this->turn_off();
27  }
28 }
30  LOG_SWITCH("", "GPIO Switch", this);
31  LOG_PIN(" Pin: ", this->pin_);
32  if (!this->interlock_.empty()) {
33  ESP_LOGCONFIG(TAG, " Interlocks:");
34  for (auto *lock : this->interlock_) {
35  if (lock == this)
36  continue;
37  ESP_LOGCONFIG(TAG, " %s", lock->get_name().c_str());
38  }
39  }
40 }
42  if (state != this->inverted_) {
43  // Turning ON, check interlocking
44 
45  bool found = false;
46  for (auto *lock : this->interlock_) {
47  if (lock == this)
48  continue;
49 
50  if (lock->state) {
51  lock->turn_off();
52  found = true;
53  }
54  }
55  if (found && this->interlock_wait_time_ != 0) {
56  this->set_timeout("interlock", this->interlock_wait_time_, [this, state] {
57  // Don't write directly, call the function again
58  // (some other switch may have changed state while we were waiting)
59  this->write_state(state);
60  });
61  return;
62  }
63  } else if (this->interlock_wait_time_ != 0) {
64  // If we are switched off during the interlock wait time, cancel any pending
65  // re-activations
66  this->cancel_timeout("interlock");
67  }
68 
69  this->pin_->digital_write(state);
70  this->publish_state(state);
71 }
72 void GPIOSwitch::set_interlock(const std::vector<Switch *> &interlock) { this->interlock_ = interlock; }
73 
74 } // namespace gpio
75 } // namespace esphome
virtual void digital_write(bool value)=0
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
Definition: component.cpp:73
void dump_config() override
Definition: gpio_switch.cpp:29
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:69
virtual void setup()=0
float get_setup_priority() const override
Definition: gpio_switch.cpp:9
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 turn_on()
Turn this switch on.
Definition: switch.cpp:11
void set_interlock(const std::vector< Switch *> &interlock)
Definition: gpio_switch.cpp:72
constexpr const char * c_str() const
Definition: string_ref.h:68
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void write_state(bool state) override
Definition: gpio_switch.cpp:41
std::vector< Switch * > interlock_
Definition: gpio_switch.h:29
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