From f4d053ab51a71edd59ec1f6b63b5f20b1acc6c31 Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Sun, 24 Mar 2024 17:54:59 +0100 Subject: [PATCH] [amazonechocontrol] Fix humidity sensor response (#572) Reported on the forum Signed-off-by: Jan N. Klug (cherry picked from commit a231f9a7142f31dc3dc3aee808c72e465929c513) Signed-off-by: Jan N. Klug --- .../smarthome/HandlerHumiditySensor.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/bundles/org.smarthomej.binding.amazonechocontrol/src/main/java/org/smarthomej/binding/amazonechocontrol/internal/smarthome/HandlerHumiditySensor.java b/bundles/org.smarthomej.binding.amazonechocontrol/src/main/java/org/smarthomej/binding/amazonechocontrol/internal/smarthome/HandlerHumiditySensor.java index ee2e94b563..694ed792c1 100644 --- a/bundles/org.smarthomej.binding.amazonechocontrol/src/main/java/org/smarthomej/binding/amazonechocontrol/internal/smarthome/HandlerHumiditySensor.java +++ b/bundles/org.smarthomej.binding.amazonechocontrol/src/main/java/org/smarthomej/binding/amazonechocontrol/internal/smarthome/HandlerHumiditySensor.java @@ -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; /** @@ -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", @@ -62,13 +66,18 @@ protected Set findChannelInfos(JsonSmartHomeCapability capability, public void updateChannels(String interfaceName, List stateList, UpdateChannelResult result) { QuantityType 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);