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.bticinosmarther.internal.api.dto;
15 import javax.measure.Unit;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.bticinosmarther.internal.api.exception.SmartherIllegalPropertyValueException;
19 import org.openhab.core.library.unit.ImperialUnits;
20 import org.openhab.core.library.unit.SIUnits;
21 import org.openhab.core.library.unit.Units;
24 * The {@code Enums} class represents a container for enums related to Smarther API.
26 * @author Fabio Possieri - Initial contribution
32 * The {@code Function} enum maps the values of chronothermostat operation function.
34 public enum Function implements TypeWithStringProperty {
38 private final String value;
40 Function(String value) {
45 public String getValue() {
50 * Returns a {@code Function} enum value from the given raw value.
53 * the raw value to get an enum value from
55 * @return the enum value representing the given raw value
57 * @throws SmartherIllegalPropertyValueException if the raw value cannot be mapped to any valid enum value
59 public static Function fromValue(String value) throws SmartherIllegalPropertyValueException {
60 return lookup(Function.class, value);
65 * The {@code Mode} enum maps the values of chronothermostat operation mode.
67 public enum Mode implements TypeWithStringProperty {
68 AUTOMATIC("AUTOMATIC"),
72 PROTECTION("PROTECTION");
74 private final String value;
81 public String getValue() {
86 * Returns a {@code Mode} enum value from the given raw value.
89 * the raw value to get an enum value from
91 * @return the enum value representing the given raw value
93 * @throws SmartherIllegalPropertyValueException if the raw value cannot be mapped to any valid enum value
95 public static Mode fromValue(String value) throws SmartherIllegalPropertyValueException {
96 return lookup(Mode.class, value);
101 * The {@code LoadState} enum maps the values of chronothermostat operation load state.
103 public enum LoadState implements TypeWithStringProperty {
105 INACTIVE("INACTIVE");
107 private final String value;
109 LoadState(String value) {
114 public String getValue() {
119 * Tells whether the load state value is "active".
121 * @return {@code true} if the load state value is "active", {@code false} otherwise
123 public boolean isActive() {
124 return ACTIVE.getValue().equals(value);
128 * Returns a {@code LoadState} enum value from the given raw value.
131 * the raw value to get an enum value from
133 * @return the enum value representing the given raw value
135 * @throws SmartherIllegalPropertyValueException if the raw value cannot be mapped to any valid enum value
137 public static LoadState fromValue(String value) throws SmartherIllegalPropertyValueException {
138 return lookup(LoadState.class, value);
143 * The {@code MeasureUnit} enum maps the values of managed measure unit.
145 public enum MeasureUnit implements TypeWithStringProperty {
151 private final String value;
153 MeasureUnit(String value) {
158 public String getValue() {
163 * Returns a {@code MeasureUnit} enum value for the given measure {@link Unit}.
166 * the measure unit to get an enum value for
168 * @return the enum value representing the given measure unit
170 public static MeasureUnit fromUnit(Unit<?> unit) {
171 if (SIUnits.CELSIUS.equals(unit)) {
173 } else if (ImperialUnits.FAHRENHEIT.equals(unit)) {
175 } else if (Units.PERCENT.equals(unit)) {
178 return DIMENSIONLESS;
183 * Returns a {@code MeasureUnit} enum value from the given raw value.
186 * the raw value to get an enum value from
188 * @return the enum value representing the given raw value
190 * @throws SmartherIllegalPropertyValueException if the raw value cannot be mapped to any valid enum value
192 public static MeasureUnit fromValue(String value) throws SmartherIllegalPropertyValueException {
193 return lookup(MeasureUnit.class, value);
198 * The {@code BoostTime} enum maps the time values of chronothermostat boost mode.
200 public enum BoostTime implements TypeWithIntProperty {
205 private final int value;
207 BoostTime(int value) {
212 public int getValue() {
217 * Returns a {@code BoostTime} enum value from the given raw value.
220 * the raw value to get an enum value from
222 * @return the enum value representing the given raw value
224 * @throws SmartherIllegalPropertyValueException if the raw value cannot be mapped to any valid enum value
226 public static BoostTime fromValue(int value) throws SmartherIllegalPropertyValueException {
227 return lookup(BoostTime.class, value);
231 // ------------------------------
232 // UTILITY INTERFACES AND METHODS
233 // ------------------------------
235 interface TypeWithIntProperty {
239 public static <E extends Enum<E> & TypeWithIntProperty> E lookup(Class<E> en, int value)
240 throws SmartherIllegalPropertyValueException {
241 E[] constants = en.getEnumConstants();
242 if (constants != null) {
243 for (E constant : constants) {
244 if (constant.getValue() == value) {
249 throw new SmartherIllegalPropertyValueException(en.getSimpleName(), String.valueOf(value));
252 interface TypeWithStringProperty {
256 public static <E extends Enum<E> & TypeWithStringProperty> E lookup(Class<E> en, String value)
257 throws SmartherIllegalPropertyValueException {
258 E[] constants = en.getEnumConstants();
259 if (constants != null) {
260 for (E constant : constants) {
261 if (constant.getValue().equals(value)) {
266 throw new SmartherIllegalPropertyValueException(en.getSimpleName(), value);