2 * Copyright (c) 2010-2022 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.stream.Collectors;
16 import java.util.stream.Stream;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.library.CoreItemFactory;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.types.Command;
24 import org.openhab.core.types.CommandDescriptionBuilder;
25 import org.openhab.core.types.CommandOption;
28 * Implements an on/off boolean value.
30 * @author David Graeff - Initial contribution
33 public class OnOffValue extends Value {
34 private final String onState;
35 private final String offState;
36 private final String onCommand;
37 private final String offCommand;
40 * Creates a switch On/Off type, that accepts "ON", "1" for on and "OFF","0" for off.
43 this(OnOffType.ON.name(), OnOffType.OFF.name());
47 * Creates a new SWITCH On/Off value.
49 * values send in messages will be the same as those expected in incomming messages
51 * @param onValue The ON value string. This will be compared to MQTT messages.
52 * @param offValue The OFF value string. This will be compared to MQTT messages.
54 public OnOffValue(@Nullable String onValue, @Nullable String offValue) {
55 this(onValue, offValue, onValue, offValue);
59 * Creates a new SWITCH On/Off value.
61 * @param onState The ON value string. This will be compared to MQTT messages.
62 * @param offState The OFF value string. This will be compared to MQTT messages.
63 * @param onCommand The ON value string. This will be send in MQTT messages.
64 * @param offCommand The OFF value string. This will be send in MQTT messages.
66 public OnOffValue(@Nullable String onState, @Nullable String offState, @Nullable String onCommand,
67 @Nullable String offCommand) {
68 super(CoreItemFactory.SWITCH, Stream.of(OnOffType.class, StringType.class).collect(Collectors.toList()));
69 this.onState = onState == null ? OnOffType.ON.name() : onState;
70 this.offState = offState == null ? OnOffType.OFF.name() : offState;
71 this.onCommand = onCommand == null ? OnOffType.ON.name() : onCommand;
72 this.offCommand = offCommand == null ? OnOffType.OFF.name() : offCommand;
76 public void update(Command command) throws IllegalArgumentException {
77 if (command instanceof OnOffType) {
78 state = (OnOffType) command;
80 final String updatedValue = command.toString();
81 if (onState.equals(updatedValue)) {
83 } else if (offState.equals(updatedValue)) {
84 state = OnOffType.OFF;
86 state = OnOffType.valueOf(updatedValue);
92 public String getMQTTpublishValue(@Nullable String pattern) {
93 String formatPattern = pattern;
94 if (formatPattern == null) {
98 return String.format(formatPattern, state == OnOffType.ON ? onCommand : offCommand);
102 public CommandDescriptionBuilder createCommandDescription() {
103 CommandDescriptionBuilder builder = super.createCommandDescription();
104 builder = builder.withCommandOption(new CommandOption(onCommand, onCommand));
105 builder = builder.withCommandOption(new CommandOption(offCommand, offCommand));