]> git.basschouten.com Git - openhab-addons.git/blob
2912ac9b7cc37ce7409f1f0ffe37bde11d722d2d
[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 java.math.BigDecimal;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.mqtt.generic.values.NumberValue;
20 import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannelType;
21 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
22 import org.openhab.binding.mqtt.homeassistant.internal.exception.ConfigurationException;
23 import org.openhab.core.types.util.UnitUtils;
24
25 import com.google.gson.annotations.SerializedName;
26
27 /**
28  * A MQTT Number, following the https://www.home-assistant.io/components/number.mqtt/ specification.
29  *
30  * @author Cody Cutrer - Initial contribution
31  */
32 @NonNullByDefault
33 public class Number extends AbstractComponent<Number.ChannelConfiguration> {
34     public static final String NUMBER_CHANNEL_ID = "number"; // Randomly chosen channel "ID"
35
36     /**
37      * Configuration class for MQTT component
38      */
39     static class ChannelConfiguration extends AbstractChannelConfiguration {
40         ChannelConfiguration() {
41             super("MQTT Number");
42         }
43
44         protected @Nullable Boolean optimistic;
45
46         @SerializedName("unit_of_measurement")
47         protected @Nullable String unitOfMeasurement;
48         @SerializedName("device_class")
49         protected @Nullable String deviceClass;
50
51         @SerializedName("command_template")
52         protected @Nullable String commandTemplate;
53         @SerializedName("command_topic")
54         protected @Nullable String commandTopic;
55         @SerializedName("state_topic")
56         protected String stateTopic = "";
57
58         protected BigDecimal min = new BigDecimal(1);
59         protected BigDecimal max = new BigDecimal(100);
60         protected BigDecimal step = new BigDecimal(1);
61
62         @SerializedName("payload_reset")
63         protected String payloadReset = "None";
64
65         protected String mode = "auto";
66
67         @SerializedName("json_attributes_topic")
68         protected @Nullable String jsonAttributesTopic;
69         @SerializedName("json_attributes_template")
70         protected @Nullable String jsonAttributesTemplate;
71     }
72
73     public Number(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
74         super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
75
76         boolean optimistic = channelConfiguration.optimistic != null ? channelConfiguration.optimistic
77                 : channelConfiguration.stateTopic.isBlank();
78
79         if (optimistic && !channelConfiguration.stateTopic.isBlank()) {
80             throw new ConfigurationException("Component:Number does not support forced optimistic mode");
81         }
82
83         NumberValue value = new NumberValue(channelConfiguration.min, channelConfiguration.max,
84                 channelConfiguration.step, UnitUtils.parseUnit(channelConfiguration.unitOfMeasurement));
85
86         buildChannel(NUMBER_CHANNEL_ID, ComponentChannelType.NUMBER, value, getName(),
87                 componentConfiguration.getUpdateListener())
88                 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
89                 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
90                         channelConfiguration.getQos(), channelConfiguration.commandTemplate)
91                 .build();
92         finalizeChannels();
93     }
94 }