Skip to content

Add externally activated scene documentation #2755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

thomasddn
Copy link
Contributor

@thomasddn thomasddn commented Aug 17, 2025

Proposed change

Added documentation to the Scene page of how to implement externally activated scenes.

Meanwhile did a small "typo" fix in the original code example: changed MySwitch to MyScene.

Type of change

  • Document existing features within Home Assistant
  • Document new or changing features for which there is an existing pull request elsewhere
  • Spelling or grammatical corrections, or rewording for improved clarity
  • Changes to the backend of this documentation
  • Remove stale or deprecated documentation

Checklist

  • I have read and followed the documentation guidelines.
  • I have verified that my changes render correctly in the documentation.

Additional information

Summary by CodeRabbit

  • Documentation
    • Added a new section on external scene activations, describing how integrations can notify the system and keep automations in sync.
    • Included a complete example demonstrating triggering a scene via an external message (e.g., MQTT) and updating state accordingly.
    • Standardized example class naming in activation samples for improved clarity.
    • Clarified activation semantics for non-UI scenarios.

Copy link
Contributor

coderabbitai bot commented Aug 17, 2025

📝 Walkthrough

Walkthrough

Updates scene entity documentation: renames a sample class in the activation example, adds an “external activation” section for integrations, and provides a new BaseScene subclass example showing _async_activate and _state_received. Changes are documentation-only; no runtime code edits.

Changes

Cohort / File(s) Change summary
Docs: Scene activation examples & external-activation guidance
docs/core/entity/scene.md
Renamed example class MySwitch(Scene)MyScene(Scene) in Activate example. Added new “external activation” section explaining how integrations should report external scene activations. Added a BaseScene subclass example implementing _async_activate (publishes a trigger, e.g., MQTT) and _state_received (records activation and schedules a HA state update). Documentation-only changes.

Sequence Diagram(s)

sequenceDiagram
  participant User/App
  participant Integration
  participant Broker as Message Broker
  participant HomeAssistant
  participant Automations

  User/App->>Integration: Trigger external scene
  Integration->>Broker: Publish scene activation message
  Broker-->>HomeAssistant: Deliver activation message
  HomeAssistant->>HomeAssistant: _state_received() records activation & schedules update
  HomeAssistant-->>Automations: State change fires automations
Loading

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
docs/core/entity/scene.md (3)

26-34: Good rename; consider clarifying imports in the snippet.

Renaming MySwitch → MyScene aligns the example with the domain. Minor nit: since the snippet uses Any, consider adding a brief comment like “# imports omitted for brevity (e.g., from typing import Any)” to avoid confusion for copy/paste.


39-42: Clarify statelessness vs “state updated” phrasing.

Scenes are documented as stateless above. Suggest wording that reflects recording an activation without implying an on/off state.

Apply this diff to tighten the language:

-Some integrations can receive external events (for example, physical button presses) that activate scenes outside of Home Assistant. These activations do not originate from the Home Assistant UI or service calls, but from external sources such as hardware controllers or third-party systems.
-
-To support this scenario, integrations should notify Home Assistant when an external scene activation occurs. This ensures the scene entity's state is updated and can be used in automations.
+Some integrations can receive external events (for example, physical button presses) that activate scenes outside of Home Assistant. These activations do not originate from the Home Assistant UI or service calls, but from external sources such as hardware controllers or third-party systems.
+
+To support this scenario, integrations should notify Home Assistant when an external scene activation occurs. This records the activation and schedules a state update (for example, updating a last_activated attribute) so automations can react. Note: Scenes remain stateless; this does not imply a persistent “on” state.

43-57: Verify BaseScene API and private hooks; make the example self-contained.

Great addition. A few actionable adjustments:

  • Ensure BaseScene and the protected hooks you’re documenting (_async_activate, _state_received, _async_record_activation) exist as public-to-override hooks in the linked Core PR; otherwise the docs will direct integrators to private APIs.
  • Consider adding minimal imports so types like Any and ReceiveMessage are resolved in copy/paste scenarios.
  • Add a pointer to the BaseScene symbol (path or link) just like you did for Scene to help discoverability.

Suggested doc/code tweaks:

