ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
gpio_switch.cpp
Go to the documentation of this file.
1#include "gpio_switch.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace gpio {
6
7static const char *const TAG = "switch.gpio";
8
11 bool initial_state = this->get_initial_state_with_restore_mode().value_or(false);
12
13 // write state before setup
14 if (initial_state) {
15 this->turn_on();
16 } else {
17 this->turn_off();
18 }
19 this->pin_->setup();
20 // write after setup again for other IOs
21 if (initial_state) {
22 this->turn_on();
23 } else {
24 this->turn_off();
25 }
26}
28 LOG_SWITCH("", "GPIO Switch", this);
29 LOG_PIN(" Pin: ", this->pin_);
30 if (!this->interlock_.empty()) {
31 ESP_LOGCONFIG(TAG, " Interlocks:");
32 for (auto *lock : this->interlock_) {
33 if (lock == this)
34 continue;
35 ESP_LOGCONFIG(TAG, " %s", lock->get_name().c_str());
36 }
37 }
38}
40 if (state != this->inverted_) {
41 // Turning ON, check interlocking
42
43 bool found = false;
44 for (auto *lock : this->interlock_) {
45 if (lock == this)
46 continue;
47
48 if (lock->state) {
49 lock->turn_off();
50 found = true;
51 }
52 }
53 if (found && this->interlock_wait_time_ != 0) {
54 this->set_timeout("interlock", this->interlock_wait_time_, [this, state] {
55 // Don't write directly, call the function again
56 // (some other switch may have changed state while we were waiting)
57 this->write_state(state);
58 });
59 return;
60 }
61 } else if (this->interlock_wait_time_ != 0) {
62 // If we are switched off during the interlock wait time, cancel any pending
63 // re-activations
64 this->cancel_timeout("interlock");
65 }
66
67 this->pin_->digital_write(state);
68 this->publish_state(state);
69}
70void GPIOSwitch::set_interlock(const std::vector<Switch *> &interlock) { this->interlock_ = interlock; }
71
72} // namespace gpio
73} // namespace esphome
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
virtual void setup()=0
virtual void digital_write(bool value)=0
void dump_config() override
float get_setup_priority() const override
void set_interlock(const std::vector< Switch * > &interlock)
void write_state(bool state) override
std::vector< Switch * > interlock_
Definition gpio_switch.h:29
value_type value_or(U const &v) const
Definition optional.h:98
void turn_on()
Turn this switch on.
Definition switch.cpp:19
void turn_off()
Turn this switch off.
Definition switch.cpp:23
bool state
The current reported state of the binary sensor.
Definition switch.h:56
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:55
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition switch.cpp:41
bool state
Definition fan.h:0
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:49
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7