From 550258299bd984d87b9e7920669f1e99b39d6fb0 Mon Sep 17 00:00:00 2001 From: Haavar Valeur Date: Mon, 29 Aug 2022 03:45:25 -0700 Subject: [PATCH] [Konnected] Fixed null pointer exception on state update with missing state (#13329) * Fixed null pointer exception on state update with missing state * Added variable for state. Changed onvalue configuration type to integer. Signed-off-by: Haavar Valeur --- .../internal/gson/KonnectedModuleGson.java | 6 ++--- .../internal/handler/KonnectedHandler.java | 24 +++++++++---------- .../resources/OH-INF/thing/thing-types.xml | 8 +++---- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/gson/KonnectedModuleGson.java b/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/gson/KonnectedModuleGson.java index 34e3459ca3368..5b9fc7ff84141 100644 --- a/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/gson/KonnectedModuleGson.java +++ b/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/gson/KonnectedModuleGson.java @@ -29,7 +29,7 @@ public class KonnectedModuleGson { private String zone; private String temp; private String humi; - private String state; + private Integer state; @SerializedName("Auth_Token") private String authToken; private String momentary; @@ -79,11 +79,11 @@ public void setHumi(String setHumi) { this.humi = setHumi; } - public String getState() { + public Integer getState() { return state; } - public void setState(String setState) { + public void setState(Integer setState) { this.state = setState; } diff --git a/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/handler/KonnectedHandler.java b/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/handler/KonnectedHandler.java index 9010e2a0f5ef9..3b7bbd3893c85 100644 --- a/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/handler/KonnectedHandler.java +++ b/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/handler/KonnectedHandler.java @@ -94,7 +94,7 @@ public void handleCommand(ChannelUID channelUID, Command command) { int sendCommand = (OnOffType.OFF.compareTo((OnOffType) command)); logger.debug("The command being sent to zone {} for channel:{} is {}", zone, channelUID.getAsString(), sendCommand); - sendActuatorCommand(Integer.toString(sendCommand), zone, channelUID); + sendActuatorCommand(sendCommand, zone, channelUID); } } else if (command instanceof RefreshType) { // check to see if handler has been initialized before attempting to get state of pin, else wait one minute @@ -137,9 +137,12 @@ public void handleWebHookEvent(KonnectedModuleGson event) { // check if the itemType has been defined for the zone received // check the itemType of the Zone, if Contact, send the State if Temp send Temp, etc. if (channelType.contains(CHANNEL_SWITCH) || channelType.contains(CHANNEL_ACTUATOR)) { - OnOffType onOffType = event.getState().equalsIgnoreCase(getOnState(channel)) ? OnOffType.ON - : OnOffType.OFF; - updateState(channelId, onOffType); + Integer state = event.getState(); + logger.debug("The event state is: {}", state); + if (state != null) { + OnOffType onOffType = state == getOnState(channel) ? OnOffType.ON : OnOffType.OFF; + updateState(channelId, onOffType); + } } else if (channelType.contains(CHANNEL_HUMIDITY)) { // if the state is of type number then this means it is the humidity channel of the dht22 updateState(channelId, new QuantityType<>(Double.parseDouble(event.getHumi()), Units.PERCENT)); @@ -415,7 +418,7 @@ private String updateKonnectedModule() throws KonnectedHttpRetryExceeded { * @param scommand the string command, either 0 or 1 to send to the actutor pin on the Konnected module * @param zone the zone to send the command to on the Konnected Module */ - private void sendActuatorCommand(String scommand, String zone, ChannelUID channelId) { + private void sendActuatorCommand(Integer scommand, String zone, ChannelUID channelId) { try { Channel channel = getThing().getChannel(channelId.getId()); if (!(channel == null)) { @@ -429,7 +432,7 @@ private void sendActuatorCommand(String scommand, String zone, ChannelUID channe // check to see if this is an On Command type, if so add the momentary, pause, times to the payload if // they exist on the configuration. - if (scommand.equals(getOnState(channel))) { + if (scommand == getOnState(channel)) { if (configuration.get(CHANNEL_ACTUATOR_TIMES) == null) { logger.debug( "The times configuration was not set for channelID: {}, not adding it to the payload.", @@ -522,12 +525,7 @@ private void sendSetSwitchState(String thingId, String payloadString) throws Kon } } - private String getOnState(Channel channel) { - String config = (String) channel.getConfiguration().get(CHANNEL_ONVALUE); - if (config == null) { - return "1"; - } else { - return config; - } + private int getOnState(Channel channel) { + return ((Number) channel.getConfiguration().get(CHANNEL_ONVALUE)).intValue(); } } diff --git a/bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/thing/thing-types.xml index 64f74385c3feb..bb53cc9f94379 100644 --- a/bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/thing/thing-types.xml @@ -37,7 +37,7 @@ - + The value that will be treated by the binding as the on value. For sensors that activate with a high value leave at the default of 1 and sensors that activate with a low value set to 0. @@ -122,7 +122,7 @@ - + The value that will be treated by the binding as an on command. For actuators that activate with a high command set to 1 and actuators that activate with a low value set to 0. @@ -171,7 +171,7 @@ - + The value that will be treated by the binding as the on value. For sensors that activate with a high value, leave at the default of 1 and sensors that activate with a low value set to 0. @@ -206,7 +206,7 @@ - + The value that will be treated by the binding as an on command. For actuators that activate with a high command set to 1 and actuators that activate with a low value set to 0.