2 * Copyright (c) 2010-2021 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.mielecloud.internal.handler.channel;
15 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.library.types.DecimalType;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.library.types.QuantityType;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.library.unit.SIUnits;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.UnDefType;
27 * Utility class handling type conversions from Java types to channel types.
29 * @author Björn Lange - Initial Contribution
32 public final class ChannelTypeUtil {
33 private ChannelTypeUtil() {
34 throw new IllegalStateException("ChannelTypeUtil cannot be instantiated.");
38 * Converts an {@link Optional} of {@link String} to {@link State}.
40 public static State stringToState(Optional<String> value) {
41 return value.filter(v -> !v.isEmpty()).filter(v -> !v.equals("null")).map(v -> (State) new StringType(v))
42 .orElse(UnDefType.UNDEF);
46 * Converts an {@link Optional} of {@link Boolean} to {@link State}.
48 public static State booleanToState(Optional<Boolean> value) {
49 return value.map(v -> (State) OnOffType.from(v)).orElse(UnDefType.UNDEF);
53 * Converts an {@link Optional} of {@link Integer} to {@link State}.
55 public static State intToState(Optional<Integer> value) {
56 return value.map(v -> (State) new DecimalType(v)).orElse(UnDefType.UNDEF);
60 * Converts an {@link Optional} of {@link Long} to {@link State}.
62 public static State longToState(Optional<Long> value) {
63 return value.map(v -> (State) new DecimalType(v)).orElse(UnDefType.UNDEF);
67 * Converts an {@link Optional} of {@link Integer} to {@link State} representing a temperature.
69 public static State intToTemperatureState(Optional<Integer> value) {
70 // The Miele 3rd Party API always provides temperatures in °C (even if the device uses another unit).
71 return value.map(v -> (State) new QuantityType<>(v, SIUnits.CELSIUS)).orElse(UnDefType.UNDEF);