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

[esp32] Fix strapping pin validation for P4 and H2 (#9980)

This commit is contained in:
Clyde Stubbs
2025-08-01 09:48:25 +10:00
committed by GitHub
parent da0c47629a
commit 161f51e1f4
4 changed files with 48 additions and 42 deletions

View File

@@ -0,0 +1,43 @@
"""Tests for mpip_spi configuration validation."""
from collections.abc import Callable, Generator
import pytest
from esphome import config_validation as cv
from esphome.components.esp32 import KEY_ESP32, KEY_VARIANT, VARIANTS
from esphome.components.esp32.gpio import validate_gpio_pin
from esphome.const import CONF_INPUT, CONF_OUTPUT
from esphome.core import CORE
from esphome.pins import gpio_pin_schema
@pytest.fixture
def choose_variant_with_pins() -> Generator[Callable[[list], None]]:
"""
Set the ESP32 variant for the given model based on pins. For ESP32 only since the other platforms
do not have variants.
"""
def chooser(pins: list) -> None:
for v in VARIANTS:
try:
CORE.data[KEY_ESP32][KEY_VARIANT] = v
for pin in pins:
if pin is not None:
pin = gpio_pin_schema(
{
CONF_INPUT: True,
CONF_OUTPUT: True,
},
internal=True,
)(pin)
validate_gpio_pin(pin)
return
except cv.Invalid:
continue
raise cv.Invalid(
f"No compatible variant found for pins: {', '.join(map(str, pins))}"
)
yield chooser

View File

@@ -9,13 +9,10 @@ import pytest
from esphome import config_validation as cv
from esphome.components.esp32 import (
KEY_BOARD,
KEY_ESP32,
KEY_VARIANT,
VARIANT_ESP32,
VARIANT_ESP32S3,
VARIANTS,
)
from esphome.components.esp32.gpio import validate_gpio_pin
from esphome.components.mipi import CONF_NATIVE_HEIGHT
from esphome.components.mipi_spi.display import (
CONF_BUS_MODE,
@@ -32,8 +29,6 @@ from esphome.const import (
CONF_WIDTH,
PlatformFramework,
)
from esphome.core import CORE
from esphome.pins import internal_gpio_pin_number
from esphome.types import ConfigType
from tests.component_tests.types import SetCoreConfigCallable
@@ -43,28 +38,6 @@ def run_schema_validation(config: ConfigType) -> None:
FINAL_VALIDATE_SCHEMA(CONFIG_SCHEMA(config))
@pytest.fixture
def choose_variant_with_pins() -> Callable[..., None]:
"""
Set the ESP32 variant for the given model based on pins. For ESP32 only since the other platforms
do not have variants.
"""
def chooser(*pins: int | str | None) -> None:
for v in VARIANTS:
try:
CORE.data[KEY_ESP32][KEY_VARIANT] = v
for pin in pins:
if pin is not None:
pin = internal_gpio_pin_number(pin)
validate_gpio_pin(pin)
return
except cv.Invalid:
continue
return chooser
@pytest.mark.parametrize(
("config", "error_match"),
[
@@ -315,7 +288,7 @@ def test_custom_model_with_all_options(
def test_all_predefined_models(
set_core_config: SetCoreConfigCallable,
set_component_config: Callable[[str, Any], None],
choose_variant_with_pins: Callable[..., None],
choose_variant_with_pins: Callable[[list], None],
) -> None:
"""Test all predefined display models validate successfully with appropriate defaults."""
set_core_config(