Add bytes support for bitwise template operations (#60211)

* Add bytes support for bitwise template operations

* spelling

* Update bitwise tests

* remove try block for bytes conversion

* do not accept empty `bytes` object
This commit is contained in:
Jan Bouwhuis 2021-11-24 15:15:27 +01:00 committed by GitHub
parent fa34153b20
commit d33457b7bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 121 additions and 5 deletions

View file

@ -1452,6 +1452,24 @@ def test_bitwise_and(hass):
)
assert tpl.async_render() == 8 & 2
tpl = template.Template(
"""
{{ ( value_a ) | bitwise_and(value_b) }}
""",
hass,
)
variables = {"value_a": b"\x9b\xc2", "value_b": 0xFF00}
assert tpl.async_render(variables=variables) == 0x9B00
tpl = template.Template(
"""
{{ ( value_a ) | bitwise_and(value_b, little_endian=True) }}
""",
hass,
)
variables = {"value_a": b"\xc2\x9b", "value_b": 0xFFFF}
assert tpl.async_render(variables=variables) == 0x9BC2
def test_bitwise_or(hass):
"""Test bitwise_or method."""
@ -1477,6 +1495,54 @@ def test_bitwise_or(hass):
)
assert tpl.async_render() == 8 | 2
tpl = template.Template(
"""
{{ value_a | bitwise_or(value_b) }}
""",
hass,
)
variables = {
"value_a": b"\xc2\x9b",
"value_b": 0xFFFF,
}
assert tpl.async_render(variables=variables) == 65535 # 39874
tpl = template.Template(
"""
{{ ( value_a ) | bitwise_or(value_b) }}
""",
hass,
)
variables = {
"value_a": 0xFF00,
"value_b": b"\xc2\x9b",
}
assert tpl.async_render(variables=variables) == 0xFF9B
tpl = template.Template(
"""
{{ ( value_a ) | bitwise_or(value_b) }}
""",
hass,
)
variables = {
"value_a": b"\xc2\x9b",
"value_b": 0x0000,
}
assert tpl.async_render(variables=variables) == 0xC29B
tpl = template.Template(
"""
{{ ( value_a ) | bitwise_or(value_b, little_endian=True) }}
""",
hass,
)
variables = {
"value_a": b"\xc2\x9b",
"value_b": 0,
}
assert tpl.async_render(variables=variables) == 0x9BC2
def test_distance_function_with_1_state(hass):
"""Test distance function with 1 state."""