]> git.basschouten.com Git - openhab-addons.git/blob
754b789fbad90eaf81ba191005fc9f2159976844
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.mielecloud.internal.handler.channel;
14
15 import java.util.Optional;
16
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;
25
26 /**
27  * Utility class handling type conversions from Java types to channel types.
28  *
29  * @author Björn Lange - Initial Contribution
30  */
31 @NonNullByDefault
32 public final class ChannelTypeUtil {
33     private ChannelTypeUtil() {
34         throw new IllegalStateException("ChannelTypeUtil cannot be instantiated.");
35     }
36
37     /**
38      * Converts an {@link Optional} of {@link String} to {@link State}.
39      */
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);
43     }
44
45     /**
46      * Converts an {@link Optional} of {@link Boolean} to {@link State}.
47      */
48     public static State booleanToState(Optional<Boolean> value) {
49         return value.map(v -> (State) OnOffType.from(v)).orElse(UnDefType.UNDEF);
50     }
51
52     /**
53      * Converts an {@link Optional} of {@link Integer} to {@link State}.
54      */
55     public static State intToState(Optional<Integer> value) {
56         return value.map(v -> (State) new DecimalType(v)).orElse(UnDefType.UNDEF);
57     }
58
59     /**
60      * Converts an {@link Optional} of {@link Long} to {@link State}.
61      */
62     public static State longToState(Optional<Long> value) {
63         return value.map(v -> (State) new DecimalType(v)).orElse(UnDefType.UNDEF);
64     }
65
66     /**
67      * Converts an {@link Optional} of {@link Integer} to {@link State} representing a temperature.
68      */
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);
72     }
73 }