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.openhab.core.types.UnDefType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Implements a number value.
37 * If min / max limits are set, values below / above are (almost) silently ignored.
40 * Accepts user updates and MQTT state updates from a DecimalType, IncreaseDecreaseType and UpDownType.
43 * @author David Graeff - Initial contribution
46 public class NumberValue extends Value {
47 private final Logger logger = LoggerFactory.getLogger(NumberValue.class);
48 private final @Nullable BigDecimal min;
49 private final @Nullable BigDecimal max;
50 private final BigDecimal step;
51 private final Unit<?> unit;
53 public NumberValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step,
54 @Nullable Unit<?> unit) {
55 super(CoreItemFactory.NUMBER, List.of(QuantityType.class, IncreaseDecreaseType.class, UpDownType.class));
58 this.step = step == null ? BigDecimal.ONE : step;
59 this.unit = unit != null ? unit : Units.ONE;
62 protected boolean checkConditions(BigDecimal newValue) {
63 BigDecimal min = this.min;
64 if (min != null && newValue.compareTo(min) == -1) {
65 logger.trace("Number not accepted as it is below the configured minimum");
68 BigDecimal max = this.max;
69 if (max != null && newValue.compareTo(max) == 1) {
70 logger.trace("Number not accepted as it is above the configured maximum");
78 public String getMQTTpublishValue(@Nullable String pattern) {
79 if (state == UnDefType.UNDEF) {
83 String formatPattern = pattern;
84 if (formatPattern == null) {
88 return state.format(formatPattern);
92 public void update(Command command) throws IllegalArgumentException {
93 BigDecimal newValue = null;
94 if (command instanceof DecimalType) {
95 newValue = ((DecimalType) command).toBigDecimal();
96 } else if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
97 BigDecimal oldValue = getOldValue();
98 if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
99 newValue = oldValue.add(step);
101 newValue = oldValue.subtract(step);
103 } else if (command instanceof QuantityType<?>) {
104 newValue = getQuantityTypeAsDecimal((QuantityType<?>) command);
106 newValue = new BigDecimal(command.toString());
108 if (!checkConditions(newValue)) {
111 // items with units specified in the label in the UI but no unit on mqtt are stored as
112 // DecimalType to avoid conversions (e.g. % expects 0-1 rather than 0-100)
113 if (!Units.ONE.equals(unit)) {
114 state = new QuantityType<>(newValue, unit);
116 state = new DecimalType(newValue);
120 private BigDecimal getOldValue() {
121 BigDecimal val = BigDecimal.ZERO;
122 if (state instanceof DecimalType) {
123 val = ((DecimalType) state).toBigDecimal();
124 } else if (state instanceof QuantityType<?>) {
125 val = ((QuantityType<?>) state).toBigDecimal();
130 private BigDecimal getQuantityTypeAsDecimal(QuantityType<?> qType) {
131 BigDecimal val = qType.toBigDecimal();
132 if (!qType.getUnit().isCompatible(Units.ONE)) {
133 QuantityType<?> convertedType = qType.toInvertibleUnit(unit);
134 if (convertedType != null) {
135 val = convertedType.toBigDecimal();
142 public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
143 StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
144 BigDecimal max = this.max;
146 builder = builder.withMaximum(max);
148 BigDecimal min = this.min;
150 builder = builder.withMinimum(min);
152 if (!unit.equals(Units.ONE)) {
153 builder.withPattern("%s " + unit);
155 builder.withPattern("%s %unit%");
157 return builder.withStep(step);