Skip to content

Commit

Permalink
Ensure bytes are json encodable
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeweerd committed Oct 25, 2023
1 parent 904acb7 commit 9bd3b57
Showing 1 changed file with 3 additions and 18 deletions.
21 changes: 3 additions & 18 deletions custom_components/zha_toolkit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,31 +446,16 @@ def get_cluster_from_params(
return cluster


def dict_to_hexvalues(read_dict):
result = {}
for attr_id, value in read_dict.items():
if callable(getattr(value, "serialize", None)):
value = value.serialize()

if isinstance(value, bytes):
try:
value = value.split(b"\x00")[0].decode().strip()
except UnicodeDecodeError:
value = value.hex()

result[attr_id] = value

return result


def dict_to_jsonable(src_dict):
result = {}
if isJsonable(src_dict):
return src_dict
for key, value in src_dict.items():
if not isJsonable(value):
LOGGER.error(f"Can't convert to JSON {value!r}")
if callable(getattr(value, "serialize", None)):
if isinstance(value, bytes):
value = str(value, encoding="ascii")
elif callable(getattr(value, "serialize", None)):
value = value.serialize()
else:
value = repr(value)
Expand Down

0 comments on commit 9bd3b57

Please sign in to comment.