2 * Copyright (c) 2010-2024 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.warmup.internal.handler;
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;
33 * The {@link WarmupThingHandler} is a super class for Things related to the Bridge consolidating logic.
35 * @author James Melville - Initial contribution
38 public class WarmupThingHandler extends BaseThingHandler {
40 public WarmupThingHandler(Thing thing) {
45 public void initialize() {
46 final MyWarmupAccountHandler bridgeHandler = getBridgeHandler();
47 if (bridgeHandler != null) {
48 updateStatus(ThingStatus.UNKNOWN);
53 public void handleCommand(ChannelUID channelUID, Command command) {
54 final MyWarmupAccountHandler bridgeHandler = getBridgeHandler();
56 if (command instanceof RefreshType && bridgeHandler != null) {
57 bridgeHandler.refreshFromCache();
61 protected void refreshFromServer() {
62 final MyWarmupAccountHandler bridgeHandler = getBridgeHandler();
64 if (bridgeHandler != null) {
65 bridgeHandler.refreshFromServer();
69 protected @Nullable MyWarmupAccountHandler getBridgeHandler() {
70 final Bridge bridge = getBridge();
73 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
76 return (MyWarmupAccountHandler) bridge.getHandler();
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}
85 protected State parseTemperature(@Nullable String temperature) {
87 return temperature != null ? parseTemperature(Integer.parseInt(temperature)) : UnDefType.UNDEF;
88 } catch (NumberFormatException e) {
89 return UnDefType.UNDEF;
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}
98 protected State parseTemperature(@Nullable Integer temperature) {
99 return temperature != null ? new QuantityType<>(temperature / 10.0, SIUnits.CELSIUS) : UnDefType.UNDEF;
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
107 protected int formatTemperature(QuantityType<?> temperature) {
108 return (int) (temperature.toUnit(SIUnits.CELSIUS).doubleValue() * 10);
113 * @param enery value returned from the API as a string "10.5" = 10.5 kWh
114 * @return the energy as a {@link QuantityType}
116 protected State parseEnergy(@Nullable String energy) {
118 return energy != null ? new QuantityType<>(Float.parseFloat(energy), Units.KILOWATT_HOUR) : UnDefType.UNDEF;
119 } catch (NumberFormatException e) {
120 return UnDefType.UNDEF;
126 * @param value a string to convert to {@link StringType}
127 * @return the string as a {@link StringType}
129 protected State parseString(@Nullable String value) {
130 return value != null ? new StringType(value) : UnDefType.UNDEF;
135 * @param value an integer to convert to {@link QuantityType} in minutes
136 * @return the number of minutes as a {@link QuantityType}
138 protected State parseDuration(@Nullable Integer value) {
139 return value != null ? new QuantityType<>(value, Units.MINUTE) : UnDefType.UNDEF;