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 {@link SmartherIllegalPropertyValueException}
58 * if the raw value cannot be mapped to any valid enum value
60 public static Function fromValue(String value) throws SmartherIllegalPropertyValueException {
61 return lookup(Function.class, value);
66 * The {@code Mode} enum maps the values of chronothermostat operation mode.
68 public enum Mode implements TypeWithStringProperty {
69 AUTOMATIC("AUTOMATIC"),
73 PROTECTION("PROTECTION");
75 private final String value;
82 public String getValue() {
87 * Returns a {@code Mode} enum value from the given raw value.
90 * the raw value to get an enum value from
92 * @return the enum value representing the given raw value
94 * @throws {@link SmartherIllegalPropertyValueException}
95 * if the raw value cannot be mapped to any valid enum value
97 public static Mode fromValue(String value) throws SmartherIllegalPropertyValueException {
98 return lookup(Mode.class, value);
103 * The {@code LoadState} enum maps the values of chronothermostat operation load state.
105 public enum LoadState implements TypeWithStringProperty {
107 INACTIVE("INACTIVE");
109 private final String value;
111 LoadState(String value) {
116 public String getValue() {
121 * Tells whether the load state value is "active".
123 * @return {@code true} if the load state value is "active", {@code false} otherwise
125 public boolean isActive() {
126 return ACTIVE.getValue().equals(value);
130 * Returns a {@code LoadState} enum value from the given raw value.
133 * the raw value to get an enum value from
135 * @return the enum value representing the given raw value
137 * @throws {@link SmartherIllegalPropertyValueException}
138 * if the raw value cannot be mapped to any valid enum value
140 public static LoadState fromValue(String value) throws SmartherIllegalPropertyValueException {
141 return lookup(LoadState.class, value);
146 * The {@code MeasureUnit} enum maps the values of managed measure unit.
148 public enum MeasureUnit implements TypeWithStringProperty {
154 private final String value;
156 MeasureUnit(String value) {
161 public String getValue() {
166 * Returns a {@code MeasureUnit} enum value for the given measure {@link Unit}.
169 * the measure unit to get an enum value for
171 * @return the enum value representing the given measure unit
173 public static MeasureUnit fromUnit(Unit<?> unit) {
174 if (SIUnits.CELSIUS.equals(unit)) {
176 } else if (ImperialUnits.FAHRENHEIT.equals(unit)) {
178 } else if (Units.PERCENT.equals(unit)) {
181 return DIMENSIONLESS;
186 * Returns a {@code MeasureUnit} enum value from the given raw value.
189 * the raw value to get an enum value from
191 * @return the enum value representing the given raw value
193 * @throws {@link SmartherIllegalPropertyValueException}
194 * if the raw value cannot be mapped to any valid enum value
196 public static MeasureUnit fromValue(String value) throws SmartherIllegalPropertyValueException {
197 return lookup(MeasureUnit.class, value);
202 * The {@code BoostTime} enum maps the time values of chronothermostat boost mode.
204 public enum BoostTime implements TypeWithIntProperty {
209 private final int value;
211 BoostTime(int value) {
216 public int getValue() {
221 * Returns a {@code BoostTime} enum value from the given raw value.
224 * the raw value to get an enum value from
226 * @return the enum value representing the given raw value
228 * @throws {@link SmartherIllegalPropertyValueException}
229 * if the raw value cannot be mapped to any valid enum value
231 public static BoostTime fromValue(int value) throws SmartherIllegalPropertyValueException {
232 return lookup(BoostTime.class, value);
236 // ------------------------------
237 // UTILITY INTERFACES AND METHODS
238 // ------------------------------
240 interface TypeWithIntProperty {
244 public static <E extends Enum<E> & TypeWithIntProperty> E lookup(Class<E> en, int value)
245 throws SmartherIllegalPropertyValueException {
246 E[] constants = en.getEnumConstants();
247 if (constants != null) {
248 for (E constant : constants) {
249 if (constant.getValue() == value) {
254 throw new SmartherIllegalPropertyValueException(en.getSimpleName(), String.valueOf(value));
257 interface TypeWithStringProperty {
261 public static <E extends Enum<E> & TypeWithStringProperty> E lookup(Class<E> en, String value)
262 throws SmartherIllegalPropertyValueException {
263 E[] constants = en.getEnumConstants();
264 if (constants != null) {
265 for (E constant : constants) {
266 if (constant.getValue().equals(value)) {
271 throw new SmartherIllegalPropertyValueException(en.getSimpleName(), value);