Skip to content

Commit

Permalink
Better conversion to jsonable for scan_device values
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeweerd committed Dec 6, 2023
1 parent babfbbd commit 50cf3da
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
7 changes: 1 addition & 6 deletions custom_components/zha_toolkit/scan_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,7 @@ async def discover_attributes_extended(cluster, manufacturer=None, tries=3):
"Reading attr success: %s, failed %s", success, failed
)
for attr_id, value in success.items():
if isinstance(value, bytes):
try:
value = value.split(b"\x00")[0].decode().strip()
except UnicodeDecodeError:
value = value.hex()
result[attr_id]["attribute_value"] = value
result[attr_id]["attribute_value"] = u.value_to_jsonable(value)
except (
DeliveryError,
asyncio.TimeoutError,
Expand Down
41 changes: 26 additions & 15 deletions custom_components/zha_toolkit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,26 +471,37 @@ def get_cluster_from_params(
return cluster


def value_to_jsonable(value):
if not isJsonable(value):
LOGGER.debug(
"Can't convert %r to JSON, serializing if possible.", value
)
if callable(getattr(value, "serialize", None)):
# Serialization results in "bytes"
value = value.serialize()
if isinstance(value, bytes):
# "bytes" is not compatible with json, convert
# try:
# value = value.split(b"\x00")[0].decode().strip()
# except:
# value = value.hex()

try:
value = str(value, encoding="ascii")
except Exception:
value = "0x" + value.hex()
else:
# Anything else: get a textual representation
value = repr(value)
return value


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.debug(
"Can't convert %r to JSON, serializing if possible.", value
)
if callable(getattr(value, "serialize", None)):
# Serialization results in "bytes"
value = value.serialize()
if isinstance(value, bytes):
# "bytes" is not compatible with json, get a "string"
value = str(value, encoding="ascii")
else:
# Anything else: get a textual representation
value = repr(value)

result[key] = value
result[key] = value_to_jsonable(value)

return result

Expand Down

0 comments on commit 50cf3da

Please sign in to comment.