From 6739a163ec4053a24bab3a86b623cd22a625f246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gr=C3=A4ff?= Date: Tue, 18 Dec 2018 11:59:04 +0100 Subject: [PATCH] MQTT: Rollershutter channel. Proper STOP command support. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Rollershutter channel stores its value as percentage. UP/DOWN/STOP commands received via MQTT do not change that state. Instead of trying to handle those commands, they are now posted to the framework. Signed-off-by: David Gräff --- .../homeassistant/ComponentLight.java | 4 ++-- .../internal/generic/ChannelState.java | 9 +++++++- .../generic/ChannelStateUpdateListener.java | 2 +- .../handler/AbstractMQTTThingHandler.java | 2 +- .../internal/values/RollershutterValue.java | 22 ++++++++++++------- .../mqtt/generic/internal/values/Value.java | 12 ++++++++++ 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/convention/homeassistant/ComponentLight.java b/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/convention/homeassistant/ComponentLight.java index cd49826bcfb..7053df3aea5 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/convention/homeassistant/ComponentLight.java +++ b/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/convention/homeassistant/ComponentLight.java @@ -160,10 +160,10 @@ public void updateChannelState(ChannelUID channelUID, State value) { * Proxy method to condense all three MQTT subscriptions to one channel */ @Override - public void postChannelState(ChannelUID channelUID, Command value) { + public void postChannelCommand(ChannelUID channelUID, Command value) { ChannelStateUpdateListener listener = channelStateUpdateListener; if (listener != null) { - listener.postChannelState(colorChannel.channelUID, value); + listener.postChannelCommand(colorChannel.channelUID, value); } } diff --git a/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/generic/ChannelState.java b/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/generic/ChannelState.java index b691c109678..e70d34595e5 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/generic/ChannelState.java +++ b/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/generic/ChannelState.java @@ -161,6 +161,13 @@ public void processMessage(String topic, byte[] payload) { return; } + Command postOnlyCommand = cachedValue.isPostOnly(command); + if (postOnlyCommand != null) { + channelStateUpdateListener.postChannelCommand(channelUID, postOnlyCommand); + receivedOrTimeout(); + return; + } + // Map the string to an ESH command, update the cached value and post the command to the framework try { cachedValue.update(command); @@ -172,7 +179,7 @@ public void processMessage(String topic, byte[] payload) { } if (config.postCommand) { - channelStateUpdateListener.postChannelState(channelUID, (Command) cachedValue.getChannelState()); + channelStateUpdateListener.postChannelCommand(channelUID, (Command) cachedValue.getChannelState()); } else { channelStateUpdateListener.updateChannelState(channelUID, cachedValue.getChannelState()); } diff --git a/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/generic/ChannelStateUpdateListener.java b/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/generic/ChannelStateUpdateListener.java index ccd0011f9fb..ac04c915093 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/generic/ChannelStateUpdateListener.java +++ b/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/generic/ChannelStateUpdateListener.java @@ -37,7 +37,7 @@ public interface ChannelStateUpdateListener { * @param channelUID The channel uid * @param value The new value. Doesn't necessarily need to be different than the value before. */ - void postChannelState(ChannelUID channelUID, Command value); + void postChannelCommand(ChannelUID channelUID, Command value); /** * A new value got published on a configured MQTT topic associated with the given channel uid. diff --git a/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/handler/AbstractMQTTThingHandler.java b/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/handler/AbstractMQTTThingHandler.java index 2c5da6856a8..f5b23e3f5b9 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/handler/AbstractMQTTThingHandler.java +++ b/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/handler/AbstractMQTTThingHandler.java @@ -225,7 +225,7 @@ public void triggerChannel(ChannelUID channelUID, String event) { } @Override - public void postChannelState(ChannelUID channelUID, Command command) { + public void postChannelCommand(ChannelUID channelUID, Command command) { postCommand(channelUID, command); } } diff --git a/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/values/RollershutterValue.java b/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/values/RollershutterValue.java index 6e71df84ff5..c85065f5c82 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/values/RollershutterValue.java +++ b/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/values/RollershutterValue.java @@ -61,23 +61,29 @@ public void update(Command command) throws IllegalArgumentException { } state = newState; } else if (command instanceof StopMoveType) { - if (command.equals(StopMoveType.STOP)) { - state = UnDefType.UNDEF; - } + throw new IllegalStateException("Cannot call update() with StopMoveType"); } else if (command instanceof PercentType) { state = (PercentType) command; } else { + throw new IllegalStateException("Cannot call update() with custom stop/move/up/down"); + } + } + + @Override + public @Nullable Command isPostOnly(Command command) { + if (command instanceof StopMoveType) { + return command; + } else if (command instanceof StringType) { final String updatedValue = command.toString(); if (updatedValue.equals(upString)) { - state = PercentType.ZERO; + return UpDownType.UP; } else if (updatedValue.equals(downString)) { - state = PercentType.HUNDRED; + return UpDownType.DOWN; } else if (updatedValue.equals(stopString)) { - state = UnDefType.UNDEF; - } else { - state = PercentType.valueOf(updatedValue); + return StopMoveType.STOP; } } + return null; } @Override diff --git a/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/values/Value.java b/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/values/Value.java index f80c264e216..b5786750001 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/values/Value.java +++ b/extensions/binding/org.eclipse.smarthome.binding.mqtt.generic/src/main/java/org/eclipse/smarthome/binding/mqtt/generic/internal/values/Value.java @@ -19,6 +19,7 @@ import java.util.List; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.binding.mqtt.generic.internal.handler.GenericThingHandler; import org.eclipse.smarthome.core.library.CoreItemFactory; import org.eclipse.smarthome.core.library.types.DecimalType; @@ -118,6 +119,17 @@ public final void resetState() { */ public abstract void update(Command command) throws IllegalArgumentException; + /** + * Returns the given command if it cannot be handled by {@link #update(Command)} + * or {@link #update(byte[])} and need to be posted straight to the framework instead. + * Returns null otherwise. + * + * @param command The command to decide about + */ + public @Nullable Command isPostOnly(Command command) { + return null; + } + /** * Updates the internal value state with the given binary payload. *