]> git.basschouten.com Git - openhab-addons.git/blob
8c035f84c250f651f5bfe2161984366cbbcd8429
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.sensibo.internal;
14
15 import javax.measure.Unit;
16 import javax.measure.quantity.Temperature;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.library.unit.ImperialUnits;
21 import org.openhab.core.library.unit.SIUnits;
22
23 /**
24  * The {@link SensiboTemperatureUnitConverter} converts to/from Sensibo temperature symbols to {@code Unit<Temperature>}
25  *
26  * @author Arne Seime - Initial contribution
27  */
28 @NonNullByDefault
29 public final class SensiboTemperatureUnitConverter {
30     public static Unit<Temperature> parseFromSensiboFormat(@Nullable String symbol) {
31         if (symbol == null) {
32             symbol = "C";
33         }
34         switch (symbol) {
35             case "C":
36                 return SIUnits.CELSIUS;
37             case "F":
38                 return ImperialUnits.FAHRENHEIT;
39             default:
40                 throw new IllegalArgumentException("Do not understand temperature unit " + symbol);
41         }
42     }
43
44     public static String toSensiboFormat(@Nullable Unit<Temperature> unit) {
45         if (SIUnits.CELSIUS.equals(unit)) {
46             return "C";
47         } else if (ImperialUnits.FAHRENHEIT.equals(unit)) {
48             return "F";
49         } else {
50             throw new IllegalArgumentException("Do not understand temperature unit " + unit);
51         }
52     }
53 }