Use official entity state formatting functions when available.#336
Use official entity state formatting functions when available.#336mill1000 wants to merge 3 commits into
Conversation
…es unless a specific format override is used.
|
Please merge, I have been using this fork. |
|
Paging @benct |
|
Seems like @benct abandoned this project or doesn't have time to update it. |
|
My fork has this fix and I just add it as a custom repository in HACs. |
Thanks, worked great! |
|
Thanks for this — the approach (delegating to HA's official |
HA 2023.9+ exposes hass.formatEntityState/formatEntityAttributeValue, which apply the user's own locale/number-format/precision preferences the same way HA's own UI does. Delegate to them when present instead of this project's own formatNumber/computeStateDisplay reimplementation, falling back to the existing logic otherwise. A custom `format:` config still takes precedence over both. Based on the approach in benct#336 (originally by @mill1000), which should close benct#333, benct#308, benct#286, and benct#220 - a cluster of long-standing decimal/precision formatting complaints. That PR alone would have introduced a regression, though: HA installs stub formatEntityState/formatEntityAttributeValue functions on initial connection that return completely raw, unformatted values (confirmed by extracting the actual implementations from a running instance's frontend bundle - the stub is literally `(e,t) => (t ?? e.state) ?? ""`), and swaps in the real implementations asynchronously. This is the same race benct#373/benct#379 fixed for formatEntityName, but benct#336 didn't extend that re-render check to the new functions it depends on - so cards would have been stuck showing raw values whenever the swap happened after initial render, same bug class as benct#370/benct#371 but for values instead of names. Generalized hasConfigOrEntitiesChanged's formatEntityName check into a list covering all three formatter keys, and added tests simulating the swap for each. Also added tests covering the new delegation paths in entityStateDisplay, which the original PR's diff had zero coverage for. Fixes benct#333, benct#308, benct#286, benct#220 Co-Authored-By: Tucker Kern <mill1000@users.noreply.github.com> Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
HA 2023.9 introduced new functions to enable custom cards to format entity state and entity attributes values.
This PR uses these new functions (if supported) unless the user specified a custom format option.
More here: https://developers.home-assistant.io/docs/frontend/data#entity-state-formatting
Should close #333, close #308, close #286 and maybe #220