mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[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:
parent
4643c1cca4
commit
bd8704af72
@ -271,33 +271,37 @@ public class StateFilterProfile implements StateProfile {
|
||||
// This allows comparing compatible types, e.g. PercentType vs OnOffType
|
||||
parsedValue = parsedValue.as(state.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
// 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());
|
||||
// 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 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user