Skip to content

Commit

Permalink
[basicprofiles] Convert to relative unit in State Filter's Delta check
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng committed Jan 19, 2025
1 parent 0b3383b commit 3c248fa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ public boolean check(State input) {
State rhsState = this.rhsState;
Item lhsItem = null;
Item rhsItem = null;
boolean isDeltaCheck = false;

if (rhsState == null) {
rhsItem = getItemOrNull(rhsString);
Expand Down Expand Up @@ -380,6 +381,9 @@ public boolean check(State input) {
logger.debug("Couldn't calculate the left hand side function '{}'", lhsString);
return false;
}
if (lhsFunction.getType() == FunctionType.Function.DELTA) {
isDeltaCheck = true;
}
}

if (rhsState == null) {
Expand All @@ -388,6 +392,10 @@ public boolean check(State input) {

// Don't convert QuantityType to other types, so that 1500 != 1500 W
if (rhsState != null && !(rhsState instanceof QuantityType)) {
if (rhsState instanceof FunctionType rhsFunction
&& rhsFunction.getType() == FunctionType.Function.DELTA) {
isDeltaCheck = true;
}
// Try to convert it to the same type as the lhs
// This allows comparing compatible types, e.g. PercentType vs OnOffType
rhsState = rhsState.as(lhsState.getClass());
Expand Down Expand Up @@ -425,6 +433,12 @@ public boolean check(State input) {

rhs = Objects.requireNonNull(rhsState instanceof StringType ? rhsState.toString() : rhsState);

if (isDeltaCheck && rhs instanceof QuantityType rhsQty && lhs instanceof QuantityType lhsQty) {
if (rhsQty.toUnitRelative(lhsQty.getUnit()) instanceof QuantityType relativeRhs) {
rhs = relativeRhs;
}
}

if (logger.isDebugEnabled()) {
if (lhsString.isEmpty()) {
logger.debug("Performing a comparison between input '{}' ({}) and value '{}' ({})", lhs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,10 +670,12 @@ public void testComparingInputStateWithItem(GenericItem linkedItem, State inputS

public static Stream<Arguments> testFunctions() {
NumberItem powerItem = new NumberItem("Number:Power", "powerItem", UNIT_PROVIDER);
NumberItem temperatureItem = new NumberItem("Number:Temperature", "temperatureItem", UNIT_PROVIDER);
NumberItem decimalItem = new NumberItem("decimalItem");
List<Number> numbers = List.of(1, 2, 3, 4, 5);
List<Number> negatives = List.of(-1, -2, -3, -4, -5);
List<QuantityType> quantities = numbers.stream().map(n -> new QuantityType(n, Units.WATT)).toList();
List<QuantityType> celsiusQuantities = numbers.stream().map(n -> new QuantityType(n, SIUnits.CELSIUS)).toList();
List<DecimalType> decimals = numbers.stream().map(DecimalType::new).toList();
List<DecimalType> negativeDecimals = negatives.stream().map(DecimalType::new).toList();

Expand All @@ -690,6 +692,14 @@ public static Stream<Arguments> testFunctions() {
Arguments.of(decimalItem, "$DELTA >= 1", decimals, DecimalType.valueOf("10"), true), //
Arguments.of(decimalItem, "$DELTA >= 1", decimals, DecimalType.valueOf("5.5"), false), //

// Test for the use of relative unit in DELTA checks
Arguments.of(temperatureItem, "$DELTA > 0.2 °F", celsiusQuantities, QuantityType.valueOf("5.1 °C"),
false), //
Arguments.of(temperatureItem, "0.2 °F < $DELTA", celsiusQuantities, QuantityType.valueOf("5.1 °C"),
false), //
Arguments.of(temperatureItem, "$DELTA < 2 °F", celsiusQuantities, QuantityType.valueOf("6 °C"), true), //
Arguments.of(temperatureItem, "2 °F > $DELTA", celsiusQuantities, QuantityType.valueOf("6 °C"), true), //

Arguments.of(decimalItem, "$DELTA_PERCENT >= 10", decimals, DecimalType.valueOf("4.6"), false), //
Arguments.of(decimalItem, "$DELTA_PERCENT >= 10", decimals, DecimalType.valueOf("4.5"), true), //
Arguments.of(decimalItem, "$DELTA_PERCENT >= 10", decimals, DecimalType.valueOf("5.4"), false), //
Expand Down

0 comments on commit 3c248fa

Please sign in to comment.