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.OnOffType;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.types.Command;
23 import org.openhab.core.types.CommandDescriptionBuilder;
24 import org.openhab.core.types.CommandOption;
27 * Implements an on/off boolean value.
29 * @author David Graeff - Initial contribution
32 public class OnOffValue extends Value {
33 private final String onState;
34 private final String offState;
35 private final String onCommand;
36 private final String offCommand;
39 * Creates a switch On/Off type, that accepts "ON", "1" for on and "OFF","0" for off.
42 this(OnOffType.ON.name(), OnOffType.OFF.name());
46 * Creates a new SWITCH On/Off value.
48 * values send in messages will be the same as those expected in incomming messages
50 * @param onValue The ON value string. This will be compared to MQTT messages.
51 * @param offValue The OFF value string. This will be compared to MQTT messages.
53 public OnOffValue(@Nullable String onValue, @Nullable String offValue) {
54 this(onValue, offValue, onValue, offValue);
58 * Creates a new SWITCH On/Off value.
60 * @param onState The ON value string. This will be compared to MQTT messages.
61 * @param offState The OFF value string. This will be compared to MQTT messages.
62 * @param onCommand The ON value string. This will be send in MQTT messages.
63 * @param offCommand The OFF value string. This will be send in MQTT messages.
65 public OnOffValue(@Nullable String onState, @Nullable String offState, @Nullable String onCommand,
66 @Nullable String offCommand) {
67 super(CoreItemFactory.SWITCH, List.of(OnOffType.class, StringType.class));
68 this.onState = onState == null ? OnOffType.ON.name() : onState;
69 this.offState = offState == null ? OnOffType.OFF.name() : offState;
70 this.onCommand = onCommand == null ? OnOffType.ON.name() : onCommand;
71 this.offCommand = offCommand == null ? OnOffType.OFF.name() : offCommand;
75 public OnOffType parseCommand(Command command) throws IllegalArgumentException {
76 if (command instanceof OnOffType) {
77 return (OnOffType) command;
79 final String updatedValue = command.toString();
80 if (onState.equals(updatedValue)) {
82 } else if (offState.equals(updatedValue)) {
85 return OnOffType.valueOf(updatedValue);
91 public String getMQTTpublishValue(Command command, @Nullable String pattern) {
92 String formatPattern = pattern;
93 if (formatPattern == null) {
97 return String.format(formatPattern, command == OnOffType.ON ? onCommand : offCommand);
101 public CommandDescriptionBuilder createCommandDescription() {
102 CommandDescriptionBuilder builder = super.createCommandDescription();
103 builder = builder.withCommandOption(new CommandOption(onCommand, onCommand));
104 builder = builder.withCommandOption(new CommandOption(offCommand, offCommand));