]> git.basschouten.com Git - openhab-addons.git/commitdiff
[basicprofile] Fix statefilter check against item's value on the rhs (#17346)
authorjimtng <2554958+jimtng@users.noreply.github.com>
Fri, 30 Aug 2024 18:44:30 +0000 (04:44 +1000)
committerGitHub <noreply@github.com>
Fri, 30 Aug 2024 18:44:30 +0000 (20:44 +0200)
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
bundles/org.openhab.transform.basicprofiles/src/main/java/org/openhab/transform/basicprofiles/internal/profiles/StateFilterProfile.java
bundles/org.openhab.transform.basicprofiles/src/test/java/org/openhab/transform/basicprofiles/internal/profiles/StateFilterProfileTest.java

index e48236a070453614ba4822e9bbee3cf102536fda..e482a1ba58afeca5656b5d3bbd22e719039c7563 100644 (file)
@@ -271,33 +271,37 @@ public class StateFilterProfile implements StateProfile {
                         // This allows comparing compatible types, e.g. PercentType vs OnOffType
                         parsedValue = parsedValue.as(state.getClass());
                     }
+                }
+
+                // From hereon, don't override this.parsedValue,
+                // so it gets checked against Item's state on each call
+                State parsedValue = this.parsedValue;
 
-                    // If the values can't be converted to a type, check to see if it's an Item name
-                    if (parsedValue == null) {
-                        try {
-                            Item valueItem = itemRegistry.getItem(value);
-                            if (valueItem != null) { // ItemRegistry.getItem can return null in tests
-                                parsedValue = valueItem.getState();
-                                // Don't convert QuantityType to other types
-                                if (!(parsedValue instanceof QuantityType)) {
-                                    parsedValue = parsedValue.as(state.getClass());
-                                }
-                                logger.debug("Condition value: '{}' is an item state: '{}' ({})", value, parsedValue,
-                                        parsedValue == null ? "null" : parsedValue.getClass().getSimpleName());
+                // If the values couldn't be converted to a type, check to see if it's an Item name
+                if (parsedValue == null) {
+                    try {
+                        Item valueItem = itemRegistry.getItem(value);
+                        if (valueItem != null) { // ItemRegistry.getItem can return null in tests
+                            parsedValue = valueItem.getState();
+                            // Don't convert QuantityType to other types
+                            if (!(parsedValue instanceof QuantityType)) {
+                                parsedValue = parsedValue.as(state.getClass());
                             }
-                        } catch (ItemNotFoundException ignore) {
+                            logger.debug("Condition value: '{}' is an item state: '{}' ({})", value, parsedValue,
+                                    parsedValue == null ? "null" : parsedValue.getClass().getSimpleName());
                         }
+                    } catch (ItemNotFoundException ignore) {
                     }
+                }
 
-                    if (parsedValue == null) {
-                        if (comparisonType == ComparisonType.NEQ || comparisonType == ComparisonType.NEQ_ALT) {
-                            // They're not even type compatible, so return true for NEQ comparison
-                            return true;
-                        } else {
-                            logger.debug("Condition value: '{}' is not compatible with state '{}' ({})", value, state,
-                                    state.getClass().getSimpleName());
-                            return false;
-                        }
+                if (parsedValue == null) {
+                    if (comparisonType == ComparisonType.NEQ || comparisonType == ComparisonType.NEQ_ALT) {
+                        // They're not even type compatible, so return true for NEQ comparison
+                        return true;
+                    } else {
+                        logger.debug("Condition value: '{}' is not compatible with state '{}' ({})", value, state,
+                                state.getClass().getSimpleName());
+                        return false;
                     }
                 }
 
index 425a23273e7d8aaf8a4f3f8cb056bc11c87cb95e..e803b602aa9a3fcd1b7d9077d7262db5db836e2f 100644 (file)
@@ -615,7 +615,6 @@ public class StateFilterProfileTest {
         String linkedItemName = linkedItem.getName();
 
         String itemName = item.getName();
-        item.setState(state);
 
         when(mockContext.getConfiguration()).thenReturn(new Configuration(Map.of("conditions", operator + itemName)));
         when(mockItemRegistry.getItem(itemName)).thenReturn(item);
@@ -623,7 +622,13 @@ public class StateFilterProfileTest {
         when(mockItemChannelLink.getItemName()).thenReturn(linkedItemName);
 
         StateFilterProfile profile = new StateFilterProfile(mockCallback, mockContext, mockItemRegistry);
+        item.setState(UnDefType.UNDEF);
+
+        profile.onStateUpdateFromHandler(inputState);
+        reset(mockCallback);
+        when(mockCallback.getItemChannelLink()).thenReturn(mockItemChannelLink);
 
+        item.setState(state);
         profile.onStateUpdateFromHandler(inputState);
         verify(mockCallback, times(expected ? 1 : 0)).sendUpdate(eq(inputState));
     }