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.ecobee.internal.handler;
15 import java.time.Instant;
16 import java.time.LocalDateTime;
17 import java.time.ZonedDateTime;
19 import javax.measure.Unit;
20 import javax.measure.quantity.Temperature;
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;
38 * The {@link EcobeeUtils} contains utility methods used by the
39 * thing handler and the bridge handler.
41 * @author Mark Hilbush - Initial contribution
44 public final class EcobeeUtils {
46 private static final int UNKNOWN_VALUE = -5002;
49 * Checks to see if a bridge is online.
51 public static boolean isBridgeOnline(@Nullable Bridge bridge) {
52 boolean bridgeStatus = false;
53 if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
60 * Set the state to the passed value. If value is null, set the state to UNDEF
62 public static State undefOrOnOff(@Nullable Boolean value) {
63 return value == null ? UnDefType.UNDEF : OnOffType.from((value));
66 public static State undefOrString(@Nullable String value) {
67 return value == null ? UnDefType.UNDEF : new StringType(value);
70 public static State undefOrDecimal(@Nullable Number value) {
71 return (value == null || isUnknown(value)) ? UnDefType.UNDEF : new DecimalType(value.doubleValue());
74 public static State undefOrLong(@Nullable Number value) {
75 return (value == null || isUnknown(value)) ? UnDefType.UNDEF : new DecimalType(value.longValue());
78 public static State undefOrQuantity(@Nullable Number value, Unit<?> unit) {
79 return (value == null || isUnknown(value)) ? UnDefType.UNDEF : new QuantityType<>(value, unit);
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);
87 public static State undefOrPoint(@Nullable String value) {
88 return value == null ? UnDefType.UNDEF : new PointType(value);
91 public static State undefOrDate(@Nullable Instant instant, TimeZoneProvider timeZoneProvider) {
92 return instant == null ? UnDefType.UNDEF
93 : new DateTimeType(ZonedDateTime.ofInstant(instant, timeZoneProvider.getTimeZone()));
96 public static State undefOrDate(@Nullable LocalDateTime ldt, TimeZoneProvider timeZoneProvider) {
97 return ldt == null ? UnDefType.UNDEF : new DateTimeType(ldt.atZone(timeZoneProvider.getTimeZone()));
100 private static boolean isUnknown(Number value) {
101 return value.intValue() == UNKNOWN_VALUE;
105 * Convert a QuantityType<Temperature> to the internal format used by the Ecobee API.
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((int) (convertedTemp.doubleValue() * 10));
116 throw new IllegalArgumentException("temperature is not a QuantityType");