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