1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-15 01:32:19 +01:00

Skip gpio validation (#5615)

This commit is contained in:
Andrew McFague
2024-05-15 16:49:04 -07:00
committed by GitHub
parent 7d804bf90f
commit f0ec900e48
5 changed files with 54 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
from dataclasses import dataclass
from typing import Any
import logging
from esphome.const import (
CONF_ID,
@@ -8,6 +9,7 @@ from esphome.const import (
CONF_NUMBER,
CONF_OPEN_DRAIN,
CONF_OUTPUT,
CONF_IGNORE_PIN_VALIDATION_ERROR,
CONF_IGNORE_STRAPPING_WARNING,
PLATFORM_ESP32,
)
@@ -42,6 +44,9 @@ from .gpio_esp32_h2 import esp32_h2_validate_gpio_pin, esp32_h2_validate_support
ESP32InternalGPIOPin = esp32_ns.class_("ESP32InternalGPIOPin", cg.InternalGPIOPin)
_LOGGER = logging.getLogger(__name__)
def _lookup_pin(value):
board = CORE.data[KEY_ESP32][KEY_BOARD]
board_pins = boards.ESP32_BOARD_PINS.get(board, {})
@@ -111,7 +116,7 @@ _esp32_validations = {
}
def validate_gpio_pin(value):
def gpio_pin_number_validator(value):
value = _translate_pin(value)
board = CORE.data[KEY_ESP32][KEY_BOARD]
board_pins = boards.ESP32_BOARD_PINS.get(board, {})
@@ -127,7 +132,33 @@ def validate_gpio_pin(value):
if variant not in _esp32_validations:
raise cv.Invalid(f"Unsupported ESP32 variant {variant}")
return _esp32_validations[variant].pin_validation(value)
return value
def validate_gpio_pin(pin):
variant = CORE.data[KEY_ESP32][KEY_VARIANT]
if variant not in _esp32_validations:
raise cv.Invalid(f"Unsupported ESP32 variant {variant}")
ignore_pin_validation_warning = pin[CONF_IGNORE_PIN_VALIDATION_ERROR]
try:
pin[CONF_NUMBER] = _esp32_validations[variant].pin_validation(pin[CONF_NUMBER])
except cv.Invalid as exc:
if not ignore_pin_validation_warning:
raise
_LOGGER.warning(
"Ignoring validation error on pin %d; error: %s",
pin[CONF_NUMBER],
exc,
)
else:
# Throw an exception if used for a pin that would not have resulted
# in a validation error anyway!
if ignore_pin_validation_warning:
raise cv.Invalid(f"GPIO{pin[CONF_NUMBER]} is not a reserved pin")
return pin
def validate_supports(value):
@@ -158,9 +189,11 @@ DRIVE_STRENGTHS = {
gpio_num_t = cg.global_ns.enum("gpio_num_t")
CONF_DRIVE_STRENGTH = "drive_strength"
ESP32_PIN_SCHEMA = cv.All(
pins.gpio_base_schema(ESP32InternalGPIOPin, validate_gpio_pin).extend(
pins.gpio_base_schema(ESP32InternalGPIOPin, gpio_pin_number_validator).extend(
{
cv.Optional(CONF_IGNORE_PIN_VALIDATION_ERROR, default=False): cv.boolean,
cv.Optional(CONF_IGNORE_STRAPPING_WARNING, default=False): cv.boolean,
cv.Optional(CONF_DRIVE_STRENGTH, default="20mA"): cv.All(
cv.float_with_unit("current", "mA", optional_unit=True),
@@ -168,6 +201,7 @@ ESP32_PIN_SCHEMA = cv.All(
),
}
),
validate_gpio_pin,
validate_supports,
)