KNX: Support external scene activation recording (#151218)

This commit is contained in:
Matthias Alphart
2025-08-27 17:13:49 +02:00
committed by GitHub
parent 22e70723f4
commit 2ef335f403
2 changed files with 31 additions and 6 deletions

View File

@@ -4,10 +4,10 @@ from __future__ import annotations
from typing import Any
from xknx.devices import Scene as XknxScene
from xknx.devices import Device as XknxDevice, Scene as XknxScene
from homeassistant import config_entries
from homeassistant.components.scene import Scene
from homeassistant.components.scene import BaseScene
from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
@@ -31,7 +31,7 @@ async def async_setup_entry(
async_add_entities(KNXScene(knx_module, entity_config) for entity_config in config)
class KNXScene(KnxYamlEntity, Scene):
class KNXScene(KnxYamlEntity, BaseScene):
"""Representation of a KNX scene."""
_device: XknxScene
@@ -52,6 +52,11 @@ class KNXScene(KnxYamlEntity, Scene):
f"{self._device.scene_value.group_address}_{self._device.scene_number}"
)
async def async_activate(self, **kwargs: Any) -> None:
async def _async_activate(self, **kwargs: Any) -> None:
"""Activate the scene."""
await self._device.run()
def after_update_callback(self, device: XknxDevice) -> None:
"""Call after device was updated."""
self._async_record_activation()
super().after_update_callback(device)

View File

@@ -8,6 +8,8 @@ from homeassistant.helpers import entity_registry as er
from .conftest import KNXTestKit
from tests.common import async_capture_events
async def test_activate_knx_scene(
hass: HomeAssistant, knx: KNXTestKit, entity_registry: er.EntityRegistry
@@ -30,9 +32,27 @@ async def test_activate_knx_scene(
assert entity.entity_category is EntityCategory.DIAGNOSTIC
assert entity.unique_id == "1/1/1_24"
events = async_capture_events(hass, "state_changed")
# activate scene from HA
await hass.services.async_call(
"scene", "turn_on", {"entity_id": "scene.test"}, blocking=True
)
# assert scene was called on bus
await knx.assert_write("1/1/1", (0x17,))
assert len(events) == 1
# consecutive call from HA
await hass.services.async_call(
"scene", "turn_on", {"entity_id": "scene.test"}, blocking=True
)
await knx.assert_write("1/1/1", (0x17,))
assert len(events) == 2
# scene activation from bus
await knx.receive_write("1/1/1", (0x17,))
assert len(events) == 3
# same scene number consecutive call
await knx.receive_write("1/1/1", (0x17,))
assert len(events) == 4
# different scene number - should not be recorded
await knx.receive_write("1/1/1", (0x00,))
assert len(events) == 4