Skip to content

Add state_attr_translated template filter and function#165317

Open
piitaya wants to merge 2 commits intodevfrom
state_attr_translated
Open

Add state_attr_translated template filter and function#165317
piitaya wants to merge 2 commits intodevfrom
state_attr_translated

Conversation

@piitaya
Copy link
Member

@piitaya piitaya commented Mar 11, 2026

Proposed change

Add state_attr_translated Jinja template function to translate entity attribute values, similar to the existing state_translated for entity states.

Attributes like fan_mode, hvac_action, preset_mode, etc. all have translations defined in strings.json under state_attributes, but these weren't accessible from templates until now.

Usage

{{ state_attr_translated("climate.living_room", "fan_mode") }}
{{ "climate.living_room" | state_attr_translated("fan_mode") }}

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

@piitaya piitaya marked this pull request as ready for review March 11, 2026 09:43
@piitaya piitaya requested a review from a team as a code owner March 11, 2026 09:43
Copilot AI review requested due to automatic review settings March 11, 2026 09:43
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Jinja template helper/filter, state_attr_translated, to translate entity attribute values using backend translation caches, analogous to existing state_translated for entity states.

Changes:

  • Add async_translate_state_attr translation helper to resolve attribute-value translation keys (entity-specific, then domain/device-class fallbacks).
  • Expose state_attr_translated as both a template global and a filter via a new StateAttrTranslated callable.
  • Add unit tests covering translation helper behavior and template rendering behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
homeassistant/helpers/translation.py Adds cached-translation lookup helper for attribute values.
homeassistant/helpers/template/__init__.py Registers new template global/filter and implements StateAttrTranslated.
tests/helpers/test_translation.py Adds tests for the new translation helper’s key resolution / fallback behavior.
tests/helpers/template/test_init.py Adds template-level tests for the new global/filter behavior.

Comment on lines +837 to +841
return async_translate_state_attr(
self._hass,
str(attr_value),
domain,
platform,
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state_attr_translated always casts the attribute value to str before translating. If a user calls it on a non-string attribute (e.g., numeric/boolean), the return value will change type even when no translation exists, which can break template comparisons/logic. Consider only attempting translation when attr_value is a str (or Enum) and otherwise return the original attribute value unchanged; update the return type accordingly.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Copy link
Contributor

Copilot AI commented Mar 11, 2026

@piitaya I've opened a new pull request, #165320, to work on those changes. Once the pull request is ready, I'll request review from you.

@piitaya
Copy link
Member Author

piitaya commented Mar 11, 2026

I'm not sure about Copilot suggestion. Should we preserve type for non-string attributes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants