Add state tracking to EntityConditionBase#169030
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds active state/duration tracking to EntityConditionBase so duration (for:) can work correctly for conditions where state.last_changed is insufficient (e.g., attribute-based matching or multi-state matching).
Changes:
- Extend
async_track_target_selector_state_change_eventwith an optionalon_entities_updatecallback that reports added/removed tracked entities. - Add
_valid_sincetracking inEntityConditionBaseand wire it to target-based state tracking to support duration evaluation beyond simplelast_changed. - Add new tests covering entity-set updates in target tracking and attribute-based duration behavior (including priming/eviction when entities enter/leave a label-based target).
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 optional on_entities_update callback and tracks previous vs current tracked entity sets. |
homeassistant/helpers/condition.py |
Introduces _valid_since duration tracking and uses target-based state tracking when needed. |
tests/helpers/test_target.py |
Adds coverage for the new on_entities_update behavior and optional callback usage. |
tests/helpers/test_condition.py |
Adds coverage for attribute-based duration semantics and target membership changes (label-based). |
21 tasks
arturpragacz
requested changes
Apr 24, 2026
Contributor
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
homeassistant/helpers/target.py:418
- Document the new
on_entities_updatecallback parameter (when it is invoked and what the added/removed sets represent) in theasync_track_target_selector_state_change_eventdocstring so callers can use it correctly.
entity_filter: Callable[[set[str]], set[str]] = lambda x: x,
on_entities_update: Callable[[set[str], set[str]], None] | None = None,
*,
primary_entities_only: bool = True,
) -> CALLBACK_TYPE:
"""Track state changes for entities referenced directly or indirectly in a target selector.
arturpragacz
approved these changes
Apr 28, 2026
| """ | ||
| return True | ||
|
|
||
| def _update_valid_since(self, entity_id: str, _state: State | None) -> None: |
Member
There was a problem hiding this comment.
Why do we prefix state with underscore?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed change
Add state tracking to
EntityConditionBaseThis will allow us to enable duration support on a wider range of conditions
Type of change
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: