ESPHome 2026.5.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#ifdef USE_GPIO_SWITCH_INTERLOCK
9static constexpr uint32_t INTERLOCK_TIMEOUT_ID = 0;
10#endif
11
14 bool initial_state = this->get_initial_state_with_restore_mode().value_or(false);
15
16 // write state before setup
17 if (initial_state) {
18 this->turn_on();
19 } else {
20 this->turn_off();
21 }
22 this->pin_->setup();
23 // write after setup again for other IOs
24 if (initial_state) {
25 this->turn_on();
26 } else {
27 this->turn_off();
28 }
29}
31 LOG_SWITCH("", "GPIO Switch", this);
32 LOG_PIN(" Pin: ", this->pin_);
33#ifdef USE_GPIO_SWITCH_INTERLOCK
34 if (!this->interlock_.empty()) {
35 ESP_LOGCONFIG(TAG, " Interlocks:");
36 for (auto *lock : this->interlock_) {
37 if (lock == this)
38 continue;
39 ESP_LOGCONFIG(TAG, " %s", lock->get_name().c_str());
40 }
41 }
42#endif
43}
45#ifdef USE_GPIO_SWITCH_INTERLOCK
46 if (state != this->inverted_) {
47 // Turning ON, check interlocking
48
49 bool found = false;
50 for (auto *lock : this->interlock_) {
51 if (lock == this)
52 continue;
53
54 if (lock->state) {
55 lock->turn_off();
56 found = true;
57 }
58 }
59 if (found && this->interlock_wait_time_ != 0) {
60 this->set_timeout(INTERLOCK_TIMEOUT_ID, this->interlock_wait_time_, [this, state] {
61 // Don't write directly, call the function again
62 // (some other switch may have changed state while we were waiting)
63 this->write_state(state);
64 });
65 return;
66 }
67 } else if (this->interlock_wait_time_ != 0) {
68 // If we are switched off during the interlock wait time, cancel any pending
69 // re-activations
70 this->cancel_timeout(INTERLOCK_TIMEOUT_ID);
71 }
72#endif
73
74 this->pin_->digital_write(state);
75 this->publish_state(state);
76}
77
78#ifdef USE_GPIO_SWITCH_INTERLOCK
79void GPIOSwitch::set_interlock(const std::initializer_list<Switch *> &interlock) { this->interlock_ = interlock; }
80#endif
81
82} // namespace gpio
83} // namespace esphome
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:680
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:31
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:40
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t