[basicprofile] Fix statefilter check against item's value on the rhs (#17346)

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
jimtng 2024-08-31 04:44:30 +10:00 committed by Ciprian Pascu
parent 4643c1cca4
commit bd8704af72
2 changed files with 32 additions and 23 deletions

View File

@ -271,33 +271,37 @@ public class StateFilterProfile implements StateProfile {
// This allows comparing compatible types, e.g. PercentType vs OnOffType // This allows comparing compatible types, e.g. PercentType vs OnOffType
parsedValue = parsedValue.as(state.getClass()); parsedValue = parsedValue.as(state.getClass());
} }
}
// If the values can't be converted to a type, check to see if it's an Item name // From hereon, don't override this.parsedValue,
if (parsedValue == null) { // so it gets checked against Item's state on each call
try { State parsedValue = this.parsedValue;
Item valueItem = itemRegistry.getItem(value);
if (valueItem != null) { // ItemRegistry.getItem can return null in tests // If the values couldn't be converted to a type, check to see if it's an Item name
parsedValue = valueItem.getState(); if (parsedValue == null) {
// Don't convert QuantityType to other types try {
if (!(parsedValue instanceof QuantityType)) { Item valueItem = itemRegistry.getItem(value);
parsedValue = parsedValue.as(state.getClass()); if (valueItem != null) { // ItemRegistry.getItem can return null in tests
} parsedValue = valueItem.getState();
logger.debug("Condition value: '{}' is an item state: '{}' ({})", value, parsedValue, // Don't convert QuantityType to other types
parsedValue == null ? "null" : parsedValue.getClass().getSimpleName()); 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 (parsedValue == null) {
if (comparisonType == ComparisonType.NEQ || comparisonType == ComparisonType.NEQ_ALT) { if (comparisonType == ComparisonType.NEQ || comparisonType == ComparisonType.NEQ_ALT) {
// They're not even type compatible, so return true for NEQ comparison // They're not even type compatible, so return true for NEQ comparison
return true; return true;
} else { } else {
logger.debug("Condition value: '{}' is not compatible with state '{}' ({})", value, state, logger.debug("Condition value: '{}' is not compatible with state '{}' ({})", value, state,
state.getClass().getSimpleName()); state.getClass().getSimpleName());
return false; return false;
}
} }
} }

View File

@ -615,7 +615,6 @@ public class StateFilterProfileTest {
String linkedItemName = linkedItem.getName(); String linkedItemName = linkedItem.getName();
String itemName = item.getName(); String itemName = item.getName();
item.setState(state);
when(mockContext.getConfiguration()).thenReturn(new Configuration(Map.of("conditions", operator + itemName))); when(mockContext.getConfiguration()).thenReturn(new Configuration(Map.of("conditions", operator + itemName)));
when(mockItemRegistry.getItem(itemName)).thenReturn(item); when(mockItemRegistry.getItem(itemName)).thenReturn(item);
@ -623,7 +622,13 @@ public class StateFilterProfileTest {
when(mockItemChannelLink.getItemName()).thenReturn(linkedItemName); when(mockItemChannelLink.getItemName()).thenReturn(linkedItemName);
StateFilterProfile profile = new StateFilterProfile(mockCallback, mockContext, mockItemRegistry); 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); profile.onStateUpdateFromHandler(inputState);
verify(mockCallback, times(expected ? 1 : 0)).sendUpdate(eq(inputState)); verify(mockCallback, times(expected ? 1 : 0)).sendUpdate(eq(inputState));
} }