mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add from_hex filter (#145229)
This commit is contained in:
parent
2cf09abb4c
commit
25f3ab3640
@ -2572,6 +2572,11 @@ def struct_unpack(value: bytes, format_string: str, offset: int = 0) -> Any | No
|
||||
return None
|
||||
|
||||
|
||||
def from_hex(value: str) -> bytes:
|
||||
"""Perform hex string decode."""
|
||||
return bytes.fromhex(value)
|
||||
|
||||
|
||||
def base64_encode(value: str) -> str:
|
||||
"""Perform base64 encode."""
|
||||
return base64.b64encode(value.encode("utf-8")).decode("utf-8")
|
||||
@ -3131,6 +3136,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
||||
self.filters["flatten"] = flatten
|
||||
self.filters["float"] = forgiving_float_filter
|
||||
self.filters["from_json"] = from_json
|
||||
self.filters["from_hex"] = from_hex
|
||||
self.filters["iif"] = iif
|
||||
self.filters["int"] = forgiving_int_filter
|
||||
self.filters["intersect"] = intersect
|
||||
|
@ -1632,6 +1632,14 @@ def test_ord(hass: HomeAssistant) -> None:
|
||||
assert template.Template('{{ "d" | ord }}', hass).async_render() == 100
|
||||
|
||||
|
||||
def test_from_hex(hass: HomeAssistant) -> None:
|
||||
"""Test the fromhex filter."""
|
||||
assert (
|
||||
template.Template("{{ '0F010003' | from_hex }}", hass).async_render()
|
||||
== b"\x0f\x01\x00\x03"
|
||||
)
|
||||
|
||||
|
||||
def test_base64_encode(hass: HomeAssistant) -> None:
|
||||
"""Test the base64_encode filter."""
|
||||
assert (
|
||||
|
Loading…
x
Reference in New Issue
Block a user