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.binding.mqtt.homeassistant.internal.exception.ConfigurationException;
23 import org.openhab.core.types.util.UnitUtils;
25 import com.google.gson.annotations.SerializedName;
28 * A MQTT Number, following the https://www.home-assistant.io/components/number.mqtt/ specification.
30 * @author Cody Cutrer - Initial contribution
33 public class Number extends AbstractComponent<Number.ChannelConfiguration> {
34 public static final String NUMBER_CHANNEL_ID = "number"; // Randomly chosen channel "ID"
37 * Configuration class for MQTT component
39 static class ChannelConfiguration extends AbstractChannelConfiguration {
40 ChannelConfiguration() {
44 protected @Nullable Boolean optimistic;
46 @SerializedName("unit_of_measurement")
47 protected @Nullable String unitOfMeasurement;
48 @SerializedName("device_class")
49 protected @Nullable String deviceClass;
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 = "";
58 protected BigDecimal min = new BigDecimal(1);
59 protected BigDecimal max = new BigDecimal(100);
60 protected BigDecimal step = new BigDecimal(1);
62 @SerializedName("payload_reset")
63 protected String payloadReset = "None";
65 protected String mode = "auto";
67 @SerializedName("json_attributes_topic")
68 protected @Nullable String jsonAttributesTopic;
69 @SerializedName("json_attributes_template")
70 protected @Nullable String jsonAttributesTemplate;
73 public Number(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
74 super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
76 boolean optimistic = channelConfiguration.optimistic != null ? channelConfiguration.optimistic
77 : channelConfiguration.stateTopic.isBlank();
79 if (optimistic && !channelConfiguration.stateTopic.isBlank()) {
80 throw new ConfigurationException("Component:Number does not support forced optimistic mode");
83 NumberValue value = new NumberValue(channelConfiguration.min, channelConfiguration.max,
84 channelConfiguration.step, UnitUtils.parseUnit(channelConfiguration.unitOfMeasurement));
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)