From 5d2f454a94b010be179baa0f495be1601808748c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 17 Jun 2025 13:13:58 +0200 Subject: [PATCH] Avoid polling for GPIO binary sensors when possible --- .../gpio/binary_sensor/gpio_binary_sensor.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/esphome/components/gpio/binary_sensor/gpio_binary_sensor.cpp b/esphome/components/gpio/binary_sensor/gpio_binary_sensor.cpp index 43e5a9d0e1..ef99863845 100644 --- a/esphome/components/gpio/binary_sensor/gpio_binary_sensor.cpp +++ b/esphome/components/gpio/binary_sensor/gpio_binary_sensor.cpp @@ -19,11 +19,12 @@ void GPIOBinarySensorStore::setup(InternalGPIOPin *pin, gpio::InterruptType type this->pin_ = pin; pin->setup(); this->isr_pin_ = pin->to_isr(); - { - InterruptLock lock; - this->last_state_ = pin->digital_read(); - this->state_ = this->last_state_; - } + + // Read initial state + this->last_state_ = pin->digital_read(); + this->state_ = this->last_state_; + + // Attach interrupt - from this point on, any changes will be caught by the interrupt pin->attach_interrupt(&GPIOBinarySensorStore::gpio_intr, this, type); }