]> git.basschouten.com Git - openhab-addons.git/blob
faafbb5bc966dc5d291636c9d2e4867d1df52340
[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 a String * 10. i.e. "215" = 21.5 degrees C
83      * @return the temperature as a {@link QuantityType}
84      */
85     protected State parseTemperature(@Nullable String temperature) {
86         try {
87             return temperature != null ? parseTemperature(Integer.parseInt(temperature)) : UnDefType.UNDEF;
88         } catch (NumberFormatException e) {
89             return UnDefType.UNDEF;
90         }
91     }
92
93     /**
94      *
95      * @param temperature value returned from the API as an Integer * 10. i.e. 215 = 21.5 degrees C
96      * @return the temperature as a {@link QuantityType}
97      */
98     protected State parseTemperature(@Nullable Integer temperature) {
99         return temperature != null ? new QuantityType<>(temperature / 10.0, SIUnits.CELSIUS) : UnDefType.UNDEF;
100     }
101
102     /**
103      *
104      * @param temperature {@link QuantityType} a temperature
105      * @return the temperature as an int in degrees C * 10. i.e. 21.5 degrees C = 215
106      */
107     protected int formatTemperature(QuantityType<?> temperature) {
108         return (int) (temperature.toUnit(SIUnits.CELSIUS).doubleValue() * 10);
109     }
110
111     /**
112      *
113      * @param enery value returned from the API as a string "10.5" = 10.5 kWh
114      * @return the energy as a {@link QuantityType}
115      */
116     protected State parseEnergy(@Nullable String energy) {
117         try {
118             return energy != null ? new QuantityType<>(Float.parseFloat(energy), Units.KILOWATT_HOUR) : UnDefType.UNDEF;
119         } catch (NumberFormatException e) {
120             return UnDefType.UNDEF;
121         }
122     }
123
124     /**
125      *
126      * @param value a string to convert to {@link StringType}
127      * @return the string as a {@link StringType}
128      */
129     protected State parseString(@Nullable String value) {
130         return value != null ? new StringType(value) : UnDefType.UNDEF;
131     }
132
133     /**
134      *
135      * @param value an integer to convert to {@link QuantityType} in minutes
136      * @return the number of minutes as a {@link QuantityType}
137      */
138     protected State parseDuration(@Nullable Integer value) {
139         return value != null ? new QuantityType<>(value, Units.MINUTE) : UnDefType.UNDEF;
140     }
141 }