2 * Copyright (c) 2010-2021 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.radiothermostat.internal.handler;
15 import static org.openhab.binding.radiothermostat.internal.RadioThermostatBindingConstants.*;
17 import java.math.BigDecimal;
18 import java.text.NumberFormat;
19 import java.text.ParseException;
20 import java.time.ZonedDateTime;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.concurrent.ScheduledFuture;
26 import java.util.concurrent.TimeUnit;
28 import javax.measure.quantity.Temperature;
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.eclipse.jetty.client.HttpClient;
33 import org.openhab.binding.radiothermostat.internal.RadioThermostatConfiguration;
34 import org.openhab.binding.radiothermostat.internal.RadioThermostatStateDescriptionProvider;
35 import org.openhab.binding.radiothermostat.internal.RadioThermostatThingActions;
36 import org.openhab.binding.radiothermostat.internal.communication.RadioThermostatConnector;
37 import org.openhab.binding.radiothermostat.internal.communication.RadioThermostatEvent;
38 import org.openhab.binding.radiothermostat.internal.communication.RadioThermostatEventListener;
39 import org.openhab.binding.radiothermostat.internal.dto.RadioThermostatDTO;
40 import org.openhab.binding.radiothermostat.internal.dto.RadioThermostatHumidityDTO;
41 import org.openhab.binding.radiothermostat.internal.dto.RadioThermostatRuntimeDTO;
42 import org.openhab.binding.radiothermostat.internal.dto.RadioThermostatTstatDTO;
43 import org.openhab.core.library.types.DateTimeType;
44 import org.openhab.core.library.types.DecimalType;
45 import org.openhab.core.library.types.OnOffType;
46 import org.openhab.core.library.types.PointType;
47 import org.openhab.core.library.types.QuantityType;
48 import org.openhab.core.library.types.StringType;
49 import org.openhab.core.library.unit.ImperialUnits;
50 import org.openhab.core.thing.Channel;
51 import org.openhab.core.thing.ChannelUID;
52 import org.openhab.core.thing.Thing;
53 import org.openhab.core.thing.ThingStatus;
54 import org.openhab.core.thing.ThingStatusDetail;
55 import org.openhab.core.thing.binding.BaseThingHandler;
56 import org.openhab.core.thing.binding.ThingHandlerService;
57 import org.openhab.core.types.Command;
58 import org.openhab.core.types.RefreshType;
59 import org.openhab.core.types.State;
60 import org.openhab.core.types.StateOption;
61 import org.openhab.core.types.UnDefType;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
65 import com.google.gson.Gson;
68 * The {@link RadioThermostatHandler} is responsible for handling commands, which are
69 * sent to one of the channels.
71 * Based on the 'airquality' binding by Kuba Wolanin
73 * @author Michael Lobstein - Initial contribution
76 public class RadioThermostatHandler extends BaseThingHandler implements RadioThermostatEventListener {
77 private static final int DEFAULT_REFRESH_PERIOD = 2;
78 private static final int DEFAULT_LOG_REFRESH_PERIOD = 10;
80 private final RadioThermostatStateDescriptionProvider stateDescriptionProvider;
81 private final Logger logger = LoggerFactory.getLogger(RadioThermostatHandler.class);
83 private final Gson gson;
84 private final RadioThermostatConnector connector;
85 private final RadioThermostatDTO rthermData = new RadioThermostatDTO();
87 private @Nullable ScheduledFuture<?> refreshJob;
88 private @Nullable ScheduledFuture<?> logRefreshJob;
90 private int refreshPeriod = DEFAULT_REFRESH_PERIOD;
91 private int logRefreshPeriod = DEFAULT_LOG_REFRESH_PERIOD;
92 private boolean isCT80 = false;
93 private boolean disableLogs = false;
94 private String setpointCmdKeyPrefix = "t_";
96 public RadioThermostatHandler(Thing thing, RadioThermostatStateDescriptionProvider stateDescriptionProvider,
97 HttpClient httpClient) {
99 this.stateDescriptionProvider = stateDescriptionProvider;
101 connector = new RadioThermostatConnector(httpClient);
105 public void initialize() {
106 logger.debug("Initializing RadioThermostat handler.");
107 RadioThermostatConfiguration config = getConfigAs(RadioThermostatConfiguration.class);
109 final String hostName = config.hostName;
110 final Integer refresh = config.refresh;
111 final Integer logRefresh = config.logRefresh;
112 this.isCT80 = config.isCT80;
113 this.disableLogs = config.disableLogs;
115 if (hostName == null || hostName.equals("")) {
116 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
117 "Thermostat Host Name must be specified");
121 if (refresh != null) {
122 this.refreshPeriod = refresh;
125 if (logRefresh != null) {
126 this.logRefreshPeriod = logRefresh;
129 connector.setThermostatHostName(hostName);
130 connector.addEventListener(this);
132 // The setpoint mode is controlled by the name of setpoint attribute sent to the thermostat.
133 // Temporary mode uses setpoint names prefixed with "t_" while absolute mode uses "a_"
134 if (config.setpointMode.equals("absolute")) {
135 this.setpointCmdKeyPrefix = "a_";
138 // populate fan mode options based on thermostat model
139 stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), FAN_MODE), getFanModeOptions());
141 // if we are not a CT-80, remove the humidity & program mode channel
143 List<Channel> channels = new ArrayList<>(this.getThing().getChannels());
144 channels.removeIf(c -> (c.getUID().getId().equals(HUMIDITY)));
145 channels.removeIf(c -> (c.getUID().getId().equals(PROGRAM_MODE)));
146 updateThing(editThing().withChannels(channels).build());
148 startAutomaticRefresh();
149 if (!this.disableLogs || this.isCT80) {
150 startAutomaticLogRefresh();
153 updateStatus(ThingStatus.UNKNOWN);
157 public Collection<Class<? extends ThingHandlerService>> getServices() {
158 return Collections.singletonList(RadioThermostatThingActions.class);
162 * Start the job to periodically update data from the thermostat
164 private void startAutomaticRefresh() {
165 ScheduledFuture<?> refreshJob = this.refreshJob;
166 if (refreshJob == null || refreshJob.isCancelled()) {
167 Runnable runnable = () -> {
168 // send an async call to the thermostat to get the 'tstat' data
169 connector.getAsyncThermostatData(DEFAULT_RESOURCE);
173 this.refreshJob = scheduler.scheduleWithFixedDelay(runnable, 0, refreshPeriod, TimeUnit.MINUTES);
178 * Start the job to periodically update humidity and runtime date from the thermostat
180 private void startAutomaticLogRefresh() {
181 ScheduledFuture<?> logRefreshJob = this.logRefreshJob;
182 if (logRefreshJob == null || logRefreshJob.isCancelled()) {
183 Runnable runnable = () -> {
184 // Request humidity data from the thermostat if we are a CT80
186 // send an async call to the thermostat to get the humidity data
187 connector.getAsyncThermostatData(HUMIDITY_RESOURCE);
190 if (!this.disableLogs) {
191 // send an async call to the thermostat to get the runtime data
192 connector.getAsyncThermostatData(RUNTIME_RESOURCE);
196 logRefreshJob = null;
197 this.logRefreshJob = scheduler.scheduleWithFixedDelay(runnable, 1, logRefreshPeriod, TimeUnit.MINUTES);
202 public void dispose() {
203 logger.debug("Disposing the RadioThermostat handler.");
204 connector.removeEventListener(this);
206 ScheduledFuture<?> refreshJob = this.refreshJob;
207 if (refreshJob != null) {
208 refreshJob.cancel(true);
209 this.refreshJob = null;
212 ScheduledFuture<?> logRefreshJob = this.logRefreshJob;
213 if (logRefreshJob != null) {
214 logRefreshJob.cancel(true);
215 this.logRefreshJob = null;
219 public void handleRawCommand(@Nullable String rawCommand) {
220 connector.sendCommand(null, null, rawCommand, DEFAULT_RESOURCE);
224 public void handleCommand(ChannelUID channelUID, Command command) {
225 if (command instanceof RefreshType) {
226 updateChannel(channelUID.getId(), rthermData);
229 String cmdStr = command.toString();
231 // parse out an Integer from the string
232 // ie '70.5 F' becomes 70, also handles negative numbers
233 cmdInt = NumberFormat.getInstance().parse(cmdStr).intValue();
234 } catch (ParseException e) {
235 logger.debug("Command: {} -> Not an integer", cmdStr);
238 switch (channelUID.getId()) {
240 // only do if commanded mode is different than current mode
241 if (!cmdInt.equals(rthermData.getThermostatData().getMode())) {
242 connector.sendCommand("tmode", cmdStr, DEFAULT_RESOURCE);
244 // set the new operating mode, reset everything else,
245 // because refreshing the tstat data below is really slow.
246 rthermData.getThermostatData().setMode(cmdInt);
247 rthermData.getThermostatData().setHeatTarget(0);
248 rthermData.getThermostatData().setCoolTarget(0);
249 updateChannel(SET_POINT, rthermData);
250 rthermData.getThermostatData().setHold(0);
251 updateChannel(HOLD, rthermData);
252 rthermData.getThermostatData().setProgramMode(-1);
253 updateChannel(PROGRAM_MODE, rthermData);
255 // now just trigger a refresh of the thermostat to get the new active setpoint
256 // this takes a while for the JSON request to complete (async).
257 connector.getAsyncThermostatData(DEFAULT_RESOURCE);
261 rthermData.getThermostatData().setFanMode(cmdInt);
262 connector.sendCommand("fmode", cmdStr, DEFAULT_RESOURCE);
265 rthermData.getThermostatData().setProgramMode(cmdInt);
266 connector.sendCommand("program_mode", cmdStr, DEFAULT_RESOURCE);
269 if (command instanceof OnOffType && command == OnOffType.ON) {
270 rthermData.getThermostatData().setHold(1);
271 connector.sendCommand("hold", "1", DEFAULT_RESOURCE);
272 } else if (command instanceof OnOffType && command == OnOffType.OFF) {
273 rthermData.getThermostatData().setHold(0);
274 connector.sendCommand("hold", "0", DEFAULT_RESOURCE);
278 String cmdKey = null;
279 if (rthermData.getThermostatData().getMode() == 1) {
280 cmdKey = this.setpointCmdKeyPrefix + "heat";
281 rthermData.getThermostatData().setHeatTarget(cmdInt);
282 } else if (rthermData.getThermostatData().getMode() == 2) {
283 cmdKey = this.setpointCmdKeyPrefix + "cool";
284 rthermData.getThermostatData().setCoolTarget(cmdInt);
286 // don't do anything if we are not in heat or cool mode
289 connector.sendCommand(cmdKey, cmdInt.toString(), DEFAULT_RESOURCE);
293 QuantityType<?> remoteTemp = ((QuantityType<Temperature>) command)
294 .toUnit(ImperialUnits.FAHRENHEIT);
295 connector.sendCommand("rem_temp", String.valueOf(remoteTemp.intValue()), REMOTE_TEMP_RESOURCE);
297 connector.sendCommand("rem_mode", "0", REMOTE_TEMP_RESOURCE);
301 logger.warn("Unsupported command: {}", command.toString());
307 * Handle a RadioThermostat event received from the listeners
309 * @param event the event received from the listeners
312 public void onNewMessageEvent(RadioThermostatEvent event) {
313 logger.debug("onNewMessageEvent: key {} = {}", event.getKey(), event.getValue());
315 String evtKey = event.getKey();
316 String evtVal = event.getValue();
318 if (KEY_ERROR.equals(evtKey)) {
319 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
320 "Error retrieving data from Thermostat ");
322 updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
324 // Map the JSON response to the correct object and update appropriate channels
326 case DEFAULT_RESOURCE:
327 rthermData.setThermostatData(gson.fromJson(evtVal, RadioThermostatTstatDTO.class));
330 case HUMIDITY_RESOURCE:
331 RadioThermostatHumidityDTO dto = gson.fromJson(evtVal, RadioThermostatHumidityDTO.class);
333 rthermData.setHumidity(dto.getHumidity());
335 updateChannel(HUMIDITY, rthermData);
337 case RUNTIME_RESOURCE:
338 rthermData.setRuntime(gson.fromJson(evtVal, RadioThermostatRuntimeDTO.class));
339 updateChannel(TODAY_HEAT_RUNTIME, rthermData);
340 updateChannel(TODAY_COOL_RUNTIME, rthermData);
341 updateChannel(YESTERDAY_HEAT_RUNTIME, rthermData);
342 updateChannel(YESTERDAY_COOL_RUNTIME, rthermData);
349 * Update the channel from the last Thermostat data retrieved
351 * @param channelId the id identifying the channel to be updated
353 private void updateChannel(String channelId, RadioThermostatDTO rthermData) {
354 if (isLinked(channelId)) {
357 value = getValue(channelId, rthermData);
358 } catch (Exception e) {
359 logger.debug("Error setting {} value", channelId.toUpperCase());
365 state = UnDefType.UNDEF;
366 } else if (value instanceof PointType) {
367 state = (PointType) value;
368 } else if (value instanceof ZonedDateTime) {
369 state = new DateTimeType((ZonedDateTime) value);
370 } else if (value instanceof QuantityType<?>) {
371 state = (QuantityType<?>) value;
372 } else if (value instanceof BigDecimal) {
373 state = new DecimalType((BigDecimal) value);
374 } else if (value instanceof Integer) {
375 state = new DecimalType(BigDecimal.valueOf(((Integer) value).longValue()));
376 } else if (value instanceof String) {
377 state = new StringType(value.toString());
378 } else if (value instanceof OnOffType) {
379 state = (OnOffType) value;
381 logger.warn("Update channel {}: Unsupported value type {}", channelId,
382 value.getClass().getSimpleName());
384 logger.debug("Update channel {} with state {} ({})", channelId, (state == null) ? "null" : state.toString(),
385 (value == null) ? "null" : value.getClass().getSimpleName());
387 // Update the channel
389 updateState(channelId, state);
395 * Update a given channelId from the thermostat data
397 * @param the channel id to be updated
398 * @param data the RadioThermostat dto
399 * @return the value to be set in the state
401 public static @Nullable Object getValue(String channelId, RadioThermostatDTO data) {
404 if (data.getThermostatData().getTemperature() != null) {
405 return new QuantityType<Temperature>(data.getThermostatData().getTemperature(),
406 API_TEMPERATURE_UNIT);
411 if (data.getHumidity() != null) {
412 return new QuantityType<>(data.getHumidity(), API_HUMIDITY_UNIT);
417 return data.getThermostatData().getMode();
419 return data.getThermostatData().getFanMode();
421 return data.getThermostatData().getProgramMode();
423 if (data.getThermostatData().getSetpoint() != 0) {
424 return new QuantityType<Temperature>(data.getThermostatData().getSetpoint(), API_TEMPERATURE_UNIT);
429 return data.getThermostatData().getOverride();
431 return OnOffType.from(data.getThermostatData().getHold() == 1);
433 return data.getThermostatData().getStatus();
435 return data.getThermostatData().getFanStatus();
437 return data.getThermostatData().getTime().getDayOfWeek();
439 return data.getThermostatData().getTime().getHour();
441 return data.getThermostatData().getTime().getMinute();
443 return data.getThermostatData().getTime().getThemostatDateTime();
444 case TODAY_HEAT_RUNTIME:
445 return new QuantityType<>(data.getRuntime().getToday().getHeatTime().getRuntime(), API_MINUTES_UNIT);
446 case TODAY_COOL_RUNTIME:
447 return new QuantityType<>(data.getRuntime().getToday().getCoolTime().getRuntime(), API_MINUTES_UNIT);
448 case YESTERDAY_HEAT_RUNTIME:
449 return new QuantityType<>(data.getRuntime().getYesterday().getHeatTime().getRuntime(),
451 case YESTERDAY_COOL_RUNTIME:
452 return new QuantityType<>(data.getRuntime().getYesterday().getCoolTime().getRuntime(),
459 * Updates all channels from rthermData
461 private void updateAllChannels() {
462 // Update all channels from rthermData
463 for (Channel channel : getThing().getChannels()) {
464 updateChannel(channel.getUID().getId(), rthermData);
469 * Build a list of fan modes based on what model thermostat is used
471 * @return list of state options for thermostat fan modes
473 private List<StateOption> getFanModeOptions() {
474 List<StateOption> fanModeOptions = new ArrayList<>();
476 fanModeOptions.add(new StateOption("0", "Auto"));
478 fanModeOptions.add(new StateOption("1", "Auto/Circulate"));
480 fanModeOptions.add(new StateOption("2", "On"));
482 return fanModeOptions;