Skip to content

Commit

Permalink
[amazonechocontrol] Fix humidity sensor response (#572)
Browse files Browse the repository at this point in the history
Reported on the forum

Signed-off-by: Jan N. Klug <github@klug.nrw>
(cherry picked from commit a231f9a)
Signed-off-by: Jan N. Klug <github@klug.nrw>
  • Loading branch information
J-N-K committed Mar 24, 2024
1 parent 6104110 commit 0c393bb
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
import org.openhab.core.library.unit.Units;
import org.openhab.core.types.Command;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smarthomej.binding.amazonechocontrol.internal.connection.Connection;
import org.smarthomej.binding.amazonechocontrol.internal.dto.smarthome.JsonSmartHomeCapability;
import org.smarthomej.binding.amazonechocontrol.internal.dto.smarthome.JsonSmartHomeDevice;
import org.smarthomej.binding.amazonechocontrol.internal.handler.SmartHomeDeviceHandler;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
Expand All @@ -41,6 +44,7 @@
*/
@NonNullByDefault
public class HandlerHumiditySensor extends AbstractInterfaceHandler {
private final Logger logger = LoggerFactory.getLogger(HandlerHumiditySensor.class);
public static final String INTERFACE = "Alexa.HumiditySensor";

private static final ChannelInfo HUMIDITY = new ChannelInfo("relativeHumidity", "humidity",
Expand All @@ -62,13 +66,18 @@ protected Set<ChannelInfo> findChannelInfos(JsonSmartHomeCapability capability,
public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
QuantityType<Dimensionless> humidityValue = null;
for (JsonObject state : stateList) {
if (HUMIDITY.propertyName.equals(state.get("name").getAsString())) {
JsonObject value = state.get("value").getAsJsonObject();
// For groups take the first
if (humidityValue == null) {
BigDecimal humidity = value.get("value").getAsBigDecimal();
humidityValue = new QuantityType<>(humidity, Units.PERCENT);
if (HUMIDITY.propertyName.equals(state.get("name").getAsString()) && humidityValue == null) {
JsonElement value = state.get("value");
BigDecimal humidity;
if (value.isJsonObject()) {
humidity = value.getAsJsonObject().getAsBigDecimal();
} else if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) {
humidity = value.getAsJsonPrimitive().getAsBigDecimal();
} else {
logger.warn("Could not properly convert {}", state);
continue;
}
humidityValue = new QuantityType<>(humidity, Units.PERCENT);
}
}
smartHomeDeviceHandler.updateState(HUMIDITY.channelId, humidityValue == null ? UnDefType.UNDEF : humidityValue);
Expand Down

0 comments on commit 0c393bb

Please sign in to comment.