]> git.basschouten.com Git - openhab-addons.git/blob
47139df314a1b138e2f8c55ddee8ce976dcc73c9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.generic.values;
14
15 import java.util.List;
16
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;
25
26 /**
27  * Implements an on/off boolean value.
28  *
29  * @author David Graeff - Initial contribution
30  */
31 @NonNullByDefault
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;
37
38     /**
39      * Creates a switch On/Off type, that accepts "ON", "1" for on and "OFF","0" for off.
40      */
41     public OnOffValue() {
42         this(OnOffType.ON.name(), OnOffType.OFF.name());
43     }
44
45     /**
46      * Creates a new SWITCH On/Off value.
47      *
48      * values send in messages will be the same as those expected in incomming messages
49      *
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.
52      */
53     public OnOffValue(@Nullable String onValue, @Nullable String offValue) {
54         this(onValue, offValue, onValue, offValue);
55     }
56
57     /**
58      * Creates a new SWITCH On/Off value.
59      *
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.
64      */
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;
72     }
73
74     @Override
75     public OnOffType parseCommand(Command command) throws IllegalArgumentException {
76         if (command instanceof OnOffType onOffCommand) {
77             return onOffCommand;
78         } else {
79             final String updatedValue = command.toString();
80             if (onState.equals(updatedValue)) {
81                 return OnOffType.ON;
82             } else if (offState.equals(updatedValue)) {
83                 return OnOffType.OFF;
84             } else {
85                 return OnOffType.valueOf(updatedValue);
86             }
87         }
88     }
89
90     @Override
91     public String getMQTTpublishValue(Command command, @Nullable String pattern) {
92         String formatPattern = pattern;
93         if (formatPattern == null) {
94             formatPattern = "%s";
95         }
96
97         return String.format(formatPattern, command == OnOffType.ON ? onCommand : offCommand);
98     }
99
100     @Override
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));
105         return builder;
106     }
107 }