From 809366995ff01ab9f7b57fbd701b15a381da2d50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20M=C3=A1rai?= Date: Thu, 16 Jan 2025 12:29:00 +0100 Subject: [PATCH] Improve valid_dac_pin() readability Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/esp32_dac/output.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/esphome/components/esp32_dac/output.py b/esphome/components/esp32_dac/output.py index 2f4cb76342..c80780986f 100644 --- a/esphome/components/esp32_dac/output.py +++ b/esphome/components/esp32_dac/output.py @@ -1,18 +1,27 @@ +import esphome.codegen as cg +import esphome.config_validation as cv from esphome import pins from esphome.components import output -import esphome.config_validation as cv -import esphome.codegen as cg from esphome.components.esp32 import get_esp32_variant -from esphome.components.esp32.const import VARIANT_ESP32S2 +from esphome.components.esp32.const import VARIANT_ESP32, VARIANT_ESP32S2 from esphome.const import CONF_ID, CONF_NUMBER, CONF_PIN DEPENDENCIES = ["esp32"] +DAC_PINS = { + VARIANT_ESP32: (25, 26), + VARIANT_ESP32S2: (17, 18), +} + def valid_dac_pin(value): - valid_pins = (17, 18) if get_esp32_variant() == VARIANT_ESP32S2 else (25, 26) - num = value[CONF_NUMBER] - cv.one_of(*valid_pins)(num) + variant = get_esp32_variant() + try: + valid_pins = DAC_PINS[variant] + except KeyError as ex: + raise cv.Invalid(f"DAC is not supported on {variant}") from ex + given_pin = value[CONF_NUMBER] + cv.one_of(*valid_pins)(given_pin) return value