Skip to content

Commit fd2ca25

Browse files
Add externally activated scene documentation (#2755)
Co-authored-by: Martin Hjelmare <[email protected]>
1 parent c021389 commit fd2ca25

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

docs/core/entity/scene.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Other properties that are common to all entities such as `icon` and `name` etc a
2323
Activate the scene.
2424

2525
```python
26-
class MySwitch(Scene):
26+
class MyScene(Scene):
2727
# Implement one of these methods.
2828
2929
def activate(self, **kwargs: Any) -> None:
@@ -36,6 +36,28 @@ class MySwitch(Scene):
3636
The activate method can be used to activate the scene towards a device or service.
3737
It is called by Home Assistant when the user presses the scene `activate` button or when the `scene.turn_on` action is called to activate the scene.
3838

39+
Some integrations can receive external events 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 physical buttons.
40+
41+
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.
42+
43+
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.
44+
45+
```python
46+
# Inherit from BaseScene
47+
class MyScene(BaseScene):
48+
49+
# Note the leading underscore
50+
async def _async_activate(self, **kwargs: Any) -> None:
51+
"""Activate scene."""
52+
# Call a service to activate scene
53+
await mqtt.async_publish(self.hass, self._topic, self._payload)
54+
55+
# Record the activation in the callback of your service
56+
async def _state_received(self, msg: ReceiveMessage) -> None:
57+
self._async_record_activation()
58+
self.async_write_ha_state()
59+
```
60+
3961
### Available device classes
4062

4163
There are no specific device classes. The `device_class` attribute is not set on the scene entity.

0 commit comments

Comments
 (0)