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.OpenClosedType;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.types.Command;
25 * Implements an open/close boolean value.
27 * @author David Graeff - Initial contribution
30 public class OpenCloseValue extends Value {
31 private final String openString;
32 private final String closeString;
35 * Creates a contact Open/Close type.
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();
44 * Creates a new contact Open/Close value.
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.
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;
56 public OpenClosedType parseCommand(Command command) throws IllegalArgumentException {
57 if (command instanceof OpenClosedType) {
58 return (OpenClosedType) command;
60 final String updatedValue = command.toString();
61 if (openString.equals(updatedValue)) {
62 return OpenClosedType.OPEN;
63 } else if (closeString.equals(updatedValue)) {
64 return OpenClosedType.CLOSED;
66 return OpenClosedType.valueOf(updatedValue);
72 public String getMQTTpublishValue(Command command, @Nullable String pattern) {
73 String formatPattern = pattern;
74 if (formatPattern == null) {
78 return String.format(formatPattern, command == OpenClosedType.OPEN ? openString : closeString);