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