1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-02 03:12:20 +01:00

Automatically disable interrupts for ESP8266 GPIO16 binary sensors (#9467)

This commit is contained in:
J. Nick Koston
2025-07-12 22:09:55 -10:00
committed by GitHub
parent 6f19808eff
commit 84956b6dc5
5 changed files with 136 additions and 3 deletions

View File

@@ -0,0 +1,69 @@
"""Tests for the GPIO binary sensor component."""
from __future__ import annotations
from collections.abc import Callable
from pathlib import Path
import pytest
def test_gpio_binary_sensor_basic_setup(
generate_main: Callable[[str | Path], str],
) -> None:
"""
When the GPIO binary sensor is set in the yaml file, it should be registered in main
"""
main_cpp = generate_main("tests/component_tests/gpio/test_gpio_binary_sensor.yaml")
assert "new gpio::GPIOBinarySensor();" in main_cpp
assert "App.register_binary_sensor" in main_cpp
assert "bs_gpio->set_use_interrupt(true);" in main_cpp
assert "bs_gpio->set_interrupt_type(gpio::INTERRUPT_ANY_EDGE);" in main_cpp
def test_gpio_binary_sensor_esp8266_gpio16_disables_interrupt(
generate_main: Callable[[str | Path], str],
caplog: pytest.LogCaptureFixture,
) -> None:
"""
Test that ESP8266 GPIO16 automatically disables interrupt mode with a warning
"""
main_cpp = generate_main(
"tests/component_tests/gpio/test_gpio_binary_sensor_esp8266.yaml"
)
# Check that interrupt is disabled for GPIO16
assert "bs_gpio16->set_use_interrupt(false);" in main_cpp
# Check that the warning was logged
assert "GPIO16 on ESP8266 doesn't support interrupts" in caplog.text
assert "Falling back to polling mode" in caplog.text
def test_gpio_binary_sensor_esp8266_other_pins_use_interrupt(
generate_main: Callable[[str | Path], str],
) -> None:
"""
Test that ESP8266 pins other than GPIO16 still use interrupt mode
"""
main_cpp = generate_main(
"tests/component_tests/gpio/test_gpio_binary_sensor_esp8266.yaml"
)
# GPIO5 should still use interrupts
assert "bs_gpio5->set_use_interrupt(true);" in main_cpp
assert "bs_gpio5->set_interrupt_type(gpio::INTERRUPT_ANY_EDGE);" in main_cpp
def test_gpio_binary_sensor_explicit_polling_mode(
generate_main: Callable[[str | Path], str],
) -> None:
"""
Test that explicitly setting use_interrupt: false works
"""
main_cpp = generate_main(
"tests/component_tests/gpio/test_gpio_binary_sensor_polling.yaml"
)
assert "bs_polling->set_use_interrupt(false);" in main_cpp

View File

@@ -0,0 +1,11 @@
esphome:
name: test
esp32:
board: esp32dev
binary_sensor:
- platform: gpio
pin: 5
name: "Test GPIO Binary Sensor"
id: bs_gpio

View File

@@ -0,0 +1,20 @@
esphome:
name: test
esp8266:
board: d1_mini
binary_sensor:
- platform: gpio
pin:
number: 16
mode: INPUT_PULLDOWN_16
name: "GPIO16 Touch Sensor"
id: bs_gpio16
- platform: gpio
pin:
number: 5
mode: INPUT_PULLUP
name: "GPIO5 Button"
id: bs_gpio5

View File

@@ -0,0 +1,12 @@
esphome:
name: test
esp32:
board: esp32dev
binary_sensor:
- platform: gpio
pin: 5
name: "Polling Mode Sensor"
id: bs_polling
use_interrupt: false