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.mielecloud.internal.handler.channel;
15 import java.math.BigDecimal;
16 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.QuantityType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.library.unit.SIUnits;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.UnDefType;
28 * Utility class handling type conversions from Java types to channel types.
30 * @author Björn Lange - Initial Contribution
33 public final class ChannelTypeUtil {
34 private ChannelTypeUtil() {
35 throw new IllegalStateException("ChannelTypeUtil cannot be instantiated.");
39 * Converts an {@link Optional} of {@link String} to {@link State}.
41 public static State stringToState(Optional<String> value) {
42 return value.filter(v -> !v.isEmpty()).filter(v -> !v.equals("null")).map(v -> (State) new StringType(v))
43 .orElse(UnDefType.UNDEF);
47 * Converts an {@link Optional} of {@link Boolean} to {@link State}.
49 public static State booleanToState(Optional<Boolean> value) {
50 return value.map(v -> (State) OnOffType.from(v)).orElse(UnDefType.UNDEF);
54 * Converts an {@link Optional} of {@link Integer} to {@link State}.
56 public static State intToState(Optional<Integer> value) {
57 return value.map(v -> (State) new DecimalType(new BigDecimal(v))).orElse(UnDefType.UNDEF);
61 * Converts an {@link Optional} of {@link Long} to {@link State}.
63 public static State longToState(Optional<Long> value) {
64 return value.map(v -> (State) new DecimalType(new BigDecimal(v))).orElse(UnDefType.UNDEF);
68 * Converts an {@link Optional} of {@link Integer} to {@link State} representing a temperature.
70 public static State intToTemperatureState(Optional<Integer> value) {
71 // The Miele 3rd Party API always provides temperatures in °C (even if the device uses another unit).
72 return value.map(v -> (State) new QuantityType<>(v, SIUnits.CELSIUS)).orElse(UnDefType.UNDEF);