mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 16:38:16 +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:
parent
e2b655a6cc
commit
f9a8629157
2 changed files with 14 additions and 3 deletions
|
@ -838,9 +838,16 @@ def percentage(value):
|
||||||
|
|
||||||
|
|
||||||
def possibly_negative_percentage(value):
|
def possibly_negative_percentage(value):
|
||||||
has_percent_sign = isinstance(value, str) and value.endswith('%')
|
has_percent_sign = False
|
||||||
if has_percent_sign:
|
if isinstance(value, str):
|
||||||
value = float(value[:-1].rstrip()) / 100.0
|
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:
|
if value > 1:
|
||||||
msg = "Percentage must not be higher than 100%."
|
msg = "Percentage must not be higher than 100%."
|
||||||
if not has_percent_sign:
|
if not has_percent_sign:
|
||||||
|
|
|
@ -14,6 +14,8 @@ esphome:
|
||||||
substitutions:
|
substitutions:
|
||||||
devicename: test3
|
devicename: test3
|
||||||
devicecomment: test3 device
|
devicecomment: test3 device
|
||||||
|
min_sub: "0.03"
|
||||||
|
max_sub: "12.0%"
|
||||||
|
|
||||||
api:
|
api:
|
||||||
port: 8000
|
port: 8000
|
||||||
|
@ -769,6 +771,8 @@ servo:
|
||||||
id: my_servo
|
id: my_servo
|
||||||
output: out
|
output: out
|
||||||
restore: true
|
restore: true
|
||||||
|
min_level: $min_sub
|
||||||
|
max_level: $max_sub
|
||||||
|
|
||||||
ttp229_lsf:
|
ttp229_lsf:
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue