mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Add is_number template filter and function (#56705)
This commit is contained in:
parent
e4dc646237
commit
14a1bb423c
@ -1370,6 +1370,17 @@ def forgiving_float(value):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def is_number(value):
|
||||||
|
"""Try to convert value to a float."""
|
||||||
|
try:
|
||||||
|
fvalue = float(value)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
return False
|
||||||
|
if math.isnan(fvalue) or math.isinf(fvalue):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def regex_match(value, find="", ignorecase=False):
|
def regex_match(value, find="", ignorecase=False):
|
||||||
"""Match value using regex."""
|
"""Match value using regex."""
|
||||||
if not isinstance(value, str):
|
if not isinstance(value, str):
|
||||||
@ -1575,6 +1586,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
|||||||
self.filters["bitwise_and"] = bitwise_and
|
self.filters["bitwise_and"] = bitwise_and
|
||||||
self.filters["bitwise_or"] = bitwise_or
|
self.filters["bitwise_or"] = bitwise_or
|
||||||
self.filters["ord"] = ord
|
self.filters["ord"] = ord
|
||||||
|
self.filters["is_number"] = is_number
|
||||||
self.globals["log"] = logarithm
|
self.globals["log"] = logarithm
|
||||||
self.globals["sin"] = sine
|
self.globals["sin"] = sine
|
||||||
self.globals["cos"] = cosine
|
self.globals["cos"] = cosine
|
||||||
@ -1597,6 +1609,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
|||||||
self.globals["urlencode"] = urlencode
|
self.globals["urlencode"] = urlencode
|
||||||
self.globals["max"] = max
|
self.globals["max"] = max
|
||||||
self.globals["min"] = min
|
self.globals["min"] = min
|
||||||
|
self.globals["is_number"] = is_number
|
||||||
self.tests["match"] = regex_match
|
self.tests["match"] = regex_match
|
||||||
self.tests["search"] = regex_search
|
self.tests["search"] = regex_search
|
||||||
|
|
||||||
|
@ -220,6 +220,41 @@ def test_float(hass):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"value, expected",
|
||||||
|
[
|
||||||
|
(0, True),
|
||||||
|
(0.0, True),
|
||||||
|
("0", True),
|
||||||
|
("0.0", True),
|
||||||
|
(True, True),
|
||||||
|
(False, True),
|
||||||
|
("True", False),
|
||||||
|
("False", False),
|
||||||
|
(None, False),
|
||||||
|
("None", False),
|
||||||
|
("horse", False),
|
||||||
|
(math.pi, True),
|
||||||
|
(math.nan, False),
|
||||||
|
(math.inf, False),
|
||||||
|
("nan", False),
|
||||||
|
("inf", False),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_isnumber(hass, value, expected):
|
||||||
|
"""Test is_number."""
|
||||||
|
assert (
|
||||||
|
template.Template("{{ is_number(value) }}", hass).async_render({"value": value})
|
||||||
|
== expected
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
template.Template("{{ value | is_number }}", hass).async_render(
|
||||||
|
{"value": value}
|
||||||
|
)
|
||||||
|
== expected
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_rounding_value(hass):
|
def test_rounding_value(hass):
|
||||||
"""Test rounding value."""
|
"""Test rounding value."""
|
||||||
hass.states.async_set("sensor.temperature", 12.78)
|
hass.states.async_set("sensor.temperature", 12.78)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user