2 * Copyright (c) 2010-2022 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.ZonedDateTime;
16 import java.util.Date;
18 import javax.measure.Unit;
19 import javax.measure.quantity.Temperature;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.i18n.TimeZoneProvider;
24 import org.openhab.core.library.types.DateTimeType;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.PointType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.library.unit.ImperialUnits;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.UnDefType;
37 * The {@link EcobeeUtils} contains utility methods used by the
38 * thing handler and the bridge handler.
40 * @author Mark Hilbush - Initial contribution
43 public final class EcobeeUtils {
45 private static final int UNKNOWN_VALUE = -5002;
48 * Checks to see if a bridge is online.
50 public static boolean isBridgeOnline(@Nullable Bridge bridge) {
51 boolean bridgeStatus = false;
52 if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
59 * Set the state to the passed value. If value is null, set the state to UNDEF
61 public static State undefOrOnOff(@Nullable Boolean value) {
62 return value == null ? UnDefType.UNDEF : (value.booleanValue() ? OnOffType.ON : OnOffType.OFF);
65 public static State undefOrString(@Nullable String value) {
66 return value == null ? UnDefType.UNDEF : new StringType(value);
69 public static State undefOrDecimal(@Nullable Number value) {
70 return (value == null || isUnknown(value)) ? UnDefType.UNDEF : new DecimalType(value.doubleValue());
73 public static State undefOrLong(@Nullable Number value) {
74 return (value == null || isUnknown(value)) ? UnDefType.UNDEF : new DecimalType(value.longValue());
77 public static State undefOrQuantity(@Nullable Number value, Unit<?> unit) {
78 return (value == null || isUnknown(value)) ? UnDefType.UNDEF : new QuantityType<>(value, unit);
81 public static State undefOrTemperature(@Nullable Number value) {
82 return (value == null || isUnknown(value)) ? UnDefType.UNDEF
83 : new QuantityType<>(value.doubleValue() / 10.0, ImperialUnits.FAHRENHEIT);
86 public static State undefOrPoint(@Nullable String value) {
87 return value == null ? UnDefType.UNDEF : new PointType(value);
90 public static State undefOrDate(@Nullable Date date, TimeZoneProvider timeZoneProvider) {
91 return date == null ? UnDefType.UNDEF
92 : new DateTimeType(ZonedDateTime.ofInstant(date.toInstant(), timeZoneProvider.getTimeZone()));
95 private static boolean isUnknown(Number value) {
96 return value.intValue() == UNKNOWN_VALUE;
100 * Convert a QuantityType<Temperature> to the internal format used by the Ecobee API.
102 @SuppressWarnings("unchecked")
103 public static Integer convertQuantityTypeToEcobeeTemp(Object value) {
104 if (value instanceof QuantityType<?>) {
105 QuantityType<Temperature> convertedTemp = ((QuantityType<Temperature>) value)
106 .toUnit(ImperialUnits.FAHRENHEIT);
107 if (convertedTemp != null) {
108 return Integer.valueOf(convertedTemp.intValue() * 10);
111 throw new IllegalArgumentException("temperature is not a QuantityType");