2 * Copyright (c) 2010-2023 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.deconz.internal.handler;
15 import static org.openhab.binding.deconz.internal.BindingConstants.*;
16 import static org.openhab.core.library.unit.SIUnits.CELSIUS;
17 import static org.openhab.core.library.unit.Units.PERCENT;
19 import java.math.BigDecimal;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
25 import javax.measure.quantity.Temperature;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.deconz.internal.dto.DeconzBaseMessage;
30 import org.openhab.binding.deconz.internal.dto.SensorConfig;
31 import org.openhab.binding.deconz.internal.dto.SensorMessage;
32 import org.openhab.binding.deconz.internal.dto.SensorState;
33 import org.openhab.binding.deconz.internal.dto.ThermostatUpdateConfig;
34 import org.openhab.binding.deconz.internal.types.ThermostatMode;
35 import org.openhab.core.library.types.DecimalType;
36 import org.openhab.core.library.types.OpenClosedType;
37 import org.openhab.core.library.types.QuantityType;
38 import org.openhab.core.library.types.StringType;
39 import org.openhab.core.thing.ChannelUID;
40 import org.openhab.core.thing.Thing;
41 import org.openhab.core.thing.ThingTypeUID;
42 import org.openhab.core.thing.type.ChannelKind;
43 import org.openhab.core.types.Command;
44 import org.openhab.core.types.RefreshType;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 import com.google.gson.Gson;
51 * This sensor Thermostat Thing doesn't establish any connections, that is done by the bridge Thing.
53 * It waits for the bridge to come online, grab the websocket connection and bridge configuration
54 * and registers to the websocket connection as a listener.
56 * A REST API call is made to get the initial sensor state.
58 * Only the Thermostat is supported by this Thing, because a unified state is kept
59 * in {@link #sensorState}. Every field that got received by the REST API for this specific
60 * sensor is published to the framework.
62 * @author Lukas Agethen - Initial contribution
65 public class SensorThermostatThingHandler extends SensorBaseThingHandler {
66 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_THERMOSTAT);
68 private static final List<String> CONFIG_CHANNELS = Arrays.asList(CHANNEL_BATTERY_LEVEL, CHANNEL_BATTERY_LOW,
69 CHANNEL_HEATSETPOINT, CHANNEL_TEMPERATURE_OFFSET, CHANNEL_THERMOSTAT_MODE);
71 private final Logger logger = LoggerFactory.getLogger(SensorThermostatThingHandler.class);
73 public SensorThermostatThingHandler(Thing thing, Gson gson) {
78 public void handleCommand(ChannelUID channelUID, Command command) {
79 if (command instanceof RefreshType) {
80 sensorState.buttonevent = null;
81 valueUpdated(channelUID, sensorState, false);
84 ThermostatUpdateConfig newConfig = new ThermostatUpdateConfig();
85 switch (channelUID.getId()) {
86 case CHANNEL_HEATSETPOINT:
87 Integer newHeatsetpoint = getTemperatureFromCommand(command);
88 if (newHeatsetpoint == null) {
89 logger.warn("Heatsetpoint must not be null.");
92 newConfig.heatsetpoint = newHeatsetpoint;
94 case CHANNEL_TEMPERATURE_OFFSET:
95 Integer newOffset = getTemperatureFromCommand(command);
96 if (newOffset == null) {
97 logger.warn("Offset must not be null.");
100 newConfig.offset = newOffset;
102 case CHANNEL_THERMOSTAT_MODE:
103 if (command instanceof StringType) {
104 String thermostatMode = ((StringType) command).toString();
106 newConfig.mode = ThermostatMode.valueOf(thermostatMode);
107 } catch (IllegalArgumentException ex) {
108 logger.warn("Invalid thermostat mode: {}. Valid values: {}", thermostatMode,
109 ThermostatMode.values());
112 if (newConfig.mode == ThermostatMode.UNKNOWN) {
113 logger.warn("Invalid thermostat mode: {}. Valid values: {}", thermostatMode,
114 ThermostatMode.values());
122 // no supported command
127 sendCommand(newConfig, command, channelUID, null);
131 protected void valueUpdated(ChannelUID channelUID, SensorConfig newConfig) {
132 super.valueUpdated(channelUID, newConfig);
133 ThermostatMode thermostatMode = newConfig.mode;
134 String mode = thermostatMode != null ? thermostatMode.name() : ThermostatMode.UNKNOWN.name();
135 switch (channelUID.getId()) {
136 case CHANNEL_HEATSETPOINT:
137 updateQuantityTypeChannel(channelUID, newConfig.heatsetpoint, CELSIUS, 1.0 / 100);
139 case CHANNEL_TEMPERATURE_OFFSET:
140 updateQuantityTypeChannel(channelUID, newConfig.offset, CELSIUS, 1.0 / 100);
142 case CHANNEL_THERMOSTAT_MODE:
143 updateState(channelUID, new StringType(mode));
149 protected void valueUpdated(ChannelUID channelUID, SensorState newState, boolean initializing) {
150 super.valueUpdated(channelUID, newState, initializing);
151 switch (channelUID.getId()) {
152 case CHANNEL_TEMPERATURE:
153 updateQuantityTypeChannel(channelUID, newState.temperature, CELSIUS, 1.0 / 100);
155 case CHANNEL_VALVE_POSITION:
156 updateQuantityTypeChannel(channelUID, newState.valve, PERCENT, 100.0 / 255);
158 case CHANNEL_WINDOWOPEN:
159 String open = newState.windowopen;
161 updateState(channelUID, "Closed".equals(open) ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
168 protected void createTypeSpecificChannels(SensorConfig sensorConfig, SensorState sensorState) {
172 protected List<String> getConfigChannels() {
173 return CONFIG_CHANNELS;
176 private @Nullable Integer getTemperatureFromCommand(Command command) {
177 BigDecimal newTemperature;
178 if (command instanceof DecimalType) {
179 newTemperature = ((DecimalType) command).toBigDecimal();
180 } else if (command instanceof QuantityType) {
181 @SuppressWarnings("unchecked")
182 QuantityType<Temperature> temperatureCelsius = ((QuantityType<Temperature>) command).toUnit(CELSIUS);
183 if (temperatureCelsius != null) {
184 newTemperature = temperatureCelsius.toBigDecimal();
191 return newTemperature.scaleByPowerOfTen(2).intValue();
195 protected void processStateResponse(DeconzBaseMessage stateResponse) {
196 if (!(stateResponse instanceof SensorMessage)) {
200 SensorMessage sensorMessage = (SensorMessage) stateResponse;
201 SensorState sensorState = sensorMessage.state;
202 if (sensorState != null && sensorState.windowopen != null) {
203 createChannel(CHANNEL_WINDOWOPEN, ChannelKind.STATE);
206 super.processStateResponse(stateResponse);