Skip to content

Add SceneActivityTracker to track active scenes per Hue group#610

Open
sliekens wants to merge 4 commits into
home-assistant-libs:mainfrom
sliekens:feat/scene-activity-tracker
Open

Add SceneActivityTracker to track active scenes per Hue group#610
sliekens wants to merge 4 commits into
home-assistant-libs:mainfrom
sliekens:feat/scene-activity-tracker

Conversation

@sliekens

@sliekens sliekens commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds GroupSceneState dataclass and SceneActivityTracker class in aiohue/v2/scene_activity.py
  • The tracker subscribes to ScenesController, seeds state from already-active scenes on start(), and notifies per-group listeners whenever a regular or smart scene transitions between active and inactive
  • Pure library code — no Home Assistant dependencies; listeners are plain Callable[[str], None] and receive the group ID on each state change

Motivation

The Home Assistant hue integration (home-assistant/core#151883) needed a central place to track which Hue scene is currently active per room/zone. Reviewers requested this logic be moved into aiohue rather than living in ha-core, since it tracks the state of library model instances.

Test plan

  • pytest tests/v2/test_scene_activity_tracker.py — 12 new tests covering active/inactive transitions for regular and smart scenes, listener subscribe/unsubscribe, start() seeding from pre-existing state, stop() cleanup, and brightness extraction
  • pytest tests/ passes in full (existing tests unaffected)

🤖 Generated with Claude Code

Adds `GroupSceneState` and `SceneActivityTracker` to `aiohue/v2/scene_activity.py`.
The tracker subscribes to the `ScenesController`, seeds initial state from any
already-active scenes on `start()`, and dispatches per-group listener callbacks
whenever a regular or smart scene transitions between active and inactive.

This is pure library logic with no Home Assistant dependencies — consumers
supply plain `Callable[[str], None]` listeners and receive the group ID on
each state change.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sliekens
sliekens marked this pull request as ready for review April 7, 2026 21:14
Copilot AI review requested due to automatic review settings April 7, 2026 21:14
Extract Scene and SmartScene branches of _apply_scene_update into
_apply_regular_scene_update and _apply_smart_scene_update to stay
within pylint's 6-return-statement limit.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 73b9181fde

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread aiohue/v2/scene_activity.py
Comment thread aiohue/v2/scene_activity.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds a small v2 utility module to centralize “which scene is active per Hue group” state tracking, intended for downstream consumers (e.g., Home Assistant) to subscribe to per-group changes without embedding model-state logic outside aiohue.

Changes:

  • Introduces GroupSceneState + SceneActivityTracker to track active regular and smart scenes per group and notify listeners.
  • Seeds initial state from already-known scenes on start() and supports start()/stop() subscription lifecycle.
  • Adds a dedicated test suite covering common active/inactive transitions, listener subscription, start seeding, stop cleanup, and brightness extraction.

Reviewed changes

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

File Description
aiohue/v2/scene_activity.py New tracker/state holder for active regular + smart scenes per group with listener callbacks.
tests/v2/test_scene_activity_tracker.py New async tests for tracker behavior and state extraction.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread aiohue/v2/scene_activity.py
Comment thread aiohue/v2/scene_activity.py
Comment thread aiohue/v2/scene_activity.py
Comment thread aiohue/v2/scene_activity.py
When a tracked active scene or smart scene is deleted, the bridge
dispatches the pre-deletion object to subscribers. Previously the
tracker ignored the event type and passed the object through
_apply_scene_update, which would re-apply active state instead of
clearing it.

Now RESOURCE_DELETED routes to _clear_deleted_scene, which clears
group state and notifies listeners only when the deleted scene was
the currently tracked active one. A isinstance guard handles the edge
case where the controller returns a raw dict for an unknown-id delete.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sliekens

Copy link
Copy Markdown
Contributor Author

@MartinHjelmare do you have time to review this week?

@sliekens

Copy link
Copy Markdown
Contributor Author

OR maybe @marcelveldt ? Do you have time to review?

@marcelveldt marcelveldt added the new-feature New feature or request label Jun 18, 2026
@marcelveldt
marcelveldt requested a review from Copilot June 18, 2026 15:29

@marcelveldt marcelveldt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sorry this took a while for me to have a look, been occupied with a bunch of projects.
In general I'm still not fully convinced about the need for this feature but the code looks good, clean and isolated enough that I'll give it the benefit of the doubt ;-)

Copilot AI left a comment

Copy link
Copy Markdown

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 2 out of 2 changed files in this pull request and generated 3 comments.

Comment on lines +53 to +55
def _handle_scene_event(
event_type: EventType, scene: Scene | SmartScene
) -> None:
Comment on lines +96 to +99
def _clear_deleted_scene(self, scene: Scene | SmartScene) -> None:
"""Clear group state when a tracked scene is deleted."""
if not isinstance(scene, (Scene, SmartScene)):
return
Comment on lines +28 to +31
active_scene_mode: str | None = None # "static" | "dynamic_palette"
active_scene_last_recall: datetime | None = None
active_scene_speed: float | None = None # 0.0–1.0 when dynamic palette active
active_scene_brightness: float | None = None # 0.0–100.0
@sliekens

Copy link
Copy Markdown
Contributor Author

Sorry this took a while for me to have a look, been occupied with a bunch of projects.

No problem. Attention is scarce everywhere now 😅.

In general I'm still not fully convinced about the need for this feature but the code looks good, clean and isolated enough that I'll give it the benefit of the doubt ;-)

@marcelveldt for context, this code was extracted from my other PR, where it was pointed out in the comments that scene activity tracking should be an abstraction provided by the library, so I'm just following that direction (https://github.com/home-assistant/core/pull/151883/changes#r3040603974)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants