mirror of
https://github.com/esphome/esphome.git
synced 2025-09-06 05:12:21 +01:00
fix percentage handling (#1094)
* fix percentage handling * add test * fix lint errors Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
@@ -838,9 +838,16 @@ def percentage(value):
|
||||
|
||||
|
||||
def possibly_negative_percentage(value):
|
||||
has_percent_sign = isinstance(value, str) and value.endswith('%')
|
||||
if has_percent_sign:
|
||||
value = float(value[:-1].rstrip()) / 100.0
|
||||
has_percent_sign = False
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
if value.endswith('%'):
|
||||
has_percent_sign = False
|
||||
value = float(value[:-1].rstrip()) / 100.0
|
||||
else:
|
||||
value = float(value)
|
||||
except ValueError:
|
||||
raise Invalid("invalid number")
|
||||
if value > 1:
|
||||
msg = "Percentage must not be higher than 100%."
|
||||
if not has_percent_sign:
|
||||
|
Reference in New Issue
Block a user