mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
Add parameter on Hysteresis Profile to exclude current Item State from calculation (#5556)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
+23
-8
@@ -43,8 +43,9 @@ import tech.units.indriya.AbstractUnit;
|
|||||||
public class SystemHysteresisStateProfile implements StateProfile {
|
public class SystemHysteresisStateProfile implements StateProfile {
|
||||||
|
|
||||||
static final String LOWER_PARAM = "lower";
|
static final String LOWER_PARAM = "lower";
|
||||||
private static final String UPPER_PARAM = "upper";
|
static final String UPPER_PARAM = "upper";
|
||||||
private static final String INVERTED_PARAM = "inverted";
|
static final String INVERTED_PARAM = "inverted";
|
||||||
|
static final String EXCLUDE_ITEM_STATE_PARAM = "excludeItemState";
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(SystemHysteresisStateProfile.class);
|
private final Logger logger = LoggerFactory.getLogger(SystemHysteresisStateProfile.class);
|
||||||
|
|
||||||
@@ -54,6 +55,7 @@ public class SystemHysteresisStateProfile implements StateProfile {
|
|||||||
private final QuantityType<?> upper;
|
private final QuantityType<?> upper;
|
||||||
private final OnOffType low;
|
private final OnOffType low;
|
||||||
private final OnOffType high;
|
private final OnOffType high;
|
||||||
|
private final boolean excludeItemState;
|
||||||
|
|
||||||
private Type previousType = UnDefType.UNDEF;
|
private Type previousType = UnDefType.UNDEF;
|
||||||
|
|
||||||
@@ -75,10 +77,15 @@ public class SystemHysteresisStateProfile implements StateProfile {
|
|||||||
}
|
}
|
||||||
this.upper = convertedUpperParam;
|
this.upper = convertedUpperParam;
|
||||||
|
|
||||||
final Object paramValue = context.getConfiguration().get(INVERTED_PARAM);
|
final Object invertedParamValue = context.getConfiguration().get(INVERTED_PARAM);
|
||||||
final boolean inverted = paramValue != null && Boolean.parseBoolean(paramValue.toString());
|
final boolean inverted = invertedParamValue != null && Boolean.parseBoolean(invertedParamValue.toString());
|
||||||
this.low = OnOffType.from(inverted);
|
this.low = OnOffType.from(inverted);
|
||||||
this.high = OnOffType.from(!inverted);
|
this.high = OnOffType.from(!inverted);
|
||||||
|
|
||||||
|
final Object excludeItemStateParamValue = context.getConfiguration().get(EXCLUDE_ITEM_STATE_PARAM);
|
||||||
|
this.excludeItemState = excludeItemStateParamValue != null
|
||||||
|
&& Boolean.parseBoolean(excludeItemStateParamValue.toString());
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
private @Nullable QuantityType<?> getParam(ProfileContext context, String param) {
|
private @Nullable QuantityType<?> getParam(ProfileContext context, String param) {
|
||||||
@@ -161,10 +168,18 @@ public class SystemHysteresisStateProfile implements StateProfile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Type mapValue(double lower, double upper, double value) {
|
private Type mapValue(double lower, double upper, double value) {
|
||||||
if (value <= lower) {
|
if (excludeItemState) {
|
||||||
return low;
|
if (value < lower) {
|
||||||
} else if (value >= upper) {
|
return low;
|
||||||
return high;
|
} else if (value >= upper) {
|
||||||
|
return high;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (value <= lower) {
|
||||||
|
return low;
|
||||||
|
} else if (value > upper) {
|
||||||
|
return high;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return previousType;
|
return previousType;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,5 +18,10 @@
|
|||||||
<description>Inverts resulting mapping of ON / OFF, if true.</description>
|
<description>Inverts resulting mapping of ON / OFF, if true.</description>
|
||||||
<default>false</default>
|
<default>false</default>
|
||||||
</parameter>
|
</parameter>
|
||||||
|
<parameter name="excludeItemState" type="boolean">
|
||||||
|
<label>Exclude Item State</label>
|
||||||
|
<description>Do not include Item State in comparison.</description>
|
||||||
|
<default>false</default>
|
||||||
|
</parameter>
|
||||||
</config-description>
|
</config-description>
|
||||||
</config-description:config-descriptions>
|
</config-description:config-descriptions>
|
||||||
|
|||||||
+2
@@ -10,6 +10,8 @@ profile.config.system.hysteresis.upper.label = Upper Bound
|
|||||||
profile.config.system.hysteresis.upper.description = Maps to ON if value is greater than or equal to upper bound (plain number or number with unit).
|
profile.config.system.hysteresis.upper.description = Maps to ON if value is greater than or equal to upper bound (plain number or number with unit).
|
||||||
profile.config.system.hysteresis.inverted.label = Inverted
|
profile.config.system.hysteresis.inverted.label = Inverted
|
||||||
profile.config.system.hysteresis.inverted.description = Inverts resulting mapping of ON / OFF, if true.
|
profile.config.system.hysteresis.inverted.description = Inverts resulting mapping of ON / OFF, if true.
|
||||||
|
profile.config.system.hysteresis.excludeItemState.label = Exclude Item State
|
||||||
|
profile.config.system.hysteresis.excludeItemState.description = Do not include Item State in comparison.
|
||||||
profile-type.system.range.label = Range
|
profile-type.system.range.label = Range
|
||||||
profile.config.system.range.lower.label = Lower Bound
|
profile.config.system.range.lower.label = Lower Bound
|
||||||
profile.config.system.range.lower.description = Maps to ON if value is between lower and upper bound (plain number or number with unit).
|
profile.config.system.range.lower.description = Maps to ON if value is between lower and upper bound (plain number or number with unit).
|
||||||
|
|||||||
+28
-6
@@ -185,13 +185,33 @@ public class SystemHysteresisStateProfileTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvertedParameter() {
|
public void testInvertedParameter() {
|
||||||
final StateProfile profile = initProfile(BigDecimal.TEN, null, true);
|
final StateProfile profile = initProfile(BigDecimal.TEN, null, true, false);
|
||||||
verifySendCommand(profile, PercentType.HUNDRED, OnOffType.OFF);
|
verifySendCommand(profile, PercentType.HUNDRED, OnOffType.OFF);
|
||||||
|
verifySendCommand(profile, PERCENT_TYPE_TEN, OnOffType.ON);
|
||||||
verifySendCommand(profile, PercentType.ZERO, OnOffType.ON);
|
verifySendCommand(profile, PercentType.ZERO, OnOffType.ON);
|
||||||
verifySendUpdate(profile, PercentType.HUNDRED, OnOffType.OFF);
|
verifySendUpdate(profile, PercentType.HUNDRED, OnOffType.OFF);
|
||||||
|
verifySendUpdate(profile, PERCENT_TYPE_TEN, OnOffType.ON);
|
||||||
verifySendUpdate(profile, PercentType.ZERO, OnOffType.ON);
|
verifySendUpdate(profile, PercentType.ZERO, OnOffType.ON);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInvertedParameterExcludeItemState() {
|
||||||
|
final StateProfile profile = initProfile(BigDecimal.TEN, null, true, true);
|
||||||
|
verifySendCommand(profile, PercentType.HUNDRED, OnOffType.OFF);
|
||||||
|
verifySendCommand(profile, PERCENT_TYPE_TEN, OnOffType.OFF);
|
||||||
|
verifySendCommand(profile, PercentType.ZERO, OnOffType.ON);
|
||||||
|
verifySendUpdate(profile, PercentType.HUNDRED, OnOffType.OFF);
|
||||||
|
verifySendUpdate(profile, PERCENT_TYPE_TEN, OnOffType.OFF);
|
||||||
|
verifySendUpdate(profile, PercentType.ZERO, OnOffType.ON);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExcludeItemStateParameter() {
|
||||||
|
final StateProfile profile = initProfile(BigDecimal.TEN, null, false, true);
|
||||||
|
verifySendCommand(profile, PERCENT_TYPE_TEN, OnOffType.ON);
|
||||||
|
verifySendUpdate(profile, PERCENT_TYPE_TEN, OnOffType.ON);
|
||||||
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@MethodSource("parameters")
|
@MethodSource("parameters")
|
||||||
public void testOnCommandFromHandler(ParameterSet parameterSet) {
|
public void testOnCommandFromHandler(ParameterSet parameterSet) {
|
||||||
@@ -211,14 +231,16 @@ public class SystemHysteresisStateProfileTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private StateProfile initProfile(@Nullable Object lower, @Nullable Object upper) {
|
private StateProfile initProfile(@Nullable Object lower, @Nullable Object upper) {
|
||||||
return initProfile(lower, upper, false);
|
return initProfile(lower, upper, false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private StateProfile initProfile(@Nullable Object lower, @Nullable Object upper, boolean inverted) {
|
private StateProfile initProfile(@Nullable Object lower, @Nullable Object upper, boolean inverted,
|
||||||
|
boolean excludeItemState) {
|
||||||
final Map<String, @Nullable Object> properties = new HashMap<>(2);
|
final Map<String, @Nullable Object> properties = new HashMap<>(2);
|
||||||
properties.put("lower", lower);
|
properties.put(SystemHysteresisStateProfile.LOWER_PARAM, lower);
|
||||||
properties.put("upper", upper);
|
properties.put(SystemHysteresisStateProfile.UPPER_PARAM, upper);
|
||||||
properties.put("inverted", inverted);
|
properties.put(SystemHysteresisStateProfile.INVERTED_PARAM, inverted);
|
||||||
|
properties.put(SystemHysteresisStateProfile.EXCLUDE_ITEM_STATE_PARAM, excludeItemState);
|
||||||
when(mockContext.getConfiguration()).thenReturn(new Configuration(properties));
|
when(mockContext.getConfiguration()).thenReturn(new Configuration(properties));
|
||||||
return new SystemHysteresisStateProfile(mockCallback, mockContext);
|
return new SystemHysteresisStateProfile(mockCallback, mockContext);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user