Skip to content

Commit

Permalink
[ism8] Allow linking switch-r to Contact items (openhab#17742)
Browse files Browse the repository at this point in the history
* [ism8] Allow linking switch-... to Contact items

Signed-off-by: Holger Friedrich <[email protected]>
  • Loading branch information
holgerfriedrich authored Nov 30, 2024
1 parent 3187b01 commit 4ed9474
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
3 changes: 3 additions & 0 deletions bundles/org.openhab.binding.ism8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ For the moment, the following data types are implemented:

Date and Time types used by for CWL Excellent and CWL2 are currently not supported by the ISM8 add-on.

*Attention:* Due to a bug in the original implementation, the states for DPT 1.009 are inverted (i.e., `1` is mapped to `OPEN` instead of `CLOSE`).
A change would break all existing installations and is therefore not implemented.

## Full Example

### ism8.things
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.ism8.server.IDataPoint;
import org.openhab.core.library.dimension.VolumetricFlowRate;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.SIUnits;
Expand Down Expand Up @@ -71,8 +72,10 @@ public static State toOpenHABState(IDataPoint dataPoint) {
return new QuantityType<Dimensionless>((Double) value, Units.PERCENT);
} else if (Units.ONE.equals(unit)) {
return new QuantityType<Dimensionless>((Double) value, Units.ONE);
} else if (value instanceof Boolean) {
return OnOffType.from((boolean) value);
} else if (value instanceof Boolean b) {
// DecimalType is compatible with Switch and Contact items, OH mapping is 0-off-closed and 1-on-open;
// note that this is opposite to definition of KNX DPT 1.009
return b ? DecimalType.valueOf("1") : DecimalType.valueOf("0");
} else if (value instanceof Byte) {
return new QuantityType<Dimensionless>((byte) value, Units.ONE);
} else if (value instanceof Integer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public void processData(byte[] data) {
@Override
protected byte[] convertWriteValue(Object value) {
String valueText = value.toString().toLowerCase();
if ("true".equalsIgnoreCase(valueText) || "1".equalsIgnoreCase(valueText) || "ON".equalsIgnoreCase(valueText)) {
if ("true".equalsIgnoreCase(valueText) || "1".equalsIgnoreCase(valueText) || "ON".equalsIgnoreCase(valueText)
|| "OPEN".equalsIgnoreCase(valueText)) {
this.setValue(true);
return new byte[] { 0x01 };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.binding.ism8.internal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.openhab.binding.ism8.internal.Ism8BindingConstants.*;

Expand All @@ -31,7 +32,9 @@
import org.openhab.binding.ism8.server.DataPointValue;
import org.openhab.binding.ism8.server.IDataPoint;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.Units;
Expand All @@ -57,6 +60,7 @@ public class Ism8HandlerTest {
private @NonNullByDefault({}) Ism8Handler thingHandler;
private ThingUID thingUID = new ThingUID(BINDING_ID, "ism8server");
private ChannelUID channel1001 = new ChannelUID(thingUID, "switch1");
private ChannelUID channel1001c = new ChannelUID(thingUID, "contact1");
private ChannelUID channel9001 = new ChannelUID(thingUID, "tempC");
private ChannelUID channel9002 = new ChannelUID(thingUID, "tempD");
private ChannelUID channel20001 = new ChannelUID(thingUID, "mode1");
Expand All @@ -72,6 +76,8 @@ public void initialize() {
.withConfiguration(createChannelConfig("4", "9.001")).build())
.withChannel(ChannelBuilder.create(channel1001, "Switch")
.withConfiguration(createChannelConfig("9", "1.001")).build())
.withChannel(ChannelBuilder.create(channel1001c, "Contact")
.withConfiguration(createChannelConfig("8", "1.001")).build())
.withChannel(ChannelBuilder.create(channel20001, "Switch")
.withConfiguration(createChannelConfig("2", "20.001")).build())
.build();
Expand Down Expand Up @@ -99,7 +105,24 @@ public void process1001MessageAndUpdateChannel() {
thingHandler.dataPointChanged(event);

// assert
Mockito.verify(thingHandlerCallback).stateUpdated(eq(channel1001), eq(OnOffType.from(false)));
Mockito.verify(thingHandlerCallback).stateUpdated(eq(channel1001), eq(DecimalType.valueOf("0")));
assertEquals(OnOffType.from(false), OnOffType.from(DecimalType.valueOf("0").toString()));
}

// @Test
public void process1001cMessageAndUpdateChannel() {
// arrange
IDataPoint dataPoint = new DataPointBool(8, "1.001", "Datapoint_1.001");
dataPoint.processData(HexUtils.hexToBytes("0008030100"));
DataPointChangedEvent event = new DataPointChangedEvent(new Object(), dataPoint);
thingHandler.setCallback(thingHandlerCallback);

// act
thingHandler.dataPointChanged(event);

// assert
Mockito.verify(thingHandlerCallback).stateUpdated(eq(channel1001c), eq(DecimalType.valueOf("0")));
assertEquals(OpenClosedType.CLOSED.as(DecimalType.class), DecimalType.valueOf("0"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.openhab.binding.ism8.server.DataPointScaling;
import org.openhab.binding.ism8.server.DataPointValue;
import org.openhab.binding.ism8.server.IDataPoint;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.ImperialUnits;
import org.openhab.core.library.unit.SIUnits;
Expand Down Expand Up @@ -118,7 +120,8 @@ public void mapDataPointBoolToOHState() {
State result = Ism8DomainMap.toOpenHABState(dataPoint);

// assert
assertEquals(OnOffType.from(false), result);
assertEquals(DecimalType.valueOf("0"), result);
assertEquals(OnOffType.from(false), OnOffType.from(DecimalType.valueOf("0").toString()));
}
{
// arrange
Expand All @@ -129,7 +132,8 @@ public void mapDataPointBoolToOHState() {
State result = Ism8DomainMap.toOpenHABState(dataPoint);

// assert
assertEquals(OnOffType.from(true), result);
assertEquals(DecimalType.valueOf("1"), result);
assertEquals(OnOffType.from(true), OnOffType.from(DecimalType.valueOf("1").toString()));
}
{
// arrange
Expand All @@ -140,7 +144,8 @@ public void mapDataPointBoolToOHState() {
State result = Ism8DomainMap.toOpenHABState(dataPoint);

// assert
assertEquals(OnOffType.from(true), result);
assertEquals(DecimalType.valueOf("1"), result);
assertEquals(OnOffType.from(true), OnOffType.from(DecimalType.valueOf("1").toString()));
}
{
// arrange
Expand All @@ -151,7 +156,8 @@ public void mapDataPointBoolToOHState() {
State result = Ism8DomainMap.toOpenHABState(dataPoint);

// assert
assertEquals(OnOffType.from(true), result);
assertEquals(DecimalType.valueOf("1"), result);
assertEquals(OnOffType.from(true), OnOffType.from(DecimalType.valueOf("1").toString()));
}
{
// arrange
Expand All @@ -162,9 +168,11 @@ public void mapDataPointBoolToOHState() {
State result = Ism8DomainMap.toOpenHABState(dataPoint);

// assert
assertEquals(OnOffType.from(true), result);
// TODO: check if OpenClosedType is appropriate
// assertEquals(OpenClosedType.valueOf("OPEN"), result);
assertEquals(DecimalType.valueOf("1"), result);
assertEquals(OnOffType.ON, OnOffType.from(DecimalType.valueOf("1").toString()));
// DecimalType is compatible with Switch and Contact items, OH mapping is 0-off-closed and 1-on-open;
// note that this is opposite to definition of KNX DPT 1.009
assertEquals(OpenClosedType.CLOSED.as(DecimalType.class), DecimalType.valueOf("0"));
}
}

Expand Down

0 comments on commit 4ed9474

Please sign in to comment.