Serialize bytes values in diagnostics JSON - #856
Open
Sanjays2402 wants to merge 1 commit into
Open
Conversation
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.
Contributor
There was a problem hiding this comment.
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
ZhaJsonEncoderto serializebytesusing Home Assistant’s{"__type": ..., "repr": ...}representation. - Add a regression test ensuring
bytesin quirk-provided button entity info survives ajson.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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #845
ZhaJsonEncoderonly special-casedset, so anybytesvalue reachingDevice.get_diagnostics_json()made the entire diagnostics JSON unserializable — and quirks v2 can putbytesthere throughcommand_button(..., command_args=...)andwrite_attr_button(attr, b"..."). Devices using either could never be imported intotests/data/devices/, so they got no coverage fromtest_devices_from_files.The encoder now emits Home Assistant's
{"__type": ..., "repr": ...}representation forbytes, which is exactly the shapetools/import_diagnostics.py'sparse_legacy_valuealready reads back, so the snapshot round-trip stays symmetric. Regression test added intests/test_discover.pycovering both builder arguments: it fails ondevwithTypeError: Object of type bytes is not JSON serializableand passes with the fix;tests/test_discover.pyis 875 passed.This change was prepared with AI assistance; the regression test was run locally and fails without the fix.