diff --git a/homeassistant/components/knx/const.py b/homeassistant/components/knx/const.py index a4e4eaceeb84..94ea65e16684 100644 --- a/homeassistant/components/knx/const.py +++ b/homeassistant/components/knx/const.py @@ -122,6 +122,7 @@ SERVICE_KNX_ATTR_REMOVE: Final = "remove" SERVICE_KNX_EVENT_REGISTER: Final = "event_register" SERVICE_KNX_EXPOSURE_REGISTER: Final = "exposure_register" SERVICE_KNX_READ: Final = "read" +SERVICE_KNX_SAVE_SCENE: Final = "save_scene" REPAIR_ISSUE_DATA_SECURE_GROUP_KEY: Final = "data_secure_group_key_issue" REPAIR_ISSUE_ENTITY_VALIDATION_ERROR: Final = "entity_validation_error" diff --git a/homeassistant/components/knx/icons.json b/homeassistant/components/knx/icons.json index e3b42be8970d..7e8916cafb92 100644 --- a/homeassistant/components/knx/icons.json +++ b/homeassistant/components/knx/icons.json @@ -51,6 +51,9 @@ "reload": { "service": "mdi:reload" }, + "save_scene": { + "service": "mdi:content-save-cog" + }, "send": { "service": "mdi:email-arrow-right" } diff --git a/homeassistant/components/knx/scene.py b/homeassistant/components/knx/scene.py index cdabb22fd57b..d85deaf2b666 100644 --- a/homeassistant/components/knx/scene.py +++ b/homeassistant/components/knx/scene.py @@ -71,6 +71,10 @@ class _KnxScene(BaseScene, _KnxEntityBase): """Activate the scene.""" await self._device.run() + async def async_save_scene(self) -> None: + """Tell KNX actuators to store their current state in this scene.""" + await self._device.learn() + @override def after_update_callback(self, device: XknxDevice) -> None: """Call after device was updated.""" diff --git a/homeassistant/components/knx/services.py b/homeassistant/components/knx/services.py index 5d113732b3cc..f424e163ab50 100644 --- a/homeassistant/components/knx/services.py +++ b/homeassistant/components/knx/services.py @@ -11,7 +11,7 @@ from xknx.telegram import Telegram from xknx.telegram.address import parse_device_group_address from xknx.telegram.apci import GroupValueRead, GroupValueResponse, GroupValueWrite -from homeassistant.const import CONF_TYPE, SERVICE_RELOAD +from homeassistant.const import CONF_TYPE, SERVICE_RELOAD, Platform from homeassistant.core import ( HomeAssistant, ServiceCall, @@ -21,7 +21,10 @@ from homeassistant.core import ( ) from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.service import async_register_admin_service +from homeassistant.helpers.service import ( + async_register_admin_service, + async_register_platform_entity_service, +) from homeassistant.util.json import JsonValueType from .const import ( @@ -35,6 +38,7 @@ from .const import ( SERVICE_KNX_EVENT_REGISTER, SERVICE_KNX_EXPOSURE_REGISTER, SERVICE_KNX_READ, + SERVICE_KNX_SAVE_SCENE, SERVICE_KNX_SEND, ) from .expose import create_knx_exposure @@ -97,6 +101,15 @@ def async_setup_services(hass: HomeAssistant) -> None: service_reload_integration, ) + async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_KNX_SAVE_SCENE, + entity_domain=Platform.SCENE, + schema=None, + func="async_save_scene", + ) + @callback def get_knx_module(hass: HomeAssistant) -> KNXModule: diff --git a/homeassistant/components/knx/services.yaml b/homeassistant/components/knx/services.yaml index 5eaaca25fd74..2b7d1e846321 100644 --- a/homeassistant/components/knx/services.yaml +++ b/homeassistant/components/knx/services.yaml @@ -74,3 +74,8 @@ exposure_register: selector: boolean: reload: +save_scene: + target: + entity: + domain: scene + integration: knx diff --git a/homeassistant/components/knx/strings.json b/homeassistant/components/knx/strings.json index 790d53fbda24..1633bde8d839 100644 --- a/homeassistant/components/knx/strings.json +++ b/homeassistant/components/knx/strings.json @@ -1821,6 +1821,10 @@ "description": "Reloads the KNX integration.", "name": "[%key:common::action::reload%]" }, + "save_scene": { + "description": "Tells KNX actuators to store their current state in the scene of the selected scene entity. Requires actuators supporting scene learning (DPT 18.001).", + "name": "Save scene" + }, "send": { "description": "Sends arbitrary data directly to the KNX bus.", "fields": { diff --git a/tests/components/knx/test_scene.py b/tests/components/knx/test_scene.py index 7653d606f367..56155415d722 100644 --- a/tests/components/knx/test_scene.py +++ b/tests/components/knx/test_scene.py @@ -1,5 +1,7 @@ """Test KNX scene.""" +import pytest + from homeassistant.components.knx.const import KNX_ADDRESS from homeassistant.components.knx.schema import SceneSchema from homeassistant.const import ( @@ -70,6 +72,35 @@ async def test_activate_knx_scene( assert len(events) == 5 +async def test_save_knx_scene( + hass: HomeAssistant, knx: KNXTestKit, caplog: pytest.LogCaptureFixture +) -> None: + """Test saving a KNX scene sends a DPT 18.001 learn telegram.""" + await knx.setup_integration( + { + SceneSchema.PLATFORM: [ + { + CONF_NAME: "test", + SceneSchema.CONF_SCENE_NUMBER: 24, + KNX_ADDRESS: "1/1/1", + }, + ] + } + ) + + events = async_capture_events(hass, "state_changed") + + await hass.services.async_call( + "knx", "save_scene", {"entity_id": "scene.test"}, blocking=True + ) + # learn bit set on the 0-based raw scene number + await knx.assert_write("1/1/1", (0x97,)) + # saving is not an activation + assert len(events) == 0 + # the outgoing telegram is decodable by the device processing it + assert "Can not process" not in caplog.text + + async def test_scene_ui_create( hass: HomeAssistant, knx: KNXTestKit,