Skip to content

Add method _should_include to EntityTriggerBase#169837

Merged
emontnemery merged 7 commits intodevfrom
triggers_improve_first_last_checks
May 5, 2026
Merged

Add method _should_include to EntityTriggerBase#169837
emontnemery merged 7 commits intodevfrom
triggers_improve_first_last_checks

Conversation

@emontnemery
Copy link
Copy Markdown
Contributor

@emontnemery emontnemery commented May 5, 2026

Proposed change

Add method _should_include to EntityTriggerBase replacing the hardcoded list of excluded states (unavailable, unknown) which is checked when trigger behavior is first or last

This allows triggers to ignore additional states when checking if all states meet a certain criteria.

The method was introduced for the media_player mute triggers, and after some discussion we concluded we want to expand it to other triggers which consume optional state attributes.

In this PR, the media player triggers media_player.muted and media_plaer.unmuted, which introduced the method, as well as climate.target_humidity_crossed_threshold, and climate.target_humidity_crossed_threshold are migrated.

Also, test helpers are improved so the new functionality can be tested without having to write custom tests.

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

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • 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.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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 diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented May 5, 2026

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

Code owner commands

Code owners of media_player can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign media_player Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented May 5, 2026

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

Code owner commands

Code owners of climate can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign climate Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR generalizes entity trigger aggregation by adding an overridable _should_include() hook to EntityTriggerBase, then uses it to let triggers ignore states that lack optional attributes during first/last evaluation. In Home Assistant, that mainly affects media player mute triggers and climate target-humidity triggers, with shared trigger test helpers expanded to cover filtered states.

Changes:

  • Added _should_include() to the shared trigger base and routed check_all_match() / count_matches() through it.
  • Simplified media player muted/unmuted triggers to use the shared aggregation hook.
  • Introduced dedicated climate target-humidity trigger classes and expanded common trigger parametrization helpers/tests for excluded states.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
homeassistant/helpers/trigger.py Adds the shared _should_include() hook and applies it to aggregate trigger evaluation.
homeassistant/components/media_player/trigger.py Removes duplicated aggregate logic and relies on the base helper hook.
homeassistant/components/climate/trigger.py Replaces factory-generated humidity triggers with classes that exclude climates lacking humidity data.
tests/components/common.py Extends shared trigger test helpers to model separately-filtered “other” targeted entities.
tests/components/media_player/test_trigger.py Updates muted-trigger parametrization to use extra excluded states.
tests/components/climate/test_trigger.py Opts humidity trigger test cases into the new attribute-required helper behavior.

Comment thread homeassistant/helpers/trigger.py Outdated
Comment thread tests/components/common.py Outdated
Comment thread tests/components/common.py Outdated
Copilot AI review requested due to automatic review settings May 5, 2026 14:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Comment thread tests/components/media_player/test_trigger.py Outdated
Comment thread homeassistant/components/climate/trigger.py
@emontnemery emontnemery added this to the 2026.5.0 milestone May 5, 2026
Comment thread tests/components/common.py Outdated
# State for the *other* targeted entities (the ones not under direct test).
# Usually equal to `included_state`; differs when the test exercises a
# scenario where targeted-but-not-under-test entities sit in a state that
# the trigger's `_should_include` override filters out of the all/count
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The last sentence has weird grammar.

Comment on lines +95 to +98
the all/count checks. Transitioning from a no-attrs state into a target
state fires only for the muted trigger (no-attrs has is_muted=False, so
the transition into muted=True flips is_muted, but a transition into
muted=False does not).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This surprises me. I think it would be more logical to exclude transitioning from a no-attrs state to a target state from firing the trigger.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree. That behavior is not introduced by this PR though. Let's fix it in a follow-up.

Comment thread tests/components/common.py Outdated
Comment on lines +440 to +445
on the transition to a target (count=1). Pass True only when the
trigger's `is_valid_transition` actually permits transitions out of the
extra-excluded state into a target — typically true for numerical
triggers (None -> numeric is a valid change/threshold cross), false for
state-based triggers like media_player.unmuted where moving from
no-attrs to muted=False is the same is_muted value.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd expect that we don't trigger from an excluded state to a target state for numerical cross threshold triggers as we don't know the initial state location, ie on which side of the threshold we started.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, agreed. Let's fix that too.

@emontnemery emontnemery marked this pull request as draft May 5, 2026 19:25
@emontnemery
Copy link
Copy Markdown
Contributor Author

I split out the docstrings and comments to #169869, I'll merge that first and then rebase this one.

Copilot AI review requested due to automatic review settings May 5, 2026 20:12
@emontnemery emontnemery force-pushed the triggers_improve_first_last_checks branch from 363fdc9 to 2e5938b Compare May 5, 2026 20:12
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Comment thread tests/components/common.py Outdated
Comment thread tests/components/media_player/test_trigger.py Outdated
Comment thread tests/components/media_player/test_trigger.py Outdated
@emontnemery emontnemery marked this pull request as ready for review May 5, 2026 22:20
Copilot AI review requested due to automatic review settings May 5, 2026 22:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Comment thread tests/components/common.py
Comment thread tests/components/common.py
Comment on lines 1702 to +1703
for other_entity_id in other_entity_ids:
set_or_remove_state(hass, other_entity_id, included_state)
set_or_remove_state(hass, other_entity_id, others_state)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will improve this in a follow-up

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Follow-up: #169881

@emontnemery emontnemery merged commit 7e8f536 into dev May 5, 2026
52 checks passed
@emontnemery emontnemery deleted the triggers_improve_first_last_checks branch May 5, 2026 22:50
@frenck frenck modified the milestone: 2026.5.0 May 6, 2026
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.

4 participants