]> git.basschouten.com Git - openhab-addons.git/blob
38108297c761981d5fbe6f8c4cb43c3071529d21
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.stream.Collectors;
16 import java.util.stream.Stream;
17
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;
26
27 /**
28  * Implements an on/off boolean value.
29  *
30  * @author David Graeff - Initial contribution
31  */
32 @NonNullByDefault
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;
38
39     /**
40      * Creates a switch On/Off type, that accepts "ON", "1" for on and "OFF","0" for off.
41      */
42     public OnOffValue() {
43         this(OnOffType.ON.name(), OnOffType.OFF.name());
44     }
45
46     /**
47      * Creates a new SWITCH On/Off value.
48      *
49      * values send in messages will be the same as those expected in incomming messages
50      *
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.
53      */
54     public OnOffValue(@Nullable String onValue, @Nullable String offValue) {
55         this(onValue, offValue, onValue, offValue);
56     }
57
58     /**
59      * Creates a new SWITCH On/Off value.
60      *
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.
65      */
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;
73     }
74
75     @Override
76     public void update(Command command) throws IllegalArgumentException {
77         if (command instanceof OnOffType) {
78             state = (OnOffType) command;
79         } else {
80             final String updatedValue = command.toString();
81             if (onState.equals(updatedValue)) {
82                 state = OnOffType.ON;
83             } else if (offState.equals(updatedValue)) {
84                 state = OnOffType.OFF;
85             } else {
86                 state = OnOffType.valueOf(updatedValue);
87             }
88         }
89     }
90
91     @Override
92     public String getMQTTpublishValue(@Nullable String pattern) {
93         String formatPattern = pattern;
94         if (formatPattern == null) {
95             formatPattern = "%s";
96         }
97
98         return String.format(formatPattern, state == OnOffType.ON ? onCommand : offCommand);
99     }
100
101     @Override
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));
106         return builder;
107     }
108 }