Skip to content

Commit

Permalink
Skip gpio validation (esphome#5615)
Browse files Browse the repository at this point in the history
  • Loading branch information
amcfague authored May 15, 2024
1 parent 7d804bf commit f0ec900
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
40 changes: 37 additions & 3 deletions esphome/components/esp32/gpio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dataclasses import dataclass
from typing import Any
import logging

from esphome.const import (
CONF_ID,
Expand All @@ -8,6 +9,7 @@
CONF_NUMBER,
CONF_OPEN_DRAIN,
CONF_OUTPUT,
CONF_IGNORE_PIN_VALIDATION_ERROR,
CONF_IGNORE_STRAPPING_WARNING,
PLATFORM_ESP32,
)
Expand Down Expand Up @@ -42,6 +44,9 @@
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, {})
Expand Down Expand Up @@ -111,7 +116,7 @@ class ESP32ValidationFunctions:
}


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, {})
Expand All @@ -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):
Expand Down Expand Up @@ -158,16 +189,19 @@ def validate_supports(value):
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),
cv.enum(DRIVE_STRENGTHS),
),
}
),
validate_gpio_pin,
validate_supports,
)

Expand Down
2 changes: 1 addition & 1 deletion esphome/components/esp32_touch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@


def validate_touch_pad(value):
value = gpio.validate_gpio_pin(value)
value = gpio.gpio_pin_number_validator(value)
variant = get_esp32_variant()
if variant not in TOUCH_PADS:
raise cv.Invalid(f"ESP32 variant {variant} does not support touch pads.")
Expand Down
1 change: 1 addition & 0 deletions esphome/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@
CONF_IF = "if"
CONF_IGNORE_EFUSE_MAC_CRC = "ignore_efuse_mac_crc"
CONF_IGNORE_OUT_OF_RANGE = "ignore_out_of_range"
CONF_IGNORE_PIN_VALIDATION_ERROR = "ignore_pin_validation_error"
CONF_IGNORE_STRAPPING_WARNING = "ignore_strapping_warning"
CONF_IIR_FILTER = "iir_filter"
CONF_ILLUMINANCE = "illuminance"
Expand Down
2 changes: 2 additions & 0 deletions esphome/pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ def _number_validator(value):
cv.Optional(CONF_MODE, default={}): cv.All(mode_dict, mode_validator),
}
)

if invertable:
return schema.extend({cv.Optional(CONF_INVERTED, default=False): cv.boolean})

return schema
13 changes: 13 additions & 0 deletions tests/test8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ light:
id: rgb_led
name: "RGB LED"
data_rate: 8MHz
- platform: binary
name: "Red Info Light"
output: board_info_ed
entity_category: diagnostic
restore_mode: ALWAYS_OFF

spi:
id: spi_id_1
Expand Down Expand Up @@ -73,6 +78,14 @@ i2c:
scl: GPIO18
sda: GPIO8

output:
- platform: gpio
id: board_info_ed
pin:
# This pin is reserved on the ESP32S3!
number: 26
ignore_pin_validation_error: true

touchscreen:
- platform: tt21100
display: displ8
Expand Down

0 comments on commit f0ec900

Please sign in to comment.