]> git.basschouten.com Git - openhab-addons.git/blob
34cb94ff49099c2f9da5d7ee82b0df6b208de3e0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.OpenClosedType;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.types.Command;
23
24 /**
25  * Implements an open/close boolean value.
26  *
27  * @author David Graeff - Initial contribution
28  */
29 @NonNullByDefault
30 public class OpenCloseValue extends Value {
31     private final String openString;
32     private final String closeString;
33
34     /**
35      * Creates a contact Open/Close type.
36      */
37     public OpenCloseValue() {
38         super(CoreItemFactory.CONTACT, List.of(OpenClosedType.class, StringType.class));
39         this.openString = OpenClosedType.OPEN.name();
40         this.closeString = OpenClosedType.CLOSED.name();
41     }
42
43     /**
44      * Creates a new contact Open/Close value.
45      *
46      * @param openValue The ON value string. This will be compared to MQTT messages.
47      * @param closeValue The OFF value string. This will be compared to MQTT messages.
48      */
49     public OpenCloseValue(@Nullable String openValue, @Nullable String closeValue) {
50         super(CoreItemFactory.CONTACT, List.of(OpenClosedType.class, StringType.class));
51         this.openString = openValue == null ? OpenClosedType.OPEN.name() : openValue;
52         this.closeString = closeValue == null ? OpenClosedType.CLOSED.name() : closeValue;
53     }
54
55     @Override
56     public void update(Command command) throws IllegalArgumentException {
57         if (command instanceof OpenClosedType) {
58             state = (OpenClosedType) command;
59         } else {
60             final String updatedValue = command.toString();
61             if (openString.equals(updatedValue)) {
62                 state = OpenClosedType.OPEN;
63             } else if (closeString.equals(updatedValue)) {
64                 state = OpenClosedType.CLOSED;
65             } else {
66                 state = OpenClosedType.valueOf(updatedValue);
67             }
68         }
69     }
70
71     @Override
72     public String getMQTTpublishValue(@Nullable String pattern) {
73         String formatPattern = pattern;
74         if (formatPattern == null) {
75             formatPattern = "%s";
76         }
77
78         return String.format(formatPattern, state == OpenClosedType.OPEN ? openString : closeString);
79     }
80 }