2 * Copyright (c) 2010-2023 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.generic.values;
15 import java.math.BigDecimal;
16 import java.util.List;
18 import javax.measure.Unit;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.library.CoreItemFactory;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.IncreaseDecreaseType;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.library.types.UpDownType;
28 import org.openhab.core.library.unit.Units;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.StateDescriptionFragmentBuilder;
31 import org.openhab.core.types.Type;
32 import org.openhab.core.types.UnDefType;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Implements a number value.
39 * If min / max limits are set, values below / above are (almost) silently ignored.
42 * Accepts user updates and MQTT state updates from a DecimalType, IncreaseDecreaseType and UpDownType.
45 * @author David Graeff - Initial contribution
48 public class NumberValue extends Value {
49 private static final String NAN = "NaN";
51 private final Logger logger = LoggerFactory.getLogger(NumberValue.class);
52 private final @Nullable BigDecimal min;
53 private final @Nullable BigDecimal max;
54 private final BigDecimal step;
55 private final Unit<?> unit;
57 public NumberValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step,
58 @Nullable Unit<?> unit) {
59 super(CoreItemFactory.NUMBER,
60 List.of(QuantityType.class, IncreaseDecreaseType.class, UpDownType.class, StringType.class));
63 this.step = step == null ? BigDecimal.ONE : step;
64 this.unit = unit != null ? unit : Units.ONE;
67 protected boolean checkConditions(BigDecimal newValue) {
68 BigDecimal min = this.min;
69 if (min != null && newValue.compareTo(min) == -1) {
70 logger.trace("Number not accepted as it is below the configured minimum");
73 BigDecimal max = this.max;
74 if (max != null && newValue.compareTo(max) == 1) {
75 logger.trace("Number not accepted as it is above the configured maximum");
83 public String getMQTTpublishValue(Command command, @Nullable String pattern) {
84 String formatPattern = pattern;
85 if (formatPattern == null) {
89 return command.format(formatPattern);
93 public Command parseCommand(Command command) throws IllegalArgumentException {
94 BigDecimal newValue = null;
95 if (command instanceof DecimalType decimalCommand) {
96 newValue = decimalCommand.toBigDecimal();
97 } else if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
98 BigDecimal oldValue = getOldValue();
99 if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
100 newValue = oldValue.add(step);
102 newValue = oldValue.subtract(step);
104 } else if (command instanceof QuantityType<?> quantityCommand) {
105 newValue = getQuantityTypeAsDecimal(quantityCommand);
107 newValue = new BigDecimal(command.toString());
109 if (!checkConditions(newValue)) {
110 throw new IllegalArgumentException(newValue + " is out of range");
112 // items with units specified in the label in the UI but no unit on mqtt are stored as
113 // DecimalType to avoid conversions (e.g. % expects 0-1 rather than 0-100)
114 if (!Units.ONE.equals(unit)) {
115 return new QuantityType<>(newValue, unit);
117 return new DecimalType(newValue);
122 public Type parseMessage(Command command) throws IllegalArgumentException {
123 if (command instanceof StringType && command.toString().equalsIgnoreCase(NAN)) {
124 return UnDefType.UNDEF;
126 return parseCommand(command);
129 private BigDecimal getOldValue() {
130 BigDecimal val = BigDecimal.ZERO;
131 if (state instanceof DecimalType decimalCommand) {
132 val = decimalCommand.toBigDecimal();
133 } else if (state instanceof QuantityType<?> quantityCommand) {
134 val = quantityCommand.toBigDecimal();
139 private BigDecimal getQuantityTypeAsDecimal(QuantityType<?> qType) {
140 BigDecimal val = qType.toBigDecimal();
141 if (!qType.getUnit().isCompatible(Units.ONE)) {
142 QuantityType<?> convertedType = qType.toInvertibleUnit(unit);
143 if (convertedType != null) {
144 val = convertedType.toBigDecimal();
151 public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
152 StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
153 BigDecimal max = this.max;
155 builder = builder.withMaximum(max);
157 BigDecimal min = this.min;
159 builder = builder.withMinimum(min);
161 if (!unit.equals(Units.ONE)) {
162 builder.withPattern("%s " + unit);
164 builder.withPattern("%s %unit%");
166 return builder.withStep(step);