]> git.basschouten.com Git - openhab-addons.git/blob
be28b1fbc589e5fa390735cf7b9b1339533ed7ba
[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 switch, following the https://www.home-assistant.io/components/switch.mqtt/ specification.
24  *
25  * @author David Graeff - Initial contribution
26  */
27 @NonNullByDefault
28 public class Switch extends AbstractComponent<Switch.ChannelConfiguration> {
29     public static final String SWITCH_CHANNEL_ID = "switch"; // Randomly chosen channel "ID"
30
31     /**
32      * Configuration class for MQTT component
33      */
34     static class ChannelConfiguration extends AbstractChannelConfiguration {
35         ChannelConfiguration() {
36             super("MQTT Switch");
37         }
38
39         protected @Nullable Boolean optimistic;
40
41         @SerializedName("command_topic")
42         protected @Nullable String commandTopic;
43         @SerializedName("state_topic")
44         protected String stateTopic = "";
45
46         @SerializedName("state_on")
47         protected @Nullable String stateOn;
48         @SerializedName("state_off")
49         protected @Nullable String stateOff;
50         @SerializedName("payload_on")
51         protected String payloadOn = "ON";
52         @SerializedName("payload_off")
53         protected String payloadOff = "OFF";
54
55         @SerializedName("json_attributes_topic")
56         protected @Nullable String jsonAttributesTopic;
57         @SerializedName("json_attributes_template")
58         protected @Nullable String jsonAttributesTemplate;
59     }
60
61     public Switch(ComponentFactory.ComponentConfiguration componentConfiguration) {
62         super(componentConfiguration, ChannelConfiguration.class);
63
64         boolean optimistic = channelConfiguration.optimistic != null ? channelConfiguration.optimistic
65                 : channelConfiguration.stateTopic.isBlank();
66
67         if (optimistic && !channelConfiguration.stateTopic.isBlank()) {
68             throw new UnsupportedOperationException("Component:Switch does not support forced optimistic mode");
69         }
70
71         String stateOn = channelConfiguration.stateOn != null ? channelConfiguration.stateOn
72                 : channelConfiguration.payloadOn;
73         String stateOff = channelConfiguration.stateOff != null ? channelConfiguration.stateOff
74                 : channelConfiguration.payloadOff;
75
76         OnOffValue value = new OnOffValue(stateOn, stateOff, channelConfiguration.payloadOn,
77                 channelConfiguration.payloadOff);
78
79         buildChannel(SWITCH_CHANNEL_ID, value, "state", componentConfiguration.getUpdateListener())
80                 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
81                 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
82                         channelConfiguration.getQos())
83                 .build();
84     }
85 }