2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.homeassistant.internal.component;
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.ComponentChannelType;
19 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
20 import org.openhab.binding.mqtt.homeassistant.internal.exception.ConfigurationException;
22 import com.google.gson.annotations.SerializedName;
25 * A MQTT switch, following the https://www.home-assistant.io/components/switch.mqtt/ specification.
27 * @author David Graeff - Initial contribution
30 public class Switch extends AbstractComponent<Switch.ChannelConfiguration> {
31 public static final String SWITCH_CHANNEL_ID = "switch"; // Randomly chosen channel "ID"
34 * Configuration class for MQTT component
36 static class ChannelConfiguration extends AbstractChannelConfiguration {
37 ChannelConfiguration() {
41 protected @Nullable Boolean optimistic;
43 @SerializedName("command_topic")
44 protected @Nullable String commandTopic;
45 @SerializedName("state_topic")
46 protected String stateTopic = "";
48 @SerializedName("state_on")
49 protected @Nullable String stateOn;
50 @SerializedName("state_off")
51 protected @Nullable String stateOff;
52 @SerializedName("payload_on")
53 protected String payloadOn = "ON";
54 @SerializedName("payload_off")
55 protected String payloadOff = "OFF";
57 @SerializedName("json_attributes_topic")
58 protected @Nullable String jsonAttributesTopic;
59 @SerializedName("json_attributes_template")
60 protected @Nullable String jsonAttributesTemplate;
63 public Switch(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
64 super(componentConfiguration, ChannelConfiguration.class, newStyleChannels, true);
66 boolean optimistic = channelConfiguration.optimistic != null ? channelConfiguration.optimistic
67 : channelConfiguration.stateTopic.isBlank();
69 if (optimistic && !channelConfiguration.stateTopic.isBlank()) {
70 throw new ConfigurationException("Component:Switch does not support forced optimistic mode");
73 OnOffValue value = new OnOffValue(channelConfiguration.stateOn, channelConfiguration.stateOff,
74 channelConfiguration.payloadOn, channelConfiguration.payloadOff);
76 buildChannel(SWITCH_CHANNEL_ID, ComponentChannelType.SWITCH, value, getName(),
77 componentConfiguration.getUpdateListener())
78 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
79 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
80 channelConfiguration.getQos())