Skip to content

Allow targeting non-primary entities in triggers#168857

Merged
MartinHjelmare merged 4 commits into
devfrom
non_primary_entity_trigger
Apr 28, 2026
Merged

Allow targeting non-primary entities in triggers#168857
MartinHjelmare merged 4 commits into
devfrom
non_primary_entity_trigger

Conversation

@abmantis
Copy link
Copy Markdown
Member

@abmantis abmantis commented Apr 22, 2026

Proposed change

Add support for extracting non-primary entities to target helpers and allow triggers to work for non-primary entities.

#168860 should be merged first, so that when this is added to triggers the entity extraction on the frontend also works.

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

  • 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:

Copilot AI review requested due to automatic review settings April 22, 2026 21:53
@abmantis abmantis requested a review from a team as a code owner April 22, 2026 21:53
@home-assistant
Copy link
Copy Markdown
Contributor

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

Code owner commands

Code owners of battery 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 battery 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

Adds an opt-out for the “primary entities only” filtering during indirect target expansion so purpose-specific triggers (notably battery triggers) can target diagnostic/config entities when expanding via floor/area/device/label.

Changes:

  • Introduces a primary_entities_only flag to target expansion/tracking and threads it through entity trigger helpers.
  • Updates battery triggers to include non-primary entities (diagnostic/config) and updates trigger selector metadata accordingly.
  • Adjusts/extends tests and shared test helpers to model diagnostic entities and validate the new behavior.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
homeassistant/helpers/target.py Adds primary_entities_only flag to indirect expansion and to target state tracking setup.
homeassistant/helpers/trigger.py Allows trigger classes/factories to control whether indirect expansion includes non-primary entities.
homeassistant/helpers/selector.py Extends the target selector config schema to accept primary_entities_only.
homeassistant/components/battery/trigger.py Configures battery triggers to include non-primary entities during indirect expansion.
homeassistant/components/battery/triggers.yaml Updates battery trigger target selector config to set primary_entities_only: false.
tests/components/common.py Enhances shared target entity factory to create registry entities with a given EntityCategory.
tests/components/battery/test_trigger.py Makes battery-target entities diagnostic to validate that battery triggers work for non-primary entities.
tests/helpers/test_target.py Adds coverage verifying primary_entities_only controls inclusion of config/diagnostic entities in indirect expansion.

Comment thread homeassistant/helpers/target.py Outdated
@abmantis abmantis marked this pull request as draft April 22, 2026 21:59
@abmantis abmantis force-pushed the non_primary_entity_trigger branch from 05d73e2 to 874fb28 Compare April 22, 2026 22:00
Copilot AI review requested due to automatic review settings April 22, 2026 22:00
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 8 out of 8 changed files in this pull request and generated 2 comments.

Comment thread homeassistant/helpers/selector.py
Comment thread homeassistant/components/battery/trigger.py
@abmantis abmantis changed the base branch from dev to extract_from_target_diag April 23, 2026 15:45
@abmantis abmantis force-pushed the non_primary_entity_trigger branch from e05e7a6 to c65c502 Compare April 23, 2026 17:01
Base automatically changed from extract_from_target_diag to dev April 24, 2026 07:13
Copilot AI review requested due to automatic review settings April 24, 2026 14:29
@abmantis abmantis marked this pull request as ready for review April 24, 2026 14:30
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 6 out of 6 changed files in this pull request and generated 1 comment.

Comment thread homeassistant/helpers/trigger.py
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

I think we should add a test for the selector changes.

@home-assistant home-assistant Bot marked this pull request as draft April 27, 2026 09:51
@home-assistant
Copy link
Copy Markdown
Contributor

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.

@abmantis abmantis marked this pull request as ready for review April 27, 2026 11:54
Copilot AI review requested due to automatic review settings April 27, 2026 11:54
@home-assistant home-assistant Bot requested a review from MartinHjelmare April 27, 2026 11:54
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 no new comments.

Copy link
Copy Markdown
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.

A general question: The "primary entities only" is added as a configuration option for the target selector in this PR.
It doesn't seem entirely far fetched to also make it part of the selection, that is, allow the user to control the flag in the selector.
Would the changes in this PR be compatible with such a future extension?

@abmantis
Copy link
Copy Markdown
Member Author

A general question: The "primary entities only" is added as a configuration option for the target selector in this PR. It doesn't seem entirely far fetched to also make it part of the selection, that is, allow the user to control the flag in the selector. Would the changes in this PR be compatible with such a future extension?

Yes, I expect so. This can serve as the default for the trigger, and the trigger user options could override it when calling async_track_target_selector_state_change_event.

@MartinHjelmare MartinHjelmare merged commit eb85d7c into dev Apr 28, 2026
52 checks passed
@MartinHjelmare MartinHjelmare deleted the non_primary_entity_trigger branch April 28, 2026 09:49
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 29, 2026
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.

4 participants