Skip to content

Allow extracting non-primary entities in websocket command#168860

Merged
wendevlin merged 2 commits into
devfrom
extract_from_target_diag
Apr 24, 2026
Merged

Allow extracting non-primary entities in websocket command#168860
wendevlin merged 2 commits into
devfrom
extract_from_target_diag

Conversation

@abmantis
Copy link
Copy Markdown
Member

@abmantis abmantis commented Apr 22, 2026

Proposed change

Add a new primary_entities_only attribute to the extract_from_target API to allow extracting non-primary entities.

This is required for triggers that need to target non-primary entities. Frontend gets the info from the trigger description, as show in https://github.com/home-assistant/core/pull/168857/changes#diff-59d5f644bc2db2d5c73c9c6e802e8d3e0f24b4d8cde68c8931b029658eeb2d9cR36

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:

@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 (websocket_api) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of websocket_api 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 websocket_api 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 adds a new primary_entities_only flag to target extraction so callers (notably websocket clients) can optionally include non-primary (config/diagnostic) entities when expanding targets.

Changes:

  • Extend homeassistant.helpers.target.async_extract_referenced_entity_ids with a kw-only primary_entities_only parameter and thread it through target tracking.
  • Add primary_entities_only to the extract_from_target websocket command schema and pass it through to target extraction.
  • Add tests covering the new flag in both helper- and websocket-level test suites.

Reviewed changes

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

File Description
homeassistant/helpers/target.py Adds primary_entities_only option and uses it to control entity-category filtering during indirect expansion.
homeassistant/components/websocket_api/commands.py Exposes primary_entities_only on the extract_from_target websocket command and forwards it into extraction.
tests/helpers/test_target.py Adds unit coverage asserting indirect expansion changes when toggling primary_entities_only.
tests/components/websocket_api/test_commands.py Adds websocket-level coverage verifying extract_from_target includes/excludes config/diagnostic entities based on the flag.

Comment thread homeassistant/helpers/target.py Outdated
Comment thread homeassistant/components/websocket_api/commands.py

def _include_entry(entry: er.RegistryEntry) -> bool:
"""Return True if the entry should be included in indirect expansion."""
if entry.hidden_by is not None:
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare Apr 23, 2026

Choose a reason for hiding this comment

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

Side note: I think it's weird that we let "hidden" influence the target selection. "hidden" is used to hide entities from a dashboard. That should not matter for how the entity is used in automations.

I had to change this setting to allow me to create working automations which was confusing and disappointing.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The thing is that the description specifically mentions this:
image

So for now we need to keep it.

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.

Yeah, I know. I think the decision to use it like that was wrong.

It's another case where we tied more than a single feature, and the features are semantically distinct, to the same API. That always backfires.

target_selection: TargetSelection,
expand_group: bool = True,
*,
primary_entities_only: bool = True,
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare Apr 23, 2026

Choose a reason for hiding this comment

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

I think long term I'd invert this setting. I think it's an edge case where we don't want to include all entities.

The only case I can think of is when controlling area ambient lights and you have some other light setting on a device in the area.

All entities with a device class you would want to include by default in the selection that targets that device class.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For now we only have 1 trigger/condition where we need it, which means that the majority of them don't.
I am not saying that we should not make the default target all, though, but for now lets keep it backwards compatible since that would be a bigger change.

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.

Yeah, we can't change this easily, but I think we should long term. We have created a default rule for the edge case instead of the other way around.

@MartinHjelmare
Copy link
Copy Markdown
Member

Waiting for linked frontend PR.

@MartinHjelmare MartinHjelmare marked this pull request as draft April 23, 2026 06:30
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 23, 2026 10:16
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 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread homeassistant/helpers/target.py
@wendevlin
Copy link
Copy Markdown
Member

Frontend PR:

@MartinHjelmare
Copy link
Copy Markdown
Member

Can be merged when frontend PR is approved.

@wendevlin wendevlin marked this pull request as ready for review April 24, 2026 07:13
@wendevlin wendevlin merged commit 6a57382 into dev Apr 24, 2026
47 of 48 checks passed
@wendevlin wendevlin deleted the extract_from_target_diag branch April 24, 2026 07:13
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 25, 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