]> git.basschouten.com Git - openhab-addons.git/blob
97d8b68cafea5787a90d0278d57f30f2e27662f9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.homeassistant.internal;
14
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;
19
20 /**
21  * A MQTT lock, following the https://www.home-assistant.io/components/lock.mqtt/ specification.
22  *
23  * @author David Graeff - Initial contribution
24  */
25 @NonNullByDefault
26 public class ComponentLock extends AbstractComponent<ComponentLock.ChannelConfiguration> {
27     public static final String switchChannelID = "lock"; // Randomly chosen channel "ID"
28
29     /**
30      * Configuration class for MQTT component
31      */
32     static class ChannelConfiguration extends BaseChannelConfiguration {
33         ChannelConfiguration() {
34             super("MQTT Lock");
35         }
36
37         protected boolean optimistic = false;
38
39         protected String state_topic = "";
40         protected String payload_lock = "LOCK";
41         protected String payload_unlock = "UNLOCK";
42         protected @Nullable String command_topic;
43     }
44
45     public ComponentLock(CFactory.ComponentConfiguration componentConfiguration) {
46         super(componentConfiguration, ChannelConfiguration.class);
47
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");
51         }
52
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();
58     }
59 }