mirror of
https://github.com/esphome/esphome.git
synced 2025-09-13 00:32:20 +01:00
Better/stricter pin validation (#903)
* Better/stricter pin validation * Update tests
This commit is contained in:
@@ -289,30 +289,48 @@ def _translate_pin(value):
|
||||
return _lookup_pin(value)
|
||||
|
||||
|
||||
_ESP_SDIO_PINS = {
|
||||
6: 'Flash Clock',
|
||||
7: 'Flash Data 0',
|
||||
8: 'Flash Data 1',
|
||||
11: 'Flash Command',
|
||||
}
|
||||
|
||||
|
||||
def validate_gpio_pin(value):
|
||||
value = _translate_pin(value)
|
||||
if CORE.is_esp32:
|
||||
if value < 0 or value > 39:
|
||||
raise cv.Invalid(u"ESP32: Invalid pin number: {}".format(value))
|
||||
if 6 <= value <= 11:
|
||||
_LOGGER.warning(u"ESP32: Pin %s (6-11) might already be used by the "
|
||||
u"flash interface. Be warned.", value)
|
||||
if value in _ESP_SDIO_PINS:
|
||||
raise cv.Invalid("This pin cannot be used on ESP32s and is already used by "
|
||||
"the flash interface (function: {})".format(_ESP_SDIO_PINS[value]))
|
||||
if 9 <= value <= 10:
|
||||
_LOGGER.warning(u"ESP32: Pin %s (9-10) might already be used by the "
|
||||
u"flash interface in QUAD IO flash mode.", value)
|
||||
if value in (20, 24, 28, 29, 30, 31):
|
||||
_LOGGER.warning(u"ESP32: Pin %s (20, 24, 28-31) can usually not be used. "
|
||||
u"Be warned.", value)
|
||||
# These pins are not exposed in GPIO mux (reason unknown)
|
||||
# but they're missing from IO_MUX list in datasheet
|
||||
raise cv.Invalid("The pin GPIO{} is not usable on ESP32s.".format(value))
|
||||
return value
|
||||
if CORE.is_esp8266:
|
||||
if 6 <= value <= 11:
|
||||
_LOGGER.warning(u"ESP8266: Pin %s (6-11) might already be used by the "
|
||||
u"flash interface. Be warned.", value)
|
||||
if value < 0 or value > 17:
|
||||
raise cv.Invalid(u"ESP8266: Invalid pin number: {}".format(value))
|
||||
if value in _ESP_SDIO_PINS:
|
||||
raise cv.Invalid("This pin cannot be used on ESP8266s and is already used by "
|
||||
"the flash interface (function: {})".format(_ESP_SDIO_PINS[value]))
|
||||
if 9 <= value <= 10:
|
||||
_LOGGER.warning(u"ESP8266: Pin %s (9-10) might already be used by the "
|
||||
u"flash interface in QUAD IO flash mode.", value)
|
||||
return value
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def input_pin(value):
|
||||
return validate_gpio_pin(value)
|
||||
value = validate_gpio_pin(value)
|
||||
if CORE.is_esp8266 and value == 17:
|
||||
raise cv.Invalid("GPIO17 (TOUT) is an analog-only pin on the ESP8266.")
|
||||
return value
|
||||
|
||||
|
||||
def input_pullup_pin(value):
|
||||
@@ -335,6 +353,8 @@ def output_pin(value):
|
||||
u"input pin.".format(value))
|
||||
return value
|
||||
if CORE.is_esp8266:
|
||||
if value == 17:
|
||||
raise cv.Invalid("GPIO17 (TOUT) is an analog-only pin on the ESP8266.")
|
||||
return value
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -348,7 +368,7 @@ def analog_pin(value):
|
||||
if CORE.is_esp8266:
|
||||
if value == 17: # A0
|
||||
return value
|
||||
raise cv.Invalid(u"ESP8266: Only pin A0 (17) supports ADC.")
|
||||
raise cv.Invalid(u"ESP8266: Only pin A0 (GPIO17) supports ADC.")
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user