ESPHome 2026.11.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::gpio {
5
6ESPHOME_LOG_TAG(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 unless the pin kept it across the reset
16 if (!this->pin_->is_held())
17 this->control(initial_state);
18 this->pin_->setup();
19 // write after setup again for other IOs
20 this->control(initial_state);
21}
23 LOG_SWITCH("", "GPIO Switch", this);
24 LOG_PIN(" Pin: ", this->pin_);
25#ifdef USE_GPIO_SWITCH_INTERLOCK
26 if (!this->interlock_.empty()) {
27 ESP_LOGCONFIG(TAG, " Interlocks:");
28 for (auto *lock : this->interlock_) {
29 if (lock == this)
30 continue;
31 ESP_LOGCONFIG(TAG, " %s", LOG_STR_ARG(lock->get_log_name()));
32 }
33 }
34#endif
35}
37#ifdef USE_GPIO_SWITCH_INTERLOCK
38 if (state != this->inverted_) {
39 // Turning ON, check interlocking
40
41 bool found = false;
42 for (auto *lock : this->interlock_) {
43 if (lock == this)
44 continue;
45
46 if (lock->state) {
47 lock->turn_off();
48 found = true;
49 }
50 }
51 if (found && this->interlock_wait_time_ != 0) {
52 this->set_timeout(INTERLOCK_TIMEOUT_ID, this->interlock_wait_time_, [this, state] {
53 // Don't write directly, call the function again
54 // (some other switch may have changed state while we were waiting)
55 this->write_state(state);
56 });
57 return;
58 }
59 } else if (this->interlock_wait_time_ != 0) {
60 // If we are switched off during the interlock wait time, cancel any pending
61 // re-activations
62 this->cancel_timeout(INTERLOCK_TIMEOUT_ID);
63 }
64#endif
65
66 this->pin_->digital_write(state);
67 this->publish_state(state);
68}
69
70} // namespace esphome::gpio
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
constexpr bool empty() const
Definition helpers.h:141
virtual void setup()=0
virtual void digital_write(bool value)=0
virtual bool is_held() const
Definition gpio_pin.h:90
void dump_config() override
float get_setup_priority() const override
ConstVector< Switch * > interlock_
Definition gpio_switch.h:36
void write_state(bool state) override
bool state
The current reported state of the binary sensor.
Definition switch.h:60
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:70
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition switch.cpp:49
void control(bool target_state)
Control this switch using a boolean state value.
Definition switch.cpp:12
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "gpio.binary_sensor")
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
static void uint32_t
SemaphoreHandle_t lock