mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00: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) |     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): | def validate_gpio_pin(value): | ||||||
|     value = _translate_pin(value) |     value = _translate_pin(value) | ||||||
|     if CORE.is_esp32: |     if CORE.is_esp32: | ||||||
|         if value < 0 or value > 39: |         if value < 0 or value > 39: | ||||||
|             raise cv.Invalid(u"ESP32: Invalid pin number: {}".format(value)) |             raise cv.Invalid(u"ESP32: Invalid pin number: {}".format(value)) | ||||||
|         if 6 <= value <= 11: |         if value in _ESP_SDIO_PINS: | ||||||
|             _LOGGER.warning(u"ESP32: Pin %s (6-11) might already be used by the " |             raise cv.Invalid("This pin cannot be used on ESP32s and is already used by " | ||||||
|                             u"flash interface. Be warned.", value) |                              "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): |         if value in (20, 24, 28, 29, 30, 31): | ||||||
|             _LOGGER.warning(u"ESP32: Pin %s (20, 24, 28-31) can usually not be used. " |             # These pins are not exposed in GPIO mux (reason unknown) | ||||||
|                             u"Be warned.", value) |             # 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 |         return value | ||||||
|     if CORE.is_esp8266: |     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: |         if value < 0 or value > 17: | ||||||
|             raise cv.Invalid(u"ESP8266: Invalid pin number: {}".format(value)) |             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 |         return value | ||||||
|     raise NotImplementedError |     raise NotImplementedError | ||||||
|  |  | ||||||
|  |  | ||||||
| def input_pin(value): | 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): | def input_pullup_pin(value): | ||||||
| @@ -335,6 +353,8 @@ def output_pin(value): | |||||||
|                              u"input pin.".format(value)) |                              u"input pin.".format(value)) | ||||||
|         return value |         return value | ||||||
|     if CORE.is_esp8266: |     if CORE.is_esp8266: | ||||||
|  |         if value == 17: | ||||||
|  |             raise cv.Invalid("GPIO17 (TOUT) is an analog-only pin on the ESP8266.") | ||||||
|         return value |         return value | ||||||
|     raise NotImplementedError |     raise NotImplementedError | ||||||
|  |  | ||||||
| @@ -348,7 +368,7 @@ def analog_pin(value): | |||||||
|     if CORE.is_esp8266: |     if CORE.is_esp8266: | ||||||
|         if value == 17:  # A0 |         if value == 17:  # A0 | ||||||
|             return value |             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 |     raise NotImplementedError | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -435,7 +435,7 @@ sensor: | |||||||
|   - platform: hx711 |   - platform: hx711 | ||||||
|     name: "HX711 Value" |     name: "HX711 Value" | ||||||
|     dout_pin: GPIO23 |     dout_pin: GPIO23 | ||||||
|     clk_pin: GPIO24 |     clk_pin: GPIO25 | ||||||
|     gain: 128 |     gain: 128 | ||||||
|     update_interval: 15s |     update_interval: 15s | ||||||
|   - platform: ina219 |   - platform: ina219 | ||||||
| @@ -542,7 +542,7 @@ sensor: | |||||||
|     name: "Rotary Encoder" |     name: "Rotary Encoder" | ||||||
|     id: rotary_encoder1 |     id: rotary_encoder1 | ||||||
|     pin_a: GPIO23 |     pin_a: GPIO23 | ||||||
|     pin_b: GPIO24 |     pin_b: GPIO25 | ||||||
|     pin_reset: GPIO25 |     pin_reset: GPIO25 | ||||||
|     filters: |     filters: | ||||||
|       - or: |       - or: | ||||||
| @@ -655,7 +655,7 @@ sensor: | |||||||
|     integration_time: 402ms |     integration_time: 402ms | ||||||
|     gain: 16x |     gain: 16x | ||||||
|   - platform: ultrasonic |   - platform: ultrasonic | ||||||
|     trigger_pin: GPIO24 |     trigger_pin: GPIO25 | ||||||
|     echo_pin: |     echo_pin: | ||||||
|       number: GPIO23 |       number: GPIO23 | ||||||
|       inverted: true |       inverted: true | ||||||
| @@ -1396,11 +1396,11 @@ display: | |||||||
|   dimensions: 18x4 |   dimensions: 18x4 | ||||||
|   data_pins: |   data_pins: | ||||||
|   - GPIO19 |   - GPIO19 | ||||||
|   - GPIO20 |  | ||||||
|   - GPIO21 |   - GPIO21 | ||||||
|   - GPIO22 |   - GPIO22 | ||||||
|  |   - GPIO23 | ||||||
|   enable_pin: GPIO23 |   enable_pin: GPIO23 | ||||||
|   rs_pin: GPIO24 |   rs_pin: GPIO25 | ||||||
|   lambda: |- |   lambda: |- | ||||||
|     it.print("Hello World!"); |     it.print("Hello World!"); | ||||||
| - platform: lcd_pcf8574 | - platform: lcd_pcf8574 | ||||||
| @@ -1528,7 +1528,7 @@ stepper: | |||||||
| - platform: a4988 | - platform: a4988 | ||||||
|   id: my_stepper |   id: my_stepper | ||||||
|   step_pin: GPIO23 |   step_pin: GPIO23 | ||||||
|   dir_pin: GPIO24 |   dir_pin: GPIO25 | ||||||
|   sleep_pin: GPIO25 |   sleep_pin: GPIO25 | ||||||
|   max_speed: 250 steps/s |   max_speed: 250 steps/s | ||||||
|   acceleration: 100 steps/s^2 |   acceleration: 100 steps/s^2 | ||||||
|   | |||||||
| @@ -10,7 +10,7 @@ substitutions: | |||||||
| ethernet: | ethernet: | ||||||
|   type: LAN8720 |   type: LAN8720 | ||||||
|   mdc_pin: GPIO23 |   mdc_pin: GPIO23 | ||||||
|   mdio_pin: GPIO24 |   mdio_pin: GPIO25 | ||||||
|   clk_mode: GPIO0_IN |   clk_mode: GPIO0_IN | ||||||
|   phy_addr: 0 |   phy_addr: 0 | ||||||
|   power_pin: GPIO25 |   power_pin: GPIO25 | ||||||
| @@ -286,7 +286,7 @@ stepper: | |||||||
|   - platform: uln2003 |   - platform: uln2003 | ||||||
|     id: my_stepper |     id: my_stepper | ||||||
|     pin_a: GPIO23 |     pin_a: GPIO23 | ||||||
|     pin_b: GPIO24 |     pin_b: GPIO27 | ||||||
|     pin_c: GPIO25 |     pin_c: GPIO25 | ||||||
|     pin_d: GPIO26 |     pin_d: GPIO26 | ||||||
|     sleep_when_done: no |     sleep_when_done: no | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user