2 * Copyright (c) 2010-2021 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.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.mqtt.generic.values.OnOffValue;
20 * A MQTT lock, following the https://www.home-assistant.io/components/lock.mqtt/ specification.
22 * @author David Graeff - Initial contribution
25 public class ComponentLock extends AbstractComponent<ComponentLock.ChannelConfiguration> {
26 public static final String switchChannelID = "lock"; // Randomly chosen channel "ID"
29 * Configuration class for MQTT component
31 static class ChannelConfiguration extends BaseChannelConfiguration {
32 ChannelConfiguration() {
36 protected boolean optimistic = false;
38 protected String state_topic = "";
39 protected String payload_lock = "LOCK";
40 protected String payload_unlock = "UNLOCK";
41 protected @Nullable String command_topic;
44 public ComponentLock(CFactory.ComponentConfiguration componentConfiguration) {
45 super(componentConfiguration, ChannelConfiguration.class);
47 // We do not support all HomeAssistant quirks
48 if (channelConfiguration.optimistic && !channelConfiguration.state_topic.isBlank()) {
49 throw new UnsupportedOperationException("Component:Lock does not support forced optimistic mode");
52 buildChannel(switchChannelID,
53 new OnOffValue(channelConfiguration.payload_lock, channelConfiguration.payload_unlock),
54 channelConfiguration.name, componentConfiguration.getUpdateListener())
55 .stateTopic(channelConfiguration.state_topic, channelConfiguration.value_template)
56 .commandTopic(channelConfiguration.command_topic, channelConfiguration.retain).build();