Skip to content

Commit

Permalink
[somfytahoma] Improved handling of target temperature command (openha…
Browse files Browse the repository at this point in the history
…b#10336)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored and thinkingstone committed Nov 7, 2021
1 parent 3d1cda9 commit a2381f4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.measure.Unit;

Expand Down Expand Up @@ -231,6 +233,10 @@ protected void cacheStateType(String stateName, int type) {
}
}

protected Unit<?> getTemperatureUnit() {
return Objects.requireNonNull(units.get("Number:Temperature"));
}

private void updateUnits(List<SomfyTahomaState> attributes) {
for (SomfyTahomaState attr : attributes) {
if ("core:MeasuredValueType".equals(attr.getName()) && attr.getType() == TYPE_STRING) {
Expand Down Expand Up @@ -387,12 +393,7 @@ public void updateThingStatus(List<SomfyTahomaState> states) {
}

private @Nullable SomfyTahomaState getStatusState(List<SomfyTahomaState> states) {
for (SomfyTahomaState state : states) {
if (STATUS_STATE.equals(state.getName()) && state.getType() == TYPE_STRING) {
return state;
}
}
return null;
return getState(states, STATUS_STATE, TYPE_STRING);
}

private void updateThingStatus(@Nullable SomfyTahomaState state) {
Expand Down Expand Up @@ -456,4 +457,29 @@ public void updateThingChannels(SomfyTahomaState state) {
public int toInteger(Command command) {
return (command instanceof DecimalType) ? ((DecimalType) command).intValue() : 0;
}

public @Nullable BigDecimal toTemperature(Command command) {
BigDecimal temperature = null;
if (command instanceof QuantityType<?>) {
QuantityType<?> quantity = (QuantityType<?>) command;
QuantityType<?> convertedQuantity = quantity.toUnit(getTemperatureUnit());
if (convertedQuantity != null) {
quantity = convertedQuantity;
}
temperature = quantity.toBigDecimal();
} else if (command instanceof DecimalType) {
temperature = ((DecimalType) command).toBigDecimal();
}
return temperature;
}

public static @Nullable SomfyTahomaState getState(List<SomfyTahomaState> states, String stateName,
@Nullable Integer stateType) {
for (SomfyTahomaState state : states) {
if (stateName.equals(state.getName()) && (stateType == null || stateType == state.getType())) {
return state;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.Thing;

/**
Expand All @@ -22,6 +23,7 @@
*
* @author Ondrej Pecta - Initial contribution
*/
@NonNullByDefault
public class SomfyTahomaElectricitySensorHandler extends SomfyTahomaBaseThingHandler {

public SomfyTahomaElectricitySensorHandler(Thing thing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.*;

import java.math.BigDecimal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.types.Command;
Expand Down Expand Up @@ -47,10 +48,12 @@ public void handleCommand(ChannelUID channelUID, Command command) {
if (command instanceof RefreshType) {
return;
} else {
if (DEROGATED_TARGET_TEMPERATURE.equals(channelUID.getId()) && command instanceof QuantityType) {
QuantityType type = (QuantityType) command;
String param = "[" + type.doubleValue() + ", \"next_mode\"]";
sendCommand(COMMAND_SET_DEROGATION, param);
if (DEROGATED_TARGET_TEMPERATURE.equals(channelUID.getId())) {
BigDecimal temperature = toTemperature(command);
if (temperature != null) {
String param = "[" + temperature.toPlainString() + ", \"next_mode\"]";
sendCommand(COMMAND_SET_DEROGATION, param);
}
} else if (DEROGATION_HEATING_MODE.equals(channelUID.getId())) {
switch (command.toString()) {
case "auto":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.*;

import java.math.BigDecimal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand Down Expand Up @@ -44,8 +46,11 @@ public void handleCommand(ChannelUID channelUID, Command command) {
return;
} else {
if (TARGET_TEMPERATURE.equals(channelUID.getId())) {
String param = "[" + command.toString() + "]";
sendCommand("setTargetTemperature", param);
BigDecimal temperature = toTemperature(command);
if (temperature != null) {
String param = "[" + temperature.toPlainString() + "]";
sendCommand("setTargetTemperature", param);
}
}
}
}
Expand Down

0 comments on commit a2381f4

Please sign in to comment.