core icon indicating copy to clipboard operation
core copied to clipboard

Add state_translated function to jinja templates

Open PiotrMachowski opened this issue 1 year ago â€ĸ 8 comments

Proposed change

This pull requests adds possibility to translate entity's state in jinja templates. This feature makes it much easier for users to send notifications including entities' states (e.g. weather status), use TTS, present data in markdown cards and probably much more.

code:

{{ states("sun.sun") }}
{{ state_translated("sun.sun") }}

result:

below_horizon
Onder de horizon

To achieve this functionality some small changes had to be done in translations loading process: state translations for configured language are loaded to cache at HA start.

Points to consider

  • use a different name for a function; suggestions: localized
  • state.default.unavailable, state.default.unknown translations are at this moment available only on FE

Possible future improvements

  • translating values for attributes
  • translating keys

Previous approach to adding this functionality: https://github.com/home-assistant/core/pull/65743

Type of change

  • [ ] Dependency upgrade
  • [ ] Bugfix (non-breaking change which fixes an issue)
  • [ ] New integration (thank you!)
  • [x] 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

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request: https://github.com/home-assistant/home-assistant.io/pull/31323

Checklist

  • [x] The code change is tested and works locally.
  • [x] Local tests pass. Your PR cannot be merged unless tests pass
  • [x] There is no commented out code in this PR.
  • [x] I have followed the development checklist
  • [x] I have followed the perfect PR recommendations
  • [x] The code has been formatted using Black (black --fast homeassistant tests)
  • [x] Tests have been added to verify that the new code works.

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 link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • [ ] Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

PiotrMachowski avatar Jul 19 '23 14:07 PiotrMachowski

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:

Learn more about our pull request process.

home-assistant[bot] avatar Jul 20 '23 07:07 home-assistant[bot]

@emontnemery thank you for your review, I have adjusted the code according to your suggestions

PiotrMachowski avatar Jul 21 '23 01:07 PiotrMachowski

Thank you @emontnemery! I'll check your comments and adjust the code later

PiotrMachowski avatar Nov 30 '23 20:11 PiotrMachowski

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes. Thank you for your contribution!

github-actions[bot] avatar Jan 30 '24 16:01 github-actions[bot]

Please keep it open, I have started working on it again

PiotrMachowski avatar Jan 30 '24 16:01 PiotrMachowski

I have applied suggestions from code review, but there are also some additional fixes needed -> work in progress.

PiotrMachowski avatar Feb 01 '24 02:02 PiotrMachowski

@PiotrMachowski great to hear you're still on this 👍 👍 You can contact me on discord, I'm @emontnemery there too, if you need any help with completing the PR.

emontnemery avatar Feb 01 '24 20:02 emontnemery

@emontnemery thank you! There was a bit of refactor made in translations which potentially broke (a little bit) my PR. I have to adjust some tests and check if everything works properly.

PiotrMachowski avatar Feb 01 '24 21:02 PiotrMachowski

Thank you, @emontnemery!

PiotrMachowski avatar Feb 10 '24 12:02 PiotrMachowski

It looks like we create a thundering heard of executor jobs after this PR.

bdraco avatar Feb 15 '24 18:02 bdraco

https://github.com/home-assistant/core/pull/110674

bdraco avatar Feb 15 '24 20:02 bdraco

It looks like we create a thundering heard of executor jobs after this PR.

@bdraco Sorry ☚ī¸

PiotrMachowski avatar Feb 15 '24 22:02 PiotrMachowski

No worries at all. It would be great if you could test https://github.com/home-assistant/core/pull/110674 to make sure it doesn't cause any regressions in the new functionality

bdraco avatar Feb 15 '24 22:02 bdraco

Sure, I'll check it out 👍

PiotrMachowski avatar Feb 15 '24 22:02 PiotrMachowski