Revert "Allow template int filter to render from a bytes based integer" (#60855)

This commit is contained in:
Erik Montnemery 2021-12-02 23:37:19 +01:00 committed by GitHub
parent c8b0a3b667
commit a07f75c6b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 51 deletions

View file

@ -1567,15 +1567,8 @@ def forgiving_float_filter(value, default=_SENTINEL):
return default
def forgiving_int(value, default=_SENTINEL, base=10, little_endian=False):
def forgiving_int(value, default=_SENTINEL, base=10):
"""Try to convert value to an int, and warn if it fails."""
if isinstance(value, bytes) and value:
if base != 10:
_LOGGER.warning(
"Template warning: 'int' got 'bytes' type input, ignoring base=%s parameter",
base,
)
return convert_to_int(value, little_endian=little_endian)
result = jinja2.filters.do_int(value, default=default, base=base)
if result is _SENTINEL:
warn_no_default("int", value, value)
@ -1583,15 +1576,8 @@ def forgiving_int(value, default=_SENTINEL, base=10, little_endian=False):
return result
def forgiving_int_filter(value, default=_SENTINEL, base=10, little_endian=False):
def forgiving_int_filter(value, default=_SENTINEL, base=10):
"""Try to convert value to an int, and warn if it fails."""
if isinstance(value, bytes) and value:
if base != 10:
_LOGGER.warning(
"Template warning: 'int' got 'bytes' type input, ignoring base=%s parameter",
base,
)
return convert_to_int(value, little_endian=little_endian)
result = jinja2.filters.do_int(value, default=default, base=base)
if result is _SENTINEL:
warn_no_default("int", value, 0)

View file

@ -252,7 +252,7 @@ def test_float_filter(hass):
assert render(hass, "{{ 'bad' | float(default=1) }}") == 1
def test_int_filter(hass, caplog):
def test_int_filter(hass):
"""Test int filter."""
hass.states.async_set("sensor.temperature", "12.2")
assert render(hass, "{{ states.sensor.temperature.state | int }}") == 12
@ -265,25 +265,8 @@ def test_int_filter(hass, caplog):
assert render(hass, "{{ 'bad' | int(1) }}") == 1
assert render(hass, "{{ 'bad' | int(default=1) }}") == 1
# Test with bytes based integer
variables = {"value": b"\xde\xad\xbe\xef"}
assert (render(hass, "{{ value | int }}", variables=variables)) == 0xDEADBEEF
assert (
render(hass, "{{ value | int(little_endian=True) }}", variables=variables)
== 0xEFBEADDE
)
# Test with base and base parameter set
assert (
render(hass, "{{ value | int(base=16) }}", variables=variables)
) == 0xDEADBEEF
assert (
"Template warning: 'int' got 'bytes' type input, ignoring base=16 parameter"
in caplog.text
)
def test_int_function(hass, caplog):
def test_int_function(hass):
"""Test int filter."""
hass.states.async_set("sensor.temperature", "12.2")
assert render(hass, "{{ int(states.sensor.temperature.state) }}") == 12
@ -296,22 +279,6 @@ def test_int_function(hass, caplog):
assert render(hass, "{{ int('bad', 1) }}") == 1
assert render(hass, "{{ int('bad', default=1) }}") == 1
# Test with base and base parameter set
variables = {"value": b"\xde\xad\xbe\xef"}
assert (render(hass, "{{ int(value) }}", variables=variables)) == 0xDEADBEEF
assert (
render(hass, "{{ int(value, little_endian=True) }}", variables=variables)
== 0xEFBEADDE
)
assert (
render(hass, "{{ int(value, base=16) }}", variables=variables)
) == 0xDEADBEEF
assert (
"Template warning: 'int' got 'bytes' type input, ignoring base=16 parameter"
in caplog.text
)
@pytest.mark.parametrize(
"value, expected",