Skip to content

Add state tracking to EntityConditionBase#169030

Merged
arturpragacz merged 5 commits into
devfrom
entity_condition_state_tracking
Apr 28, 2026
Merged

Add state tracking to EntityConditionBase#169030
arturpragacz merged 5 commits into
devfrom
entity_condition_state_tracking

Conversation

@emontnemery
Copy link
Copy Markdown
Contributor

Proposed change

Add state tracking to EntityConditionBase

This will allow us to enable duration support on a wider range of 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

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

Copilot AI review requested due to automatic review settings April 24, 2026 06:06
@emontnemery emontnemery requested a review from a team as a code owner April 24, 2026 06:06
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 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_event with an optional on_entities_update callback that reports added/removed tracked entities.
  • Add _valid_since tracking in EntityConditionBase and wire it to target-based state tracking to support duration evaluation beyond simple last_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).

Comment thread homeassistant/helpers/condition.py
Comment thread homeassistant/helpers/condition.py
Comment thread homeassistant/helpers/condition.py Outdated
Comment thread homeassistant/helpers/condition.py
Comment thread homeassistant/helpers/condition.py Outdated
@home-assistant home-assistant Bot marked this pull request as draft April 24, 2026 12:33
@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.

Copilot AI review requested due to automatic review settings April 28, 2026 06: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 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread homeassistant/helpers/condition.py Outdated
Copilot AI review requested due to automatic review settings April 28, 2026 06:50
@emontnemery emontnemery marked this pull request as ready for review April 28, 2026 06:50
@home-assistant home-assistant Bot requested a review from arturpragacz April 28, 2026 06:50
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 no new comments.

Comments suppressed due to low confidence (1)

homeassistant/helpers/target.py:418

  • Document the new on_entities_update callback parameter (when it is invoked and what the added/removed sets represent) in the async_track_target_selector_state_change_event docstring 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 arturpragacz merged commit 4eb000d into dev Apr 28, 2026
52 checks passed
@arturpragacz arturpragacz deleted the entity_condition_state_tracking branch April 28, 2026 07:14
"""
return True

def _update_valid_since(self, entity_id: str, _state: State | None) -> None:
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.

Why do we prefix state with underscore?

@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