Skip to content

Commit

Permalink
[mqtt] Fix issue by extending OnOffValue (openhab#5064)
Browse files Browse the repository at this point in the history
Fixes: openhab#5027

Signed-off-by: Jochen Klein <[email protected]>
  • Loading branch information
jochen314 authored and martinvw committed Mar 15, 2019
1 parent 7c2e471 commit 1a6a1be
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ public ComponentSwitch(CFactory.ComponentConfiguration componentConfiguration) {
throw new UnsupportedOperationException("Component:Switch does not support forced optimistic mode");
}

OnOffValue value = new OnOffValue(channelConfiguration.state_on, channelConfiguration.state_off,
channelConfiguration.payload_on, channelConfiguration.payload_off);

channels.put(switchChannelID,
new CChannel(this, switchChannelID, new OnOffValue(channelConfiguration.state_on, channelConfiguration.state_off),
channelConfiguration.state_topic, channelConfiguration.command_topic, channelConfiguration.name, "",
new CChannel(this, switchChannelID, value, channelConfiguration.state_topic,
channelConfiguration.command_topic, channelConfiguration.name, "",
componentConfiguration.getUpdateListener()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,45 @@
*/
@NonNullByDefault
public class OnOffValue extends Value {
private final String onString;
private final String offString;
private final String onState;
private final String offState;
private final String onCommand;
private final String offCommand;

/**
* Creates a switch On/Off type, that accepts "ON", "1" for on and "OFF","0" for off.
*/
public OnOffValue() {
super(CoreItemFactory.SWITCH, Stream.of(OnOffType.class, StringType.class).collect(Collectors.toList()));
this.onString = OnOffType.ON.name();
this.offString = OnOffType.OFF.name();
this(OnOffType.ON.name(), OnOffType.OFF.name());
}

/**
* Creates a new SWITCH On/Off value.
*
* @param onValue The ON value string. This will be compared to MQTT messages.
* values send in messages will be the same as those expected in incomming messages
*
* @param onValue The ON value string. This will be compared to MQTT messages.
* @param offValue The OFF value string. This will be compared to MQTT messages.
*/
public OnOffValue(@Nullable String onValue, @Nullable String offValue) {
this(onValue, offValue, onValue, offValue);
}

/**
* Creates a new SWITCH On/Off value.
*
* @param onState The ON value string. This will be compared to MQTT messages.
* @param offState The OFF value string. This will be compared to MQTT messages.
* @param onCommand The ON value string. This will be send in MQTT messages.
* @param offCommand The OFF value string. This will be send in MQTT messages.
*/
public OnOffValue(@Nullable String onState, @Nullable String offState, @Nullable String onCommand,
@Nullable String offCommand) {
super(CoreItemFactory.SWITCH, Stream.of(OnOffType.class, StringType.class).collect(Collectors.toList()));
this.onString = onValue == null ? OnOffType.ON.name() : onValue;
this.offString = offValue == null ? OnOffType.OFF.name() : offValue;
this.onState = onState == null ? OnOffType.ON.name() : onState;
this.offState = offState == null ? OnOffType.OFF.name() : offState;
this.onCommand = onCommand == null ? OnOffType.ON.name() : onCommand;
this.offCommand = offCommand == null ? OnOffType.OFF.name() : offCommand;
}

@Override
Expand All @@ -59,9 +76,9 @@ public void update(Command command) throws IllegalArgumentException {
state = (OnOffType) command;
} else {
final String updatedValue = command.toString();
if (onString.equals(updatedValue)) {
if (onState.equals(updatedValue)) {
state = OnOffType.ON;
} else if (offString.equals(updatedValue)) {
} else if (offState.equals(updatedValue)) {
state = OnOffType.OFF;
} else {
state = OnOffType.valueOf(updatedValue);
Expand All @@ -71,6 +88,6 @@ public void update(Command command) throws IllegalArgumentException {

@Override
public String getMQTTpublishValue() {
return (state == OnOffType.ON) ? onString : offString;
return (state == OnOffType.ON) ? onCommand : offCommand;
}
}

0 comments on commit 1a6a1be

Please sign in to comment.