]> git.basschouten.com Git - openhab-addons.git/blob
fec258982cfaafc7d34aa40f9baee2c1d8879436
[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.ecobee.internal.handler;
14
15 import java.time.Instant;
16 import java.time.LocalDateTime;
17 import java.time.ZonedDateTime;
18
19 import javax.measure.Unit;
20 import javax.measure.quantity.Temperature;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.i18n.TimeZoneProvider;
25 import org.openhab.core.library.types.DateTimeType;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.PointType;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.library.unit.ImperialUnits;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.types.State;
35 import org.openhab.core.types.UnDefType;
36
37 /**
38  * The {@link EcobeeUtils} contains utility methods used by the
39  * thing handler and the bridge handler.
40  *
41  * @author Mark Hilbush - Initial contribution
42  */
43 @NonNullByDefault
44 public final class EcobeeUtils {
45
46     private static final int UNKNOWN_VALUE = -5002;
47
48     /*
49      * Checks to see if a bridge is online.
50      */
51     public static boolean isBridgeOnline(@Nullable Bridge bridge) {
52         boolean bridgeStatus = false;
53         if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
54             bridgeStatus = true;
55         }
56         return bridgeStatus;
57     }
58
59     /*
60      * Set the state to the passed value. If value is null, set the state to UNDEF
61      */
62     public static State undefOrOnOff(@Nullable Boolean value) {
63         return value == null ? UnDefType.UNDEF : (value.booleanValue() ? OnOffType.ON : OnOffType.OFF);
64     }
65
66     public static State undefOrString(@Nullable String value) {
67         return value == null ? UnDefType.UNDEF : new StringType(value);
68     }
69
70     public static State undefOrDecimal(@Nullable Number value) {
71         return (value == null || isUnknown(value)) ? UnDefType.UNDEF : new DecimalType(value.doubleValue());
72     }
73
74     public static State undefOrLong(@Nullable Number value) {
75         return (value == null || isUnknown(value)) ? UnDefType.UNDEF : new DecimalType(value.longValue());
76     }
77
78     public static State undefOrQuantity(@Nullable Number value, Unit<?> unit) {
79         return (value == null || isUnknown(value)) ? UnDefType.UNDEF : new QuantityType<>(value, unit);
80     }
81
82     public static State undefOrTemperature(@Nullable Number value) {
83         return (value == null || isUnknown(value)) ? UnDefType.UNDEF
84                 : new QuantityType<>(value.doubleValue() / 10.0, ImperialUnits.FAHRENHEIT);
85     }
86
87     public static State undefOrPoint(@Nullable String value) {
88         return value == null ? UnDefType.UNDEF : new PointType(value);
89     }
90
91     public static State undefOrDate(@Nullable Instant instant, TimeZoneProvider timeZoneProvider) {
92         return instant == null ? UnDefType.UNDEF
93                 : new DateTimeType(ZonedDateTime.ofInstant(instant, timeZoneProvider.getTimeZone()));
94     }
95
96     public static State undefOrDate(@Nullable LocalDateTime ldt, TimeZoneProvider timeZoneProvider) {
97         return ldt == null ? UnDefType.UNDEF : new DateTimeType(ldt.atZone(timeZoneProvider.getTimeZone()));
98     }
99
100     private static boolean isUnknown(Number value) {
101         return value.intValue() == UNKNOWN_VALUE;
102     }
103
104     /*
105      * Convert a QuantityType<Temperature> to the internal format used by the Ecobee API.
106      */
107     @SuppressWarnings("unchecked")
108     public static Integer convertQuantityTypeToEcobeeTemp(Object value) {
109         if (value instanceof QuantityType<?>) {
110             QuantityType<Temperature> convertedTemp = ((QuantityType<Temperature>) value)
111                     .toUnit(ImperialUnits.FAHRENHEIT);
112             if (convertedTemp != null) {
113                 return Integer.valueOf(convertedTemp.intValue() * 10);
114             }
115         }
116         throw new IllegalArgumentException("temperature is not a QuantityType");
117     }
118 }