]> git.basschouten.com Git - openhab-addons.git/blob
92bc7edfda58dc005121b1faf7e9ae54eb630c56
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.warmup.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.core.library.types.QuantityType;
18 import org.openhab.core.library.types.StringType;
19 import org.openhab.core.library.unit.SIUnits;
20 import org.openhab.core.library.unit.Units;
21 import org.openhab.core.thing.Bridge;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.thing.binding.BaseThingHandler;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
31
32 /**
33  * The {@link WarmupThingHandler} is a super class for Things related to the Bridge consolidating logic.
34  *
35  * @author James Melville - Initial contribution
36  */
37 @NonNullByDefault
38 public class WarmupThingHandler extends BaseThingHandler {
39
40     public WarmupThingHandler(Thing thing) {
41         super(thing);
42     }
43
44     @Override
45     public void initialize() {
46         final MyWarmupAccountHandler bridgeHandler = getBridgeHandler();
47         if (bridgeHandler != null) {
48             updateStatus(ThingStatus.UNKNOWN);
49         }
50     }
51
52     @Override
53     public void handleCommand(ChannelUID channelUID, Command command) {
54         final MyWarmupAccountHandler bridgeHandler = getBridgeHandler();
55
56         if (command instanceof RefreshType && bridgeHandler != null) {
57             bridgeHandler.refreshFromCache();
58         }
59     }
60
61     protected void refreshFromServer() {
62         final MyWarmupAccountHandler bridgeHandler = getBridgeHandler();
63
64         if (bridgeHandler != null) {
65             bridgeHandler.refreshFromServer();
66         }
67     }
68
69     protected @Nullable MyWarmupAccountHandler getBridgeHandler() {
70         final Bridge bridge = getBridge();
71
72         if (bridge == null) {
73             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
74             return null;
75         } else {
76             return (MyWarmupAccountHandler) bridge.getHandler();
77         }
78     }
79
80     /**
81      *
82      * @param temperature value returned from the API as an Integer * 10. i.e. 215 = 21.5 degrees C
83      * @return the temperature as a {@link QuantityType}
84      */
85     protected State parseTemperature(@Nullable Integer temperature) {
86         return temperature != null ? new QuantityType<>(temperature / 10.0, SIUnits.CELSIUS) : UnDefType.UNDEF;
87     }
88
89     /**
90      *
91      * @param value a string to convert to {@link StringType}
92      * @return the string as a {@link StringType}
93      */
94     protected State parseString(@Nullable String value) {
95         return value != null ? new StringType(value) : UnDefType.UNDEF;
96     }
97
98     /**
99      *
100      * @param value an integer to convert to {@link QuantityType} in minutes
101      * @return the number of minutes as a {@link QuantityType}
102      */
103     protected State parseDuration(@Nullable Integer value) {
104         return value != null ? new QuantityType<>(value, Units.MINUTE) : UnDefType.UNDEF;
105     }
106 }