Skip to content

Allow targeting non-primary entities in conditions#169291

Merged
justanotherariel merged 10 commits intodevfrom
non_primary_entity_conditions2
Apr 29, 2026
Merged

Allow targeting non-primary entities in conditions#169291
justanotherariel merged 10 commits intodevfrom
non_primary_entity_conditions2

Conversation

@abmantis
Copy link
Copy Markdown
Member

Proposed change

Follow up to #168857, for conditions.

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 (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.

Base automatically changed from non_primary_entity_trigger to dev April 28, 2026 09:49
Copilot AI review requested due to automatic review settings April 28, 2026 10:04
@abmantis abmantis marked this pull request as ready for review April 28, 2026 10:05
@abmantis abmantis requested a review from a team as a code owner April 28, 2026 10:05
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

Extends purpose-specific condition evaluation to optionally include non-primary (entity_category) entities when expanding indirect targets (area/device/floor/label), aligning conditions with the earlier trigger changes.

Changes:

  • Add a primary_entities_only flag to entity state and numerical condition factories, and thread it into target expansion.
  • Configure battery conditions (YAML + runtime condition factories) to include non-primary entities in indirect target expansion.
  • Update battery condition tests to use diagnostic entities to validate the new behavior.

Reviewed changes

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

File Description
homeassistant/helpers/condition.py Adds the primary_entities_only flag to entity condition bases and passes it into indirect target expansion.
homeassistant/components/battery/condition.py Instantiates battery conditions with primary_entities_only=False so indirect targets include diagnostic battery entities.
homeassistant/components/battery/conditions.yaml Updates target selector metadata to indicate non-primary entities should be included for battery conditions.
tests/components/battery/test_condition.py Adjusts fixtures to create diagnostic-category entities to ensure indirect target expansion includes them.

Comment on lines 621 to +625
targeted_entities = async_extract_referenced_entity_ids(
self._hass, self._target_selection, expand_group=False
self._hass,
self._target_selection,
expand_group=False,
primary_entities_only=self._primary_entities_only,
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

Pass the same primary_entities_only flag to the target state-change tracker so duration-based conditions track the same expanded entity set that _async_check evaluates.

Copilot uses AI. Check for mistakes.
@abmantis abmantis marked this pull request as draft April 28, 2026 14:41
Copilot AI review requested due to automatic review settings April 28, 2026 14:49
@abmantis abmantis marked this pull request as ready for review April 28, 2026 14:49
@abmantis abmantis added this to the 2026.5.0b0 milestone Apr 28, 2026
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 5 out of 5 changed files in this pull request and generated no new comments.

Copy link
Copy Markdown
Member

@justanotherariel justanotherariel left a comment

Choose a reason for hiding this comment

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

Looks good to me

Comment thread tests/helpers/test_condition.py Outdated
diagnostic_id,
) = await _create_primary_and_diagnostic_entities_in_area(hass, "test")

# Primary starts with valid attribute, diagnostic with invalid attribute
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 think 'valid'/'invalid' is a bit misleading here. Maybe 'matching' and 'non-matching'?

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.

That follows other tests like test_state_condition_attr_duration_initial_state have, but I agree that its weird. Changed it.

Copilot AI review requested due to automatic review settings April 29, 2026 09:56
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 5 out of 5 changed files in this pull request and generated 3 comments.

Comment on lines +5181 to +5184
# If diagnostic is included (primary_entities_only=False), behavior=all fails because
# the diagnostic entity is off. If excluded, only the primary is checked and it's on.
assert test(hass) is primary_entities_only

Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

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

Use test.async_check() (or test.async_check(variables=...)) instead of calling the checker as test(hass), since the hass argument is explicitly ignored for backwards compatibility and this call pattern can mislead readers into thinking the passed hass is used.

Copilot uses AI. Check for mistakes.
Comment on lines +5217 to +5225
# If diagnostic is included (primary_entities_only=False), behavior=all fails because
# the diagnostic value is below the threshold. If excluded, only the primary is
# checked and it's above.
assert test(hass) is primary_entities_only

# Both above threshold — true regardless of flag
hass.states.async_set(diagnostic_id, "75")
await hass.async_block_till_done()
assert test(hass) is True
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

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

Use test.async_check() instead of calling the checker as test(hass), since the hass argument is ignored and the callable form is only kept for backwards compatibility.

Copilot uses AI. Check for mistakes.
Comment on lines +5270 to +5275
assert test(hass) is primary_entities_only

# 3 more seconds later (6s after diagnostic became matching). Now diagnostic
# has also been matching for >= 5s → True regardless of flag.
freezer.tick(timedelta(seconds=3))
assert test(hass) is True
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

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

Use test.async_check() instead of calling the checker as test(hass), since the callable form ignores the provided hass and is kept only for backwards compatibility.

Suggested change
assert test(hass) is primary_entities_only
# 3 more seconds later (6s after diagnostic became matching). Now diagnostic
# has also been matching for >= 5s → True regardless of flag.
freezer.tick(timedelta(seconds=3))
assert test(hass) is True
assert await test.async_check() is primary_entities_only
# 3 more seconds later (6s after diagnostic became matching). Now diagnostic
# has also been matching for >= 5s → True regardless of flag.
freezer.tick(timedelta(seconds=3))
assert await test.async_check() is True

Copilot uses AI. Check for mistakes.
@justanotherariel justanotherariel merged commit 7cb1d5b into dev Apr 29, 2026
52 checks passed
@justanotherariel justanotherariel deleted the non_primary_entity_conditions2 branch April 29, 2026 10:25
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 30, 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.

Stop filtering battery-related entities out of floor/area/device/label targets

3 participants