Skip to content

Commit

Permalink
Ensure read_before is jsonable
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeweerd committed Oct 24, 2023
1 parent 6568a6e commit 904acb7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
34 changes: 34 additions & 0 deletions custom_components/zha_toolkit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,40 @@ 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)):
value = value.serialize()
else:
value = repr(value)

result[key] = value

return result


def write_json_to_file(
data, subdir, fname, desc, listener=None, normalize_name=False
):
Expand Down
31 changes: 23 additions & 8 deletions custom_components/zha_toolkit/zcl_attr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import asyncio
import importlib
import logging

from homeassistant.helpers.template import Template
Expand Down Expand Up @@ -424,6 +423,9 @@ async def attr_write( # noqa: C901
attr = f.Attribute(attr_id, value=attr_val)
attr_write_list.append(attr) # Write list

# Use serialize to compare if the compare_val allows it
use_serialize = callable(getattr(compare_val, "serialize", None))

if attr_type is not None:
event_data["attr_type"] = f"0x{attr_type:02X}"

Expand All @@ -439,6 +441,11 @@ async def attr_write( # noqa: C901
attr_id
].serialize() # type:ignore[union-attr]
== compare_val.serialize()
if use_serialize
else result_read[0][ # type:ignore[index]
attr_id
] # type:ignore[union-attr]
== compare_val
)
)
)
Expand Down Expand Up @@ -501,11 +508,17 @@ async def attr_write( # noqa: C901
if (
result_read[0][attr_id].serialize()
!= compare_val.serialize()
if use_serialize
else result_read[0][attr_id] != compare_val
):
success = False
msg = "Read does not match expected: {!r} <> {!r}".format(
result_read[0][attr_id].serialize(),
compare_val.serialize(),
result_read[0][attr_id].serialize()
if use_serialize
else result_read[0][attr_id],
compare_val.serialize()
if use_serialize
else compare_val,
)
LOGGER.warning(msg)
if "warnings" not in event_data:
Expand Down Expand Up @@ -623,11 +636,13 @@ async def attr_write( # noqa: C901
listener=listener,
)

importlib.reload(u)
if "result_read" in event_data and not u.isJsonable(
event_data["result_read"]
):
event_data["result_read"] = repr(event_data["result_read"])
for key in ["read_before", "result_read"]:
if key not in event_data:
continue
event_data[key] = (
u.dict_to_jsonable(event_data[key][0]),
event_data[key][1],
)

# For internal use
return result_read
Expand Down

0 comments on commit 904acb7

Please sign in to comment.