-# Inherit from BaseScene
-class MyScene(BaseScene):
+# Inherit from BaseScene (homeassistant.components.scene.BaseScene)
+# Imports shown for completeness:
+from typing import Any
+from homeassistant.components.scene import BaseScene
+from homeassistant.components.mqtt import async_publish as mqtt_publish
+from homeassistant.components.mqtt.models import ReceiveMessage
+
+class MyScene(BaseScene):

     # Note the leading underscore
     async def _async_activate(self, **kwargs: Any) -> None:
         """Activate scene."""
         # Call a service to activate scene
-        await mqtt.async_publish(self.hass, self._topic, self._payload)
+        await mqtt_publish(self.hass, self._topic, self._payload)

     # Record the activation in the callback of your service
     async def _state_received(self, msg: ReceiveMessage) -> None:
-        self._async_record_activation()
+        # Record the activation so automations can react (e.g., updates last_activated)
+        self._async_record_activation()
         self.async_schedule_update_ha_state()

Follow-ups:

  • After this code block, add a short note that these are protected hooks provided by BaseScene and are safe to override.
  • Optionally add a link to BaseScene in the header paragraph similar to the link you provide for Scene.

To confirm symbol names and stability, please verify against the Core PR you referenced. If helpful, I can also open a small follow-up PR to add the BaseScene link and import hints once the Core PR is merged.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between dba3aea and 149abd5.

📒 Files selected for processing (1)
  • docs/core/entity/scene.md (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Redirect rules - developers-home-assistant
  • GitHub Check: Header rules - developers-home-assistant
  • GitHub Check: Pages changed - developers-home-assistant

@home-assistant
Copy link

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.

@home-assistant home-assistant bot marked this pull request as draft August 18, 2025 21:37
@thomasddn thomasddn marked this pull request as ready for review August 19, 2025 08:21
@home-assistant home-assistant bot requested a review from abmantis August 19, 2025 08:21
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
docs/core/entity/scene.md (1)

39-40: Clear rationale for external activations — good addition.

Nicely explains that activations can originate outside HA (e.g., physical buttons). This addresses prior feedback.

🧹 Nitpick comments (3)
docs/core/entity/scene.md (3)

41-41: Wording and cross-link nit: “inherit from” and link BaseScene.

Suggest minor wording tweak for precision and adding a link, to mirror the earlier link to Scene.

-To support this scenario, integrations should extend from `BaseScene` instead of `Scene`, override `_async_activate()` to handle the scene activation from the Home Assistant side, and call `_async_record_activation()` when an external scene activation occurs.
+To support this scenario, integrations should inherit from [`BaseScene`](https://github.com/home-assistant/core/blob/dev/homeassistant/components/scene/__init__.py) instead of [`Scene`](https://github.com/home-assistant/core/blob/dev/homeassistant/components/scene/__init__.py), override `_async_activate()` to handle activation initiated by Home Assistant, and call `_async_record_activation()` when an external activation is observed.

43-43: Grammar/clarity: article and phrasing around the timestamp.

Tighten wording (also matches the static analyzer hint).

-Also, since these scenes activate outside of Home Assistant, the integration may want to delay updating scene state timestamp until the external scene reports being active, even when it is activated from the Home Assistant UI.
+Also, because these scenes can be activated outside of Home Assistant, the integration might delay updating the activation timestamp until the external scene reports as active—even when the activation originates from the Home Assistant UI.

45-59: Example: add one clarifying comment to the callback.

Make it explicit that _state_received is called from an integration callback (e.g., MQTT message handler).

     # Record the activation in the callback of your service
-    async def _state_received(self, msg: ReceiveMessage) -> None:
+    # Called from your integration's message handler (e.g., MQTT) when the scene reports active
+    async def _state_received(self, msg: ReceiveMessage) -> None:
         self._async_record_activation()
         self.async_schedule_update_ha_state()
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 149abd5 and 38d931f.

📒 Files selected for processing (1)
  • docs/core/entity/scene.md (2 hunks)
🧰 Additional context used
🪛 LanguageTool
docs/core/entity/scene.md

[grammar] ~43-~43: There might be a mistake here.
Context: ...tant, the integration may want to delay updating scene state timestamp until the externa...

(QB_NEW_EN)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Redirect rules - developers-home-assistant
  • GitHub Check: Header rules - developers-home-assistant
  • GitHub Check: Pages changed - developers-home-assistant
🔇 Additional comments (1)
docs/core/entity/scene.md (1)

26-34: Typo fix in example class name looks good.

Renaming MySwitch to MyScene improves clarity and consistency with the rest of the page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants