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.TextValue;
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 select, following the https://www.home-assistant.io/components/select.mqtt/ specification.
27 * @author Cody Cutrer - Initial contribution
30 public class Select extends AbstractComponent<Select.ChannelConfiguration> {
31 public static final String SELECT_CHANNEL_ID = "select"; // 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_template")
44 protected @Nullable String commandTemplate;
45 @SerializedName("command_topic")
46 protected @Nullable String commandTopic;
47 @SerializedName("state_topic")
48 protected String stateTopic = "";
50 protected String[] options = new String[0];
52 @SerializedName("json_attributes_topic")
53 protected @Nullable String jsonAttributesTopic;
54 @SerializedName("json_attributes_template")
55 protected @Nullable String jsonAttributesTemplate;
58 public Select(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
59 super(componentConfiguration, ChannelConfiguration.class, newStyleChannels, true);
61 boolean optimistic = channelConfiguration.optimistic != null ? channelConfiguration.optimistic
62 : channelConfiguration.stateTopic.isBlank();
64 if (optimistic && !channelConfiguration.stateTopic.isBlank()) {
65 throw new ConfigurationException("Component:Select does not support forced optimistic mode");
68 TextValue value = new TextValue(channelConfiguration.options);
70 buildChannel(SELECT_CHANNEL_ID, ComponentChannelType.STRING, value, getName(),
71 componentConfiguration.getUpdateListener())
72 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
73 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
74 channelConfiguration.getQos(), channelConfiguration.commandTemplate)