Skip to content

Serialize bytes values in diagnostics JSON - #856

Open
Sanjays2402 wants to merge 1 commit into
zigpy:devfrom
Sanjays2402:fix/diagnostics-encode-bytes
Open

Serialize bytes values in diagnostics JSON#856
Sanjays2402 wants to merge 1 commit into
zigpy:devfrom
Sanjays2402:fix/diagnostics-encode-bytes

Conversation

@Sanjays2402

Copy link
Copy Markdown

Closes #845

ZhaJsonEncoder only special-cased set, so any bytes value reaching Device.get_diagnostics_json() made the entire diagnostics JSON unserializable — and quirks v2 can put bytes there through command_button(..., command_args=...) and write_attr_button(attr, b"..."). Devices using either could never be imported into tests/data/devices/, so they got no coverage from test_devices_from_files.

The encoder now emits Home Assistant's {"__type": ..., "repr": ...} representation for bytes, which is exactly the shape tools/import_diagnostics.py's parse_legacy_value already reads back, so the snapshot round-trip stays symmetric. Regression test added in tests/test_discover.py covering both builder arguments: it fails on dev with TypeError: Object of type bytes is not JSON serializable and passes with the fix; tests/test_discover.py is 875 passed.

This change was prepared with AI assistance; the regression test was run locally and fails without the fix.

ZhaJsonEncoder only special-cased set, so any bytes value reaching
Device.get_diagnostics_json() made the whole diagnostics JSON
unserializable. Quirks v2 can put bytes there via command_button's
command_args and write_attr_button's attribute_value, so devices using
either could never be imported into tests/data/devices/ and got no
coverage from test_devices_from_files.

Emit Home Assistant's {"__type": ..., "repr": ...} representation for
bytes, which is exactly the shape tools/import_diagnostics.py's
parse_legacy_value already reads back.

Adds a regression test covering both builder arguments.
Copilot AI review requested due to automatic review settings August 2, 2026 12:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes diagnostics snapshot tooling so Device.get_diagnostics_json() can be JSON-serialized even when quirks v2 entity info contains bytes (e.g., command_args / attribute_value), keeping the diagnostics import/regeneration round-trip symmetric with the existing legacy parser.

Changes:

  • Extend ZhaJsonEncoder to serialize bytes using Home Assistant’s {"__type": ..., "repr": ...} representation.
  • Add a regression test ensuring bytes in quirk-provided button entity info survives a json.dumps(...)/json.loads(...) round-trip with the custom encoder.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/test_discover.py Adds a regression test covering bytes in command_args and attribute_value within quirk entity info diagnostics.
tests/common.py Updates ZhaJsonEncoder to serialize bytes into a deterministic legacy-compatible dict representation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov

codecov Bot commented Aug 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.15%. Comparing base (a270540) to head (776caab).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev     #856   +/-   ##
=======================================
  Coverage   97.15%   97.15%           
=======================================
  Files          55       55           
  Lines       10482    10482           
=======================================
  Hits        10184    10184           
  Misses        298      298           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Diagnostics JSON tooling can't serialize bytes in quirk entity info (command_args / attribute_value)

2 participants