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