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

Update trigger based template entity resolution order #140660

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

Petro31
Copy link
Contributor

@Petro31 Petro31 commented Mar 15, 2025

There are differences between trigger based template entities and normal template entities.

Resolution order

  • Template entities resolve availability first, and if the template resolves false, no other templates are executed.
  • Trigger based template entities resolve availability first, all other templates are resolved regardless the availability result.

Resolution exception handling

  • Template entities will ignore availability if availability has an exception. This means an entity will be available if the template entity has a valid state. There are test cases (in test_light.py) that ensure this functionality occurs for regular template entities.
  • Trigger based template entities will go unavailable if any template has an exception.

Breaking change

  1. Trigger based template entities will remain available when an attribute template has an exception.
  2. Trigger based template entities will not resolve any other template if availability is false.

Proposed change

Trigger based template entities will behave the same way as template entities.

  1. Trigger based template entities will resolve in order: availability, state, (icon, picture, name, etc), attributes
  2. If availability resolves false, no other templates are executed.
  3. If availability has an exception, state and attributes will still be resolved.
  4. Attributes will resolve if other attributes have exceptions

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 Ruff (ruff format 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.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @PhracturedBlue, @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of template can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign template Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@emontnemery
Copy link
Contributor

Please improve the breaking change section, it should explain what has changed (what's the new behavior, and what was the behavior in the previous release) and how it may break things

Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this fixed in TriggerEntity instead of in TriggerBaseEntity, do other classes derived from TriggerBaseEntity never have availability templates?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't want to impact all the other integrations that use ManualTriggerEntity which inherits from TriggerBaseEntity. I can investigate how this change will impact those integrations if need be.

Comment on lines +98 to +102
return render_complex(self._config[key], variables)

return self._config[key].async_render(
variables, parse_result=key in self._parse_result, strict=strict
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can these also return _SENTINEL or can that only happen below after we caught an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That can only happen after we caught an error. This is so that the templates can render None. Originally I was only using none as the return, however that caused issues across the board because None means unknown for trigger based entities. I was forced to use _SENTINEL to allow None as a returned result.

) is not _SENTINEL:
rendered[key] = result

for key in self._to_render_complex:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't availability or state templates be complex?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not that I can tell. The original method only treats them as non-complex renders. I assumed this is because they only render a string or true/false and they won't render any complex objects like a dictionary or list.

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.

2 participants