2 * Copyright (c) 2010-2020 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.homeassistant.internal;
15 import org.apache.commons.lang.StringUtils;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.openhab.binding.mqtt.generic.values.OnOffValue;
21 * A MQTT lock, following the https://www.home-assistant.io/components/lock.mqtt/ specification.
23 * @author David Graeff - Initial contribution
26 public class ComponentLock extends AbstractComponent<ComponentLock.ChannelConfiguration> {
27 public static final String switchChannelID = "lock"; // Randomly chosen channel "ID"
30 * Configuration class for MQTT component
32 static class ChannelConfiguration extends BaseChannelConfiguration {
33 ChannelConfiguration() {
37 protected boolean optimistic = false;
39 protected String state_topic = "";
40 protected String payload_lock = "LOCK";
41 protected String payload_unlock = "UNLOCK";
42 protected @Nullable String command_topic;
45 public ComponentLock(CFactory.ComponentConfiguration componentConfiguration) {
46 super(componentConfiguration, ChannelConfiguration.class);
48 // We do not support all HomeAssistant quirks
49 if (channelConfiguration.optimistic && StringUtils.isNotBlank(channelConfiguration.state_topic)) {
50 throw new UnsupportedOperationException("Component:Lock does not support forced optimistic mode");
53 buildChannel(switchChannelID,
54 new OnOffValue(channelConfiguration.payload_lock, channelConfiguration.payload_unlock),
55 channelConfiguration.name, componentConfiguration.getUpdateListener())
56 .stateTopic(channelConfiguration.state_topic, channelConfiguration.value_template)
57 .commandTopic(channelConfiguration.command_topic, channelConfiguration.retain).build();