Add state_attr_translated template filter and function#165317
Add state_attr_translated template filter and function#165317
Conversation
There was a problem hiding this comment.
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_attrtranslation helper to resolve attribute-value translation keys (entity-specific, then domain/device-class fallbacks). - Expose
state_attr_translatedas both a template global and a filter via a newStateAttrTranslatedcallable. - 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. |
| return async_translate_state_attr( | ||
| self._hass, | ||
| str(attr_value), | ||
| domain, | ||
| platform, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
|
I'm not sure about Copilot suggestion. Should we preserve type for non-string attributes? |
Proposed change
Add
state_attr_translatedJinja template function to translate entity attribute values, similar to the existingstate_translatedfor entity states.Attributes like
fan_mode,hvac_action,preset_mode, etc. all have translations defined instrings.jsonunderstate_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
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: