Skip to content

Fix state_attr_translated to preserve original type for non-string attributes#165320

Draft
Copilot wants to merge 2 commits intostate_attr_translatedfrom
copilot/sub-pr-165317
Draft

Fix state_attr_translated to preserve original type for non-string attributes#165320
Copilot wants to merge 2 commits intostate_attr_translatedfrom
copilot/sub-pr-165317

Conversation

Copy link
Contributor

Copilot AI commented Mar 11, 2026

state_attr_translated was unconditionally casting attribute values to str before translation lookup, meaning numeric/boolean attributes would silently change type (e.g., 42"42") even when no translation existed — breaking template comparisons.

Changes

  • StateAttrTranslated.__call__: Skip translation and return the original value unchanged when attr_value is not a str or Enum; only those types have meaningful translation keys
  • Return type: Updated from str | None to Any to reflect that non-string values pass through unmodified
  • Tests: Added coverage for numeric and boolean attributes confirming type preservation

Example

# Attribute: numeric_attr = 42
# Before: returned "42" (string) — breaks `== 42` comparisons
# After:  returns 42 (int) — type preserved when no translation applies
{% if state_attr_translated("sensor.foo", "numeric_attr") == 42 %}  {# now works correctly #}

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: piitaya <5878303+piitaya@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix feedback on state_attr_translated template filter Fix state_attr_translated to preserve original type for non-string attributes Mar 11, 2026
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.

2 participants