From 125c693e3f54aabdda8901c575a1c1354e919143 Mon Sep 17 00:00:00 2001 From: Oxan van Leeuwen Date: Sat, 19 Feb 2022 11:41:34 +0100 Subject: [PATCH] Add ESP32 variant config validator function (#3088) * Add esp32_variant config validator function * Drop unused is_esp32c3 function Co-authored-by: Otto Winter --- esphome/components/esp32/__init__.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/esphome/components/esp32/__init__.py b/esphome/components/esp32/__init__.py index 1229675ad8..0b2c291ba2 100644 --- a/esphome/components/esp32/__init__.py +++ b/esphome/components/esp32/__init__.py @@ -65,8 +65,26 @@ def get_esp32_variant(): return CORE.data[KEY_ESP32][KEY_VARIANT] -def is_esp32c3(): - return get_esp32_variant() == VARIANT_ESP32C3 +def only_on_variant(*, supported=None, unsupported=None): + """Config validator for features only available on some ESP32 variants.""" + if supported is not None and not isinstance(supported, list): + supported = [supported] + if unsupported is not None and not isinstance(unsupported, list): + unsupported = [unsupported] + + def validator_(obj): + variant = get_esp32_variant() + if supported is not None and variant not in supported: + raise cv.Invalid( + f"This feature is only available on {', '.join(supported)}" + ) + if unsupported is not None and variant in unsupported: + raise cv.Invalid( + f"This feature is not available on {', '.join(unsupported)}" + ) + return obj + + return validator_ @dataclass