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.UpDownType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.StateDescriptionFragmentBuilder;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Implements a number value.
36 * If min / max limits are set, values below / above are (almost) silently ignored.
39 * Accepts user updates and MQTT state updates from a DecimalType, IncreaseDecreaseType and UpDownType.
42 * @author David Graeff - Initial contribution
45 public class NumberValue extends Value {
46 private final Logger logger = LoggerFactory.getLogger(NumberValue.class);
47 private final @Nullable BigDecimal min;
48 private final @Nullable BigDecimal max;
49 private final BigDecimal step;
50 private final Unit<?> unit;
52 public NumberValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step,
53 @Nullable Unit<?> unit) {
54 super(CoreItemFactory.NUMBER, List.of(QuantityType.class, IncreaseDecreaseType.class, UpDownType.class));
57 this.step = step == null ? BigDecimal.ONE : step;
58 this.unit = unit != null ? unit : Units.ONE;
61 protected boolean checkConditions(BigDecimal newValue) {
62 BigDecimal min = this.min;
63 if (min != null && newValue.compareTo(min) == -1) {
64 logger.trace("Number not accepted as it is below the configured minimum");
67 BigDecimal max = this.max;
68 if (max != null && newValue.compareTo(max) == 1) {
69 logger.trace("Number not accepted as it is above the configured maximum");
77 public String getMQTTpublishValue(Command command, @Nullable String pattern) {
78 String formatPattern = pattern;
79 if (formatPattern == null) {
83 return command.format(formatPattern);
87 public Command parseCommand(Command command) throws IllegalArgumentException {
88 BigDecimal newValue = null;
89 if (command instanceof DecimalType) {
90 newValue = ((DecimalType) command).toBigDecimal();
91 } else if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
92 BigDecimal oldValue = getOldValue();
93 if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
94 newValue = oldValue.add(step);
96 newValue = oldValue.subtract(step);
98 } else if (command instanceof QuantityType<?>) {
99 newValue = getQuantityTypeAsDecimal((QuantityType<?>) command);
101 newValue = new BigDecimal(command.toString());
103 if (!checkConditions(newValue)) {
104 throw new IllegalArgumentException(newValue + " is out of range");
106 // items with units specified in the label in the UI but no unit on mqtt are stored as
107 // DecimalType to avoid conversions (e.g. % expects 0-1 rather than 0-100)
108 if (!Units.ONE.equals(unit)) {
109 return new QuantityType<>(newValue, unit);
111 return new DecimalType(newValue);
115 private BigDecimal getOldValue() {
116 BigDecimal val = BigDecimal.ZERO;
117 if (state instanceof DecimalType) {
118 val = ((DecimalType) state).toBigDecimal();
119 } else if (state instanceof QuantityType<?>) {
120 val = ((QuantityType<?>) state).toBigDecimal();
125 private BigDecimal getQuantityTypeAsDecimal(QuantityType<?> qType) {
126 BigDecimal val = qType.toBigDecimal();
127 if (!qType.getUnit().isCompatible(Units.ONE)) {
128 QuantityType<?> convertedType = qType.toInvertibleUnit(unit);
129 if (convertedType != null) {
130 val = convertedType.toBigDecimal();
137 public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
138 StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
139 BigDecimal max = this.max;
141 builder = builder.withMaximum(max);
143 BigDecimal min = this.min;
145 builder = builder.withMinimum(min);
147 if (!unit.equals(Units.ONE)) {
148 builder.withPattern("%s " + unit);
150 builder.withPattern("%s %unit%");
152 return builder.withStep(step);