]> git.basschouten.com Git - openhab-addons.git/blob
0d54c4ca1578303684759b8286b7ab17757c236c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.TextValue;
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 select, following the https://www.home-assistant.io/components/select.mqtt/ specification.
25  *
26  * @author Cody Cutrer - Initial contribution
27  */
28 @NonNullByDefault
29 public class Select extends AbstractComponent<Select.ChannelConfiguration> {
30     public static final String SELECT_CHANNEL_ID = "select"; // Randomly chosen channel "ID"
31
32     /**
33      * Configuration class for MQTT component
34      */
35     static class ChannelConfiguration extends AbstractChannelConfiguration {
36         ChannelConfiguration() {
37             super("MQTT Select");
38         }
39
40         protected @Nullable Boolean optimistic;
41
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 = "";
48
49         protected String[] options = new String[0];
50
51         @SerializedName("json_attributes_topic")
52         protected @Nullable String jsonAttributesTopic;
53         @SerializedName("json_attributes_template")
54         protected @Nullable String jsonAttributesTemplate;
55     }
56
57     public Select(ComponentFactory.ComponentConfiguration componentConfiguration) {
58         super(componentConfiguration, ChannelConfiguration.class);
59
60         boolean optimistic = channelConfiguration.optimistic != null ? channelConfiguration.optimistic
61                 : channelConfiguration.stateTopic.isBlank();
62
63         if (optimistic && !channelConfiguration.stateTopic.isBlank()) {
64             throw new ConfigurationException("Component:Select does not support forced optimistic mode");
65         }
66
67         TextValue value = new TextValue(channelConfiguration.options);
68
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)
73                 .build();
74     }
75 }