ESPHome 2026.5.1
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::gpio {
5
6static const char *const TAG = "switch.gpio";
7#ifdef USE_GPIO_SWITCH_INTERLOCK
8static constexpr uint32_t INTERLOCK_TIMEOUT_ID = 0;
9#endif
10
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#ifdef USE_GPIO_SWITCH_INTERLOCK
33 if (!this->interlock_.empty()) {
34 ESP_LOGCONFIG(TAG, " Interlocks:");
35 for (auto *lock : this->interlock_) {
36 if (lock == this)
37 continue;
38 ESP_LOGCONFIG(TAG, " %s", lock->get_name().c_str());
39 }
40 }
41#endif
42}
44#ifdef USE_GPIO_SWITCH_INTERLOCK
45 if (state != this->inverted_) {
46 // Turning ON, check interlocking
47
48 bool found = false;
49 for (auto *lock : this->interlock_) {
50 if (lock == this)
51 continue;
52
53 if (lock->state) {
54 lock->turn_off();
55 found = true;
56 }
57 }
58 if (found && this->interlock_wait_time_ != 0) {
59 this->set_timeout(INTERLOCK_TIMEOUT_ID, this->interlock_wait_time_, [this, state] {
60 // Don't write directly, call the function again
61 // (some other switch may have changed state while we were waiting)
62 this->write_state(state);
63 });
64 return;
65 }
66 } else if (this->interlock_wait_time_ != 0) {
67 // If we are switched off during the interlock wait time, cancel any pending
68 // re-activations
69 this->cancel_timeout(INTERLOCK_TIMEOUT_ID);
70 }
71#endif
72
73 this->pin_->digital_write(state);
74 this->publish_state(state);
75}
76
77#ifdef USE_GPIO_SWITCH_INTERLOCK
78void GPIOSwitch::set_interlock(const std::initializer_list<Switch *> &interlock) { this->interlock_ = interlock; }
79#endif
80
81} // namespace esphome::gpio
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_timeout(const std boo cancel_timeout)(const char *name)
Cancel a timeout function.
Definition component.h:532
bool empty() const
Definition helpers.h:687
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::initializer_list< Switch * > &interlock)
void write_state(bool state) override
FixedVector< Switch * > interlock_
Definition gpio_switch.h:30
void turn_on()
Turn this switch on.
Definition switch.cpp:20
void turn_off()
Turn this switch off.
Definition switch.cpp:24
bool state
The current reported state of the binary sensor.
Definition switch.h:55
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:56
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition switch.cpp:42
bool state
Definition fan.h:2
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:41
static void uint32_t