2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.homeassistant.internal.component;
15 import java.math.BigDecimal;
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;
24 import com.google.gson.annotations.SerializedName;
27 * A MQTT Number, following the https://www.home-assistant.io/components/number.mqtt/ specification.
29 * @author Cody Cutrer - Initial contribution
32 public class Number extends AbstractComponent<Number.ChannelConfiguration> {
33 public static final String NUMBER_CHANNEL_ID = "number"; // Randomly chosen channel "ID"
36 * Configuration class for MQTT component
38 static class ChannelConfiguration extends AbstractChannelConfiguration {
39 ChannelConfiguration() {
43 protected @Nullable Boolean optimistic;
45 @SerializedName("unit_of_measurement")
46 protected @Nullable String unitOfMeasurement;
47 @SerializedName("device_class")
48 protected @Nullable String deviceClass;
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 = "";
57 protected BigDecimal min = new BigDecimal(1);
58 protected BigDecimal max = new BigDecimal(100);
59 protected BigDecimal step = new BigDecimal(1);
61 @SerializedName("payload_reset")
62 protected String payloadReset = "None";
64 protected String mode = "auto";
66 @SerializedName("json_attributes_topic")
67 protected @Nullable String jsonAttributesTopic;
68 @SerializedName("json_attributes_template")
69 protected @Nullable String jsonAttributesTemplate;
72 public Number(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
73 super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
75 NumberValue value = new NumberValue(channelConfiguration.min, channelConfiguration.max,
76 channelConfiguration.step, UnitUtils.parseUnit(channelConfiguration.unitOfMeasurement));
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();