Skip to content

fix(coercion): tolerate string/percentage input on numeric fields; widen visible falsy set#30

Open
balloob wants to merge 1 commit into
OpenDisplay:mainfrom
balloob:fix/input-coercion
Open

fix(coercion): tolerate string/percentage input on numeric fields; widen visible falsy set#30
balloob wants to merge 1 commit into
OpenDisplay:mainfrom
balloob:fix/input-coercion

Conversation

@balloob

@balloob balloob commented Jul 4, 2026

Copy link
Copy Markdown

Summary

Home Assistant templates render every value to a string, so numeric element fields (progress, size, width, radius, offset_y, pattern geometry, …) raised TypeError deep inside the handlers when a dashboard fed them templated values. This is the single highest-leverage robustness change for HA use (finding A8), plus a small fix to visible coercion (A9).

Approach

  • Added coordinates.coerce_number() — coerces ints/floats/numeric strings to a number, falling back to a default for non-numeric input. Used for plain scalar fields (widths, counts, progress) that don't take percentages.
  • Routed genuine dimension fields through the existing CoordinateParser.parse_x/parse_y/parse_size, which already coerces numeric strings and supports "50%".
  • str()-coerced text value fields.

Findings addressed

A8 — numeric fields crash on template-string input; percentage support made consistent. Fields now coerced:

Element Fields
progress_bar progress (verified "50" crash), width
multiline value (verified non-str crash), size, offset_y, y_padding, stroke_width
text y_padding, stroke_width, max_width
icon / icon_sequence size (verified "50%" crash), stroke_width, spacing
rectangle_pattern x_start/y_start (verified "10%" crash), sizes, offsets, repeats
line / rectangle / circle / ellipse width, radius
circle radius now parses "%" — parity with arc.radius
plot x_start/x_end/y_start/y_end
dlimg / qrcode xsize/ysize, boxsize/border

A9 — visible string coercion. _FALSY_VISIBLE_STRINGS gains "0", "no", "off", "none", so a template rendering {{ 0 }} (→ "0") hides the element rather than showing it. The existing test_visible.py cases that pinned the old behavior are updated to the corrected expectation.

Tests

  • test_coordinates.py: unit tests for coerce_number.
  • test_visible.py: updated to expect "0"/"no"/"off"/"none" → hidden.
  • test_string_coercion.py (new): integration tests for each verified A8 crash plus a few of the same-shape fields (circle radius %, line width, text dimensions, dlimg size).

No new failures (pre-existing tests/visual/* byte-snapshot failures under Pillow 12 are unrelated).

🤖 Generated with Claude Code

…den visible

Home Assistant templates render every value to a string, so numeric element
fields raised TypeError deep inside the handlers. This routes dimension-like
fields through CoordinateParser (which already coerces numeric strings and
percentages) and str()-coerces text, and hardens the `visible` coercion.

- A8: add coordinates.coerce_number() for plain numeric fields, and route
  dimension fields through parse_x/parse_y/parse_size:
    * progress_bar.progress ("50"), progress_bar/line/rect/circle/ellipse width
    * multiline.value (non-str), multiline.size/offset_y/y_padding/stroke_width
    * text.y_padding/stroke_width/max_width
    * icon.size / icon_sequence.size ("50%"), stroke_width, spacing
    * rectangle_pattern geometry (x_start "10%", sizes, offsets, repeats)
    * circle.radius (now parses "%" like arc.radius)
    * plot.x_start/x_end/y_start/y_end
    * dlimg.xsize/ysize, qrcode.boxsize/border
- A9: extend the falsy `visible` strings with "0"/"no"/"off"/"none" so a
  template rendering {{ 0 }} ("0") hides the element instead of showing it.

Adds unit tests for coerce_number and _coerce_visible, and integration tests
covering the verified A8 crash cases.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UVpLN4Rzdv7y3ud7QzutVJ
@balloob balloob requested a review from g4bri3lDev as a code owner July 4, 2026 07:31
@codecov

codecov Bot commented Jul 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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