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