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