From ebca936b7e76d846659a7a35380ff04dd4669111 Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Tue, 7 Jun 2022 13:00:27 +0200 Subject: [PATCH] Fix percentage validation for wrong data type input (#3524) Co-authored-by: Maurice Makaay --- esphome/config_validation.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/esphome/config_validation.py b/esphome/config_validation.py index 3dc8011a87..dacbcf72ba 100644 --- a/esphome/config_validation.py +++ b/esphome/config_validation.py @@ -1086,16 +1086,21 @@ def possibly_negative_percentage(value): except ValueError: # pylint: disable=raise-missing-from raise Invalid("invalid number") - if value > 1: - msg = "Percentage must not be higher than 100%." - if not has_percent_sign: - msg += " Please put a percent sign after the number!" - raise Invalid(msg) - if value < -1: - msg = "Percentage must not be smaller than -100%." - if not has_percent_sign: - msg += " Please put a percent sign after the number!" - raise Invalid(msg) + try: + if value > 1: + msg = "Percentage must not be higher than 100%." + if not has_percent_sign: + msg += " Please put a percent sign after the number!" + raise Invalid(msg) + if value < -1: + msg = "Percentage must not be smaller than -100%." + if not has_percent_sign: + msg += " Please put a percent sign after the number!" + raise Invalid(msg) + except TypeError: + raise Invalid( # pylint: disable=raise-missing-from + "Expected percentage or float between -1.0 and 1.0" + ) return negative_one_to_one_float(value)