Add SceneActivityTracker to track active scenes per Hue group#610
Add SceneActivityTracker to track active scenes per Hue group#610sliekens wants to merge 4 commits into
Conversation
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>
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>
There was a problem hiding this comment.
💡 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".
There was a problem hiding this comment.
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+SceneActivityTrackerto track active regular and smart scenes per group and notify listeners. - Seeds initial state from already-known scenes on
start()and supportsstart()/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.
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>
|
@MartinHjelmare do you have time to review this week? |
|
OR maybe @marcelveldt ? Do you have time to review? |
marcelveldt
left a comment
There was a problem hiding this comment.
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 ;-)
| def _handle_scene_event( | ||
| event_type: EventType, scene: Scene | SmartScene | ||
| ) -> None: |
| 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 |
| 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 |
No problem. Attention is scarce everywhere now 😅.
@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) |
Summary
GroupSceneStatedataclass andSceneActivityTrackerclass inaiohue/v2/scene_activity.pyScenesController, seeds state from already-active scenes onstart(), and notifies per-group listeners whenever a regular or smart scene transitions between active and inactiveCallable[[str], None]and receive the group ID on each state changeMotivation
The Home Assistant
hueintegration (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 extractionpytest tests/passes in full (existing tests unaffected)🤖 Generated with Claude Code