Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add state_translated function to jinja templates #96906

Merged

Conversation

PiotrMachowski
Copy link
Contributor

@PiotrMachowski PiotrMachowski commented Jul 19, 2023

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: #65743

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

  • 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 Black (black --fast homeassistant tests)
  • 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:

Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Very nice @PiotrMachowski !

Some comments though.

homeassistant/helpers/translation.py Outdated Show resolved Hide resolved
homeassistant/helpers/translation.py Outdated Show resolved Hide resolved
homeassistant/helpers/translation.py Outdated Show resolved Hide resolved
homeassistant/helpers/template.py Outdated Show resolved Hide resolved
homeassistant/helpers/translation.py Outdated Show resolved Hide resolved
homeassistant/helpers/translation.py Outdated Show resolved Hide resolved
homeassistant/helpers/translation.py Outdated Show resolved Hide resolved
homeassistant/config.py Outdated Show resolved Hide resolved
homeassistant/config.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft July 20, 2023 07:57
@home-assistant
Copy link

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

Learn more about our pull request process.

@PiotrMachowski PiotrMachowski marked this pull request as ready for review July 21, 2023 01:47
@PiotrMachowski
Copy link
Contributor Author

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

homeassistant/config.py Outdated Show resolved Hide resolved
homeassistant/helpers/translation.py Outdated Show resolved Hide resolved
homeassistant/config.py Outdated Show resolved Hide resolved
homeassistant/config.py Outdated Show resolved Hide resolved
homeassistant/config.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft July 21, 2023 11:27
@emontnemery emontnemery marked this pull request as ready for review February 7, 2024 18:49
Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Looks great, just a few minor remarks 👍

homeassistant/bootstrap.py Outdated Show resolved Hide resolved
homeassistant/helpers/translation.py Outdated Show resolved Hide resolved
homeassistant/helpers/translation.py Outdated Show resolved Hide resolved
homeassistant/helpers/translation.py Outdated Show resolved Hide resolved
homeassistant/helpers/translation.py Show resolved Hide resolved
Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Thanks, @PiotrMachowski 👍

@emontnemery emontnemery self-requested a review February 8, 2024 18:31
@emontnemery emontnemery merged commit a2f4e99 into home-assistant:dev Feb 10, 2024
53 checks passed
@PiotrMachowski
Copy link
Contributor Author

Thank you, @emontnemery!

@github-actions github-actions bot locked and limited conversation to collaborators Feb 11, 2024
@bdraco
Copy link
Member

bdraco commented Feb 15, 2024

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

@bdraco
Copy link
Member

bdraco commented Feb 15, 2024

#110674

@home-assistant home-assistant unlocked this conversation Feb 15, 2024
@PiotrMachowski
Copy link
Contributor Author

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

@bdraco Sorry ☹️

@bdraco
Copy link
Member

bdraco commented Feb 15, 2024

No worries at all. It would be great if you could test #110674 to make sure it doesn't cause any regressions in the new functionality

@PiotrMachowski
Copy link
Contributor Author

Sure, I'll check it out 👍

@github-actions github-actions bot locked and limited conversation to collaborators Feb 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants