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.io.neeo.internal.models;
15 import java.util.Objects;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.items.Item;
19 import org.openhab.core.library.types.PercentType;
20 import org.openhab.core.types.StateDescription;
23 * The model representing a Neeo Channel Range used for sliders to specify the min/max and unit (serialize/deserialize
26 * Note that the unit is just a label that is put on the slider itself for the user
28 * @author Tim Roberts - Initial Contribution
31 public class NeeoDeviceChannelRange {
32 /** The range unit representing a number */
33 private static final String UNIT_NUMBER = "";
35 /** The range unit representing a percentage */
36 private static final String UNIT_PERCENT = "%";
39 * The default range with a minimum of 0, maximum of 100 and a unit of {@link #UNIT_PERCENT}
41 static final NeeoDeviceChannelRange DEFAULT = new NeeoDeviceChannelRange(0, 100, UNIT_PERCENT);
43 /** The minimum value for the range */
44 private final int minValue;
46 /** The maximum value for the range */
47 private final int maxValue;
49 /** The unit of the range */
50 private final String unit;
53 * Create the channel range from the given values
55 * @param minValue the miminmum value
56 * @param maxValue the maximum value
57 * @param unit a unit for the range. Defaults to {@link #UNIT_NUMBER} if null or empty
59 public NeeoDeviceChannelRange(int minValue, int maxValue, String unit) {
60 if (minValue > maxValue) {
61 throw new IllegalArgumentException(
62 "maxValue (" + maxValue + ") is smaller than minValue (" + minValue + ")");
64 this.minValue = minValue;
65 this.maxValue = maxValue;
66 this.unit = unit.isEmpty() ? UNIT_NUMBER : unit;
70 * Returns the minimum value for the range
72 * @return the minimum value for the range
74 public int getMinValue() {
79 * Returns the maximum value for the range
81 * @return the maximum value for the range
83 public int getMaxValue() {
88 * Returns the range unit
90 * @return a non-null, non-empty range unit
92 public String getUnit() {
97 * Helper function to create a {@link NeeoDeviceChannelRange} from a given {@link Item}
99 * @param item a non-null item
100 * @return a non-null {@link NeeoDeviceChannelRange} representing the {@link Item}
102 public static NeeoDeviceChannelRange from(Item item) {
103 Objects.requireNonNull(item, "item cannot be null");
105 final boolean supportsPercent = item.getAcceptedDataTypes().contains(PercentType.class);
107 final StateDescription sd = item.getStateDescription();
108 int min = 0, max = 100;
110 if (sd != null && sd.getMinimum() != null) {
111 min = sd.getMinimum().intValue();
114 if (sd != null && sd.getMaximum() != null) {
115 max = sd.getMaximum().intValue();
124 return new NeeoDeviceChannelRange(min, max, supportsPercent ? UNIT_PERCENT : UNIT_NUMBER);
128 public String toString() {
129 return "NeeoDeviceChannelRange [minValue=" + minValue + ", maxValue=" + maxValue + ", unit=" + unit + "]";