From d26aa080ab581fdc83f866cf0e1a85ed2734dc75 Mon Sep 17 00:00:00 2001
From: Florian Hotze <florianh_dev@icloud.com>
Date: Sat, 2 Mar 2024 10:46:28 +0100
Subject: [PATCH] Script profile: Fix deprecation warning (#4110)

Refs #4058.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
---
 .../module/script/profile/ScriptProfile.java  |  6 ++--
 .../script/profile/ScriptProfileTest.java     | 31 +++++++++++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/profile/ScriptProfile.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/profile/ScriptProfile.java
index ccd0c37086..3faebddbd9 100644
--- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/profile/ScriptProfile.java
+++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/profile/ScriptProfile.java
@@ -83,8 +83,10 @@ public class ScriptProfile implements StateProfile {
         this.stateFromItemScript = ConfigParser
                 .valueAsOrElse(profileContext.getConfiguration().get(CONFIG_STATE_FROM_ITEM_SCRIPT), String.class, "");
 
-        if (!toHandlerScript.isBlank() && commandFromItemScript.isBlank()) {
-            logger.warn("'toHandlerScript' has been deprecated! Please use 'commandFromItemScript' instead.");
+        if (!toHandlerScript.isBlank() && localCommandFromItemScript.isBlank()) {
+            logger.warn(
+                    "'toHandlerScript' has been deprecated! Please use 'commandFromItemScript' instead in link '{}'.",
+                    callback.getItemChannelLink());
         }
 
         if (toItemScript.isBlank() && commandFromItemScript.isBlank() && stateFromItemScript.isBlank()) {
diff --git a/bundles/org.openhab.core.automation.module.script/src/test/java/org/openhab/core/automation/module/script/profile/ScriptProfileTest.java b/bundles/org.openhab.core.automation.module.script/src/test/java/org/openhab/core/automation/module/script/profile/ScriptProfileTest.java
index 867ee651dc..51f6cdf8e3 100644
--- a/bundles/org.openhab.core.automation.module.script/src/test/java/org/openhab/core/automation/module/script/profile/ScriptProfileTest.java
+++ b/bundles/org.openhab.core.automation.module.script/src/test/java/org/openhab/core/automation/module/script/profile/ScriptProfileTest.java
@@ -96,6 +96,37 @@ public class ScriptProfileTest extends JavaTest {
                         + link.toString() + "'. Profile will discard all states and commands.");
     }
 
+    @Test
+    public void fallsBackToToHandlerScriptIfCommandFromItemScriptNotDefined() throws TransformationException {
+        ProfileContext profileContext = ProfileContextBuilder.create().withToItemScript("inScript")
+                .withToHandlerScript("outScript").withAcceptedCommandTypes(List.of(OnOffType.class))
+                .withAcceptedDataTypes(List.of(OnOffType.class))
+                .withHandlerAcceptedCommandTypes(List.of(OnOffType.class)).build();
+
+        ItemChannelLink link = new ItemChannelLink("DummyItem", new ChannelUID("foo:bar:baz:qux"));
+        when(profileCallback.getItemChannelLink()).thenReturn(link);
+
+        when(transformationServiceMock.transform(any(), any())).thenReturn(OnOffType.OFF.toString());
+
+        setupInterceptedLogger(ScriptProfile.class, LogLevel.WARN);
+
+        ScriptProfile scriptProfile = new ScriptProfile(mock(ProfileTypeUID.class), profileCallback, profileContext,
+                transformationServiceMock);
+
+        scriptProfile.onCommandFromHandler(DecimalType.ZERO);
+        scriptProfile.onStateUpdateFromHandler(DecimalType.ZERO);
+        scriptProfile.onCommandFromItem(DecimalType.ZERO);
+
+        verify(transformationServiceMock, times(3)).transform(any(), any());
+        verify(profileCallback, times(1)).handleCommand(OnOffType.OFF);
+        verify(profileCallback).sendUpdate(OnOffType.OFF);
+        verify(profileCallback).sendCommand(OnOffType.OFF);
+
+        assertLogMessage(ScriptProfile.class, LogLevel.WARN,
+                "'toHandlerScript' has been deprecated! Please use 'commandFromItemScript' instead in link '"
+                        + link.toString() + "'.");
+    }
+
     @Test
     public void scriptExecutionErrorForwardsNoValueToCallback() throws TransformationException {
         ProfileContext profileContext = ProfileContextBuilder.create().withToItemScript("inScript")