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;
44 * Creates a new rollershutter value.
46 * @param upString The UP value string. This will be compared to MQTT messages.
47 * @param downString The DOWN value string. This will be compared to MQTT messages.
48 * @param stopString The STOP value string. This will be compared to MQTT messages.
50 public RollershutterValue(@Nullable String upString, @Nullable String downString, @Nullable String stopString) {
51 super(CoreItemFactory.ROLLERSHUTTER,
52 List.of(UpDownType.class, StopMoveType.class, PercentType.class, StringType.class));
53 this.upString = upString;
54 this.downString = downString;
55 this.stopString = stopString == null ? StopMoveType.STOP.name() : stopString;
59 public Command parseCommand(Command command) throws IllegalArgumentException {
60 if (command instanceof StopMoveType) {
61 if (command == StopMoveType.STOP) {
64 throw new IllegalArgumentException(command.toString() + " is not a valid command for MQTT.");
66 } else if (command instanceof UpDownType) {
67 if (command == UpDownType.UP) {
68 if (upString != null) {
71 return PercentType.ZERO;
74 if (downString != null) {
77 return PercentType.HUNDRED;
80 } else if (command instanceof PercentType percentage) {
82 } else if (command instanceof StringType) {
83 final String updatedValue = command.toString();
84 if (updatedValue.equals(upString)) {
86 } else if (updatedValue.equals(downString)) {
87 return UpDownType.DOWN;
88 } else if (updatedValue.equals(stopString)) {
89 return StopMoveType.STOP;
92 throw new IllegalStateException("Cannot call parseCommand() with " + command.toString());
96 public String getMQTTpublishValue(Command command, @Nullable String pattern) {
97 final String upString = this.upString;
98 final String downString = this.downString;
99 final String stopString = this.stopString;
100 if (command == UpDownType.UP) {
101 if (upString != null) {
104 return ((UpDownType) command).name();
106 } else if (command == UpDownType.DOWN) {
107 if (downString != null) {
110 return ((UpDownType) command).name();
112 } else if (command == StopMoveType.STOP) {
113 if (stopString != null) {
116 return ((StopMoveType) command).name();
118 } else if (command instanceof PercentType percentage) {
119 if (command.equals(PercentType.HUNDRED) && downString != null) {
121 } else if (command.equals(PercentType.ZERO) && upString != null) {
124 return String.valueOf(percentage.intValue());
127 throw new IllegalArgumentException("Invalid command type for Rollershutter item");