]> git.basschouten.com Git - openhab-addons.git/blob
b3ceccafe67c38dbd77363570b23856f172cca18
[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.ComponentChannelType;
19 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
20 import org.openhab.binding.mqtt.homeassistant.internal.exception.ConfigurationException;
21
22 import com.google.gson.annotations.SerializedName;
23
24 /**
25  * A MQTT select, following the https://www.home-assistant.io/components/select.mqtt/ specification.
26  *
27  * @author Cody Cutrer - Initial contribution
28  */
29 @NonNullByDefault
30 public class Select extends AbstractComponent<Select.ChannelConfiguration> {
31     public static final String SELECT_CHANNEL_ID = "select"; // Randomly chosen channel "ID"
32
33     /**
34      * Configuration class for MQTT component
35      */
36     static class ChannelConfiguration extends AbstractChannelConfiguration {
37         ChannelConfiguration() {
38             super("MQTT Select");
39         }
40
41         protected @Nullable Boolean optimistic;
42
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 = "";
49
50         protected String[] options = new String[0];
51
52         @SerializedName("json_attributes_topic")
53         protected @Nullable String jsonAttributesTopic;
54         @SerializedName("json_attributes_template")
55         protected @Nullable String jsonAttributesTemplate;
56     }
57
58     public Select(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
59         super(componentConfiguration, ChannelConfiguration.class, newStyleChannels, true);
60
61         boolean optimistic = channelConfiguration.optimistic != null ? channelConfiguration.optimistic
62                 : channelConfiguration.stateTopic.isBlank();
63
64         if (optimistic && !channelConfiguration.stateTopic.isBlank()) {
65             throw new ConfigurationException("Component:Select does not support forced optimistic mode");
66         }
67
68         TextValue value = new TextValue(channelConfiguration.options);
69
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)
75                 .build();
76     }
77 }