]> git.basschouten.com Git - openhab-addons.git/blob
41148fc0178f6cff4210f5529ed2b6c1fac35dc2
[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.component;
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 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
19
20 import com.google.gson.annotations.SerializedName;
21
22 /**
23  * A MQTT lock, following the https://www.home-assistant.io/components/lock.mqtt/ specification.
24  *
25  * @author David Graeff - Initial contribution
26  */
27 @NonNullByDefault
28 public class Lock extends AbstractComponent<Lock.ChannelConfiguration> {
29     public static final String SWITCH_CHANNEL_ID = "lock"; // Randomly chosen channel "ID"
30
31     /**
32      * Configuration class for MQTT component
33      */
34     static class ChannelConfiguration extends AbstractChannelConfiguration {
35         ChannelConfiguration() {
36             super("MQTT Lock");
37         }
38
39         protected boolean optimistic = false;
40
41         @SerializedName("state_topic")
42         protected String stateTopic = "";
43         @SerializedName("payload_lock")
44         protected String payloadLock = "LOCK";
45         @SerializedName("payload_unlock")
46         protected String payloadUnlock = "UNLOCK";
47         @SerializedName("command_topic")
48         protected @Nullable String commandTopic;
49     }
50
51     public Lock(ComponentFactory.ComponentConfiguration componentConfiguration) {
52         super(componentConfiguration, ChannelConfiguration.class);
53
54         // We do not support all HomeAssistant quirks
55         if (channelConfiguration.optimistic && !channelConfiguration.stateTopic.isBlank()) {
56             throw new UnsupportedOperationException("Component:Lock does not support forced optimistic mode");
57         }
58
59         buildChannel(SWITCH_CHANNEL_ID,
60                 new OnOffValue(channelConfiguration.payloadLock, channelConfiguration.payloadUnlock),
61                 channelConfiguration.getName(), componentConfiguration.getUpdateListener())
62                         .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
63                         .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
64                                 channelConfiguration.getQos())
65                         .build();
66     }
67 }