Skip to content

Fix non-string unit_of_measurement 500-ing /api/states on HA 2026.7+ - #462

Open
Austin519 wants to merge 1 commit into
sfstar:mainfrom
Austin519:fix/non-str-unit-serialization
Open

Fix non-string unit_of_measurement 500-ing /api/states on HA 2026.7+#462
Austin519 wants to merge 1 commit into
sfstar:mainfrom
Austin519:fix/non-str-unit-serialization

Conversation

@Austin519

Copy link
Copy Markdown

Problem

On Home Assistant 2026.7+, adding this integration returns HTTP 500 for the entire /api/states endpoint (the whole dashboard/app becomes unusable), not just a single broken entity.

Root cause: several register definitions in const.py pass a TextReadEntityType instance as the positional unit argument of RegisterInfo(...) (numeric registers reusing an enum for display). Those entities then get a non-string unit_of_measurement — the TextReadEntityType object itself. HA 2026.7 tightened its state serializer, which now raises:

TypeError: Type is not JSON serializable: TextReadEntityType

Because that happens in the shared /api/states serialization path, it fails the whole response, taking every entity down with it. Example offender: vebus Microgrid error (state_class=measurement, unit_of_measurement=<TextReadEntityType object>).

Fix

Coerce any non-string unit to None when building the entity description, in both sensor.py and number.py:

native_unit_of_measurement=(registerInfo.unit if isinstance(registerInfo.unit, str) else None),

Minimal and safe — text/enum entities have no meaningful unit anyway, and their value decoding (in sensor.py) is unchanged. This guards at the description layer regardless of which const.py entries carry the bad unit.

Testing

  • HA 2026.7.4, Cerbo GX (3-phase ESS + FischerPanda genset), advanced mode, 657 entities.
  • Before: /api/states → 500; log shows the TextReadEntityType TypeError above.
  • After: /api/states → 200; all entities (incl. the writable number/select/switch/button controls) load and operate normally.

Some register definitions in const.py pass a TextReadEntityType instance as the
positional `unit` argument of RegisterInfo(...), so those entities carry a
non-string unit_of_measurement (the TextReadEntityType object). On HA 2026.7+ the
stricter state serializer raises 'TypeError: Type is not JSON serializable:
TextReadEntityType' while serializing state; because that happens in the shared
/api/states serialization it fails the entire response with a 500, not just one
entity. Coerce any non-string unit to None when building the entity description in
sensor.py and number.py.
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.

1 participant