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.nikohomecontrol.internal.handler;
15 import static org.openhab.binding.nikohomecontrol.internal.NikoHomeControlBindingConstants.*;
16 import static org.openhab.core.library.unit.SIUnits.CELSIUS;
17 import static org.openhab.core.types.RefreshType.REFRESH;
19 import java.util.HashMap;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
24 import javax.measure.quantity.Temperature;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcThermostat;
29 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcThermostatEvent;
30 import org.openhab.binding.nikohomecontrol.internal.protocol.NikoHomeControlCommunication;
31 import org.openhab.binding.nikohomecontrol.internal.protocol.nhc2.NhcThermostat2;
32 import org.openhab.core.library.types.DecimalType;
33 import org.openhab.core.library.types.QuantityType;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.ChannelUID;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingStatus;
38 import org.openhab.core.thing.ThingStatusDetail;
39 import org.openhab.core.thing.binding.BaseThingHandler;
40 import org.openhab.core.types.Command;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * The {@link NikoHomeControlThermostatHandler} is responsible for handling commands, which are
46 * sent to one of the channels.
48 * @author Mark Herwege - Initial Contribution
51 public class NikoHomeControlThermostatHandler extends BaseThingHandler implements NhcThermostatEvent {
53 private final Logger logger = LoggerFactory.getLogger(NikoHomeControlThermostatHandler.class);
55 private volatile @NonNullByDefault({}) NhcThermostat nhcThermostat;
57 private String thermostatId = "";
58 private int overruleTime;
60 private volatile @Nullable ScheduledFuture<?> refreshTimer; // used to refresh the remaining overrule time every
63 public NikoHomeControlThermostatHandler(Thing thing) {
68 public void handleCommand(ChannelUID channelUID, Command command) {
69 NikoHomeControlCommunication nhcComm = getCommunication();
70 if (nhcComm == null) {
71 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
72 "Niko Home Control: bridge communication not initialized when trying to execute thermostat command "
77 // This can be expensive, therefore do it in a job.
78 scheduler.submit(() -> {
79 if (!nhcComm.communicationActive()) {
80 restartCommunication(nhcComm);
83 if (nhcComm.communicationActive()) {
84 handleCommandSelection(channelUID, command);
89 private void handleCommandSelection(ChannelUID channelUID, Command command) {
90 logger.debug("Niko Home Control: handle command {} for {}", command, channelUID);
92 if (REFRESH.equals(command)) {
93 thermostatEvent(nhcThermostat.getMeasured(), nhcThermostat.getSetpoint(), nhcThermostat.getMode(),
94 nhcThermostat.getOverrule(), nhcThermostat.getDemand());
98 switch (channelUID.getId()) {
99 case CHANNEL_MEASURED:
101 updateStatus(ThingStatus.ONLINE);
105 if (command instanceof DecimalType) {
106 nhcThermostat.executeMode(((DecimalType) command).intValue());
108 updateStatus(ThingStatus.ONLINE);
111 case CHANNEL_SETPOINT:
112 QuantityType<Temperature> setpoint = null;
113 if (command instanceof QuantityType) {
114 setpoint = ((QuantityType<Temperature>) command).toUnit(CELSIUS);
115 // Always set the new setpoint temperature as an overrule
116 // If no overrule time is given yet, set the overrule time to the configuration parameter
117 int time = nhcThermostat.getOverruletime();
121 if (setpoint != null) {
122 nhcThermostat.executeOverrule(Math.round(setpoint.floatValue() * 10), time);
125 updateStatus(ThingStatus.ONLINE);
128 case CHANNEL_OVERRULETIME:
129 if (command instanceof DecimalType) {
130 int overruletime = ((DecimalType) command).intValue();
131 int overrule = nhcThermostat.getOverrule();
132 if (overruletime <= 0) {
136 nhcThermostat.executeOverrule(overrule, overruletime);
138 updateStatus(ThingStatus.ONLINE);
142 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
143 "Niko Home Control: channel unknown " + channelUID.getId());
148 public void initialize() {
149 NikoHomeControlThermostatConfig config = getConfig().as(NikoHomeControlThermostatConfig.class);
151 thermostatId = config.thermostatId;
152 overruleTime = config.overruleTime;
154 NikoHomeControlCommunication nhcComm = getCommunication();
155 if (nhcComm == null) {
159 // We need to do this in a separate thread because we may have to wait for the
160 // communication to become active
161 scheduler.submit(() -> {
162 if (!nhcComm.communicationActive()) {
163 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
164 "Niko Home Control: no connection with Niko Home Control, could not initialize thermostat "
169 nhcThermostat = nhcComm.getThermostats().get(thermostatId);
170 if (nhcThermostat == null) {
171 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
172 "Niko Home Control: thermostatId does not match a thermostat in the controller "
177 nhcThermostat.setEventHandler(this);
181 String thermostatLocation = nhcThermostat.getLocation();
182 if (thing.getLocation() == null) {
183 thing.setLocation(thermostatLocation);
186 thermostatEvent(nhcThermostat.getMeasured(), nhcThermostat.getSetpoint(), nhcThermostat.getMode(),
187 nhcThermostat.getOverrule(), nhcThermostat.getDemand());
189 logger.debug("Niko Home Control: thermostat intialized {}", thermostatId);
191 Bridge bridge = getBridge();
192 if ((bridge != null) && (bridge.getStatus() == ThingStatus.ONLINE)) {
193 updateStatus(ThingStatus.ONLINE);
195 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
200 private void updateProperties() {
201 Map<String, String> properties = new HashMap<>();
203 if (nhcThermostat instanceof NhcThermostat2) {
204 NhcThermostat2 thermostat = (NhcThermostat2) nhcThermostat;
205 properties.put("model", thermostat.getModel());
206 properties.put("technology", thermostat.getTechnology());
209 thing.setProperties(properties);
213 public void thermostatEvent(int measured, int setpoint, int mode, int overrule, int demand) {
214 updateState(CHANNEL_MEASURED, new QuantityType<>(nhcThermostat.getMeasured() / 10.0, CELSIUS));
216 int overruletime = nhcThermostat.getRemainingOverruletime();
217 updateState(CHANNEL_OVERRULETIME, new DecimalType(overruletime));
218 // refresh the remaining time every minute
219 scheduleRefreshOverruletime(nhcThermostat);
221 // If there is an overrule temperature set, use this in the setpoint channel, otherwise use the original
222 // setpoint temperature
223 if (overruletime == 0) {
224 updateState(CHANNEL_SETPOINT, new QuantityType<>(setpoint / 10.0, CELSIUS));
226 updateState(CHANNEL_SETPOINT, new QuantityType<>(overrule / 10.0, CELSIUS));
229 updateState(CHANNEL_MODE, new DecimalType(mode));
231 updateState(CHANNEL_DEMAND, new DecimalType(demand));
233 updateStatus(ThingStatus.ONLINE);
237 * Method to update state of overruletime channel every minute with remaining time.
239 * @param NhcThermostat object
242 private void scheduleRefreshOverruletime(NhcThermostat nhcThermostat) {
243 cancelRefreshTimer();
245 if (nhcThermostat.getRemainingOverruletime() <= 0) {
249 refreshTimer = scheduler.scheduleWithFixedDelay(() -> {
250 int remainingTime = nhcThermostat.getRemainingOverruletime();
251 updateState(CHANNEL_OVERRULETIME, new DecimalType(remainingTime));
252 if (remainingTime <= 0) {
253 cancelRefreshTimer();
255 }, 1, 1, TimeUnit.MINUTES);
258 private void cancelRefreshTimer() {
259 ScheduledFuture<?> timer = refreshTimer;
267 public void thermostatRemoved() {
268 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
269 "Niko Home Control: thermostat has been removed from the controller " + thermostatId);
272 private void restartCommunication(NikoHomeControlCommunication nhcComm) {
273 // We lost connection but the connection object is there, so was correctly started.
274 // Try to restart communication.
275 nhcComm.restartCommunication();
276 // If still not active, take thing offline and return.
277 if (!nhcComm.communicationActive()) {
278 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
279 "Niko Home Control: communication socket error");
282 // Also put the bridge back online
283 NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler();
284 if (nhcBridgeHandler != null) {
285 nhcBridgeHandler.bridgeOnline();
289 private @Nullable NikoHomeControlCommunication getCommunication() {
290 NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler();
291 if (nhcBridgeHandler == null) {
292 updateStatus(ThingStatus.UNINITIALIZED, ThingStatusDetail.BRIDGE_UNINITIALIZED,
293 "Niko Home Control: no bridge initialized for thermostat " + thermostatId);
296 NikoHomeControlCommunication nhcComm = nhcBridgeHandler.getCommunication();
300 private @Nullable NikoHomeControlBridgeHandler getBridgeHandler() {
301 Bridge nhcBridge = getBridge();
302 if (nhcBridge == null) {
303 updateStatus(ThingStatus.UNINITIALIZED, ThingStatusDetail.BRIDGE_UNINITIALIZED,
304 "Niko Home Control: no bridge initialized for thermostat " + thermostatId);
307 NikoHomeControlBridgeHandler nhcBridgeHandler = (NikoHomeControlBridgeHandler) nhcBridge.getHandler();
308 return nhcBridgeHandler;