]> git.basschouten.com Git - openhab-addons.git/blob
8de66574f32e987d2127ab64acc3f28047f245f4
[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.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, boolean newStyleChannels) {
73         super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
74
75         NumberValue value = new NumberValue(channelConfiguration.min, channelConfiguration.max,
76                 channelConfiguration.step, UnitUtils.parseUnit(channelConfiguration.unitOfMeasurement));
77
78         buildChannel(NUMBER_CHANNEL_ID, ComponentChannelType.NUMBER, value, getName(),
79                 componentConfiguration.getUpdateListener())
80                 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
81                 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
82                         channelConfiguration.getQos(), channelConfiguration.commandTemplate)
83                 .inferOptimistic(channelConfiguration.optimistic).build();
84         finalizeChannels();
85     }
86 }