2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.generic.values;
15 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.library.CoreItemFactory;
20 import org.openhab.core.library.types.PercentType;
21 import org.openhab.core.library.types.StopMoveType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.library.types.UpDownType;
24 import org.openhab.core.types.Command;
27 * Implements a rollershutter value.
29 * The stop, up and down strings have multiple purposes.
30 * For one if those strings are received via MQTT they are recognised as corresponding commands
31 * and also posted as Commands to the framework.
32 * And if a user commands an Item->Channel to perform Stop the corresponding string is send. For Up,Down
33 * the percentage 0 and 100 is send.
35 * @author David Graeff - Initial contribution
38 public class RollershutterValue extends Value {
39 private final @Nullable String upString;
40 private final @Nullable String downString;
41 private final String stopString;
42 private boolean nextIsStop = false; // If set: getMQTTpublishValue will return the stop string
45 * Creates a new rollershutter value.
47 * @param upString The UP value string. This will be compared to MQTT messages.
48 * @param downString The DOWN value string. This will be compared to MQTT messages.
49 * @param stopString The STOP value string. This will be compared to MQTT messages.
51 public RollershutterValue(@Nullable String upString, @Nullable String downString, @Nullable String stopString) {
52 super(CoreItemFactory.ROLLERSHUTTER,
53 List.of(UpDownType.class, StopMoveType.class, PercentType.class, StringType.class));
54 this.upString = upString;
55 this.downString = downString;
56 this.stopString = stopString == null ? StopMoveType.STOP.name() : stopString;
60 public void update(Command command) throws IllegalArgumentException {
62 if (command instanceof StopMoveType) {
63 nextIsStop = (((StopMoveType) command) == StopMoveType.STOP);
65 } else if (command instanceof UpDownType) {
66 state = ((UpDownType) command) == UpDownType.UP ? PercentType.ZERO : PercentType.HUNDRED;
68 } else if (command instanceof PercentType) {
69 state = (PercentType) command;
71 } else if (command instanceof StringType) {
72 final String updatedValue = command.toString();
73 if (updatedValue.equals(upString)) {
74 state = PercentType.ZERO;
76 } else if (updatedValue.equals(downString)) {
77 state = PercentType.HUNDRED;
79 } else if (updatedValue.equals(stopString)) {
84 throw new IllegalStateException("Cannot call update() with " + command.toString());
88 * The stop command will not update the internal state and is posted to the framework.
90 * The Up/Down commands (100%/0%) are not updating the state directly and are also
91 * posted as percent value to the framework. It is up to the user if the posted values
92 * are applied to the item state immediately (autoupdate=true) or not.
95 public @Nullable Command isPostOnly(Command command) {
96 if (command instanceof UpDownType) {
98 } else if (command instanceof StopMoveType) {
100 } else if (command instanceof StringType) {
101 final String updatedValue = command.toString();
102 if (updatedValue.equals(upString)) {
103 return UpDownType.UP.as(PercentType.class);
104 } else if (updatedValue.equals(downString)) {
105 return UpDownType.DOWN.as(PercentType.class);
106 } else if (updatedValue.equals(stopString)) {
107 return StopMoveType.STOP;
114 public String getMQTTpublishValue(@Nullable String pattern) {
115 final String upString = this.upString;
116 final String downString = this.downString;
117 if (this.nextIsStop) {
118 this.nextIsStop = false;
120 } else if (state instanceof PercentType) {
121 if (state.equals(PercentType.HUNDRED) && downString != null) {
123 } else if (state.equals(PercentType.ZERO) && upString != null) {
126 return String.valueOf(((PercentType) state).intValue());