2 * Copyright (c) 2010-2023 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.TextValue;
18 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
19 import org.openhab.binding.mqtt.homeassistant.internal.exception.ConfigurationException;
21 import com.google.gson.annotations.SerializedName;
24 * A MQTT select, following the https://www.home-assistant.io/components/select.mqtt/ specification.
26 * @author Cody Cutrer - Initial contribution
29 public class Select extends AbstractComponent<Select.ChannelConfiguration> {
30 public static final String SELECT_CHANNEL_ID = "select"; // Randomly chosen channel "ID"
33 * Configuration class for MQTT component
35 static class ChannelConfiguration extends AbstractChannelConfiguration {
36 ChannelConfiguration() {
40 protected @Nullable Boolean optimistic;
42 @SerializedName("command_template")
43 protected @Nullable String commandTemplate;
44 @SerializedName("command_topic")
45 protected @Nullable String commandTopic;
46 @SerializedName("state_topic")
47 protected String stateTopic = "";
49 protected String[] options = new String[0];
51 @SerializedName("json_attributes_topic")
52 protected @Nullable String jsonAttributesTopic;
53 @SerializedName("json_attributes_template")
54 protected @Nullable String jsonAttributesTemplate;
57 public Select(ComponentFactory.ComponentConfiguration componentConfiguration) {
58 super(componentConfiguration, ChannelConfiguration.class);
60 boolean optimistic = channelConfiguration.optimistic != null ? channelConfiguration.optimistic
61 : channelConfiguration.stateTopic.isBlank();
63 if (optimistic && !channelConfiguration.stateTopic.isBlank()) {
64 throw new ConfigurationException("Component:Select does not support forced optimistic mode");
67 TextValue value = new TextValue(channelConfiguration.options);
69 buildChannel(SELECT_CHANNEL_ID, value, getName(), componentConfiguration.getUpdateListener())
70 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
71 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
72 channelConfiguration.getQos(), channelConfiguration.commandTemplate)