core
core copied to clipboard
Add state_translated function to jinja templates
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:
- [ ] Documentation added/updated for www.home-assistant.io
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 runningpython3 -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:
- [x] I have reviewed two other open pull requests in this repository.
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:
@emontnemery thank you for your review, I have adjusted the code according to your suggestions
Thank you @emontnemery! I'll check your comments and adjust the code later
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!
Please keep it open, I have started working on it again
I have applied suggestions from code review, but there are also some additional fixes needed -> work in progress.
@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 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.
Thank you, @emontnemery!
It looks like we create a thundering heard of executor jobs after this PR.
https://github.com/home-assistant/core/pull/110674
It looks like we create a thundering heard of executor jobs after this PR.
@bdraco Sorry âšī¸
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
Sure, I'll check it out đ