]> git.basschouten.com Git - openhab-addons.git/commitdiff
Fix signalStrength channel update after REFRESH command (#12930)
authorJacob Laursen <jacob-github@vindvejr.dk>
Mon, 13 Jun 2022 05:46:20 +0000 (07:46 +0200)
committerGitHub <noreply@github.com>
Mon, 13 Jun 2022 05:46:20 +0000 (07:46 +0200)
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewWebTargets.java
bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/api/responses/Survey.java
bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewShadeHandler.java

index d40ee5b91b31d62242f0f5c304eea93fdb89678d..13798a81130cb59605231b81176bdcd9a6f4ef4b 100644 (file)
@@ -28,6 +28,7 @@ import org.eclipse.jetty.http.HttpMethod;
 import org.eclipse.jetty.http.HttpStatus;
 import org.openhab.binding.hdpowerview.internal.api.Color;
 import org.openhab.binding.hdpowerview.internal.api.ShadePosition;
+import org.openhab.binding.hdpowerview.internal.api.SurveyData;
 import org.openhab.binding.hdpowerview.internal.api.requests.RepeaterBlinking;
 import org.openhab.binding.hdpowerview.internal.api.requests.RepeaterColor;
 import org.openhab.binding.hdpowerview.internal.api.requests.ShadeCalibrate;
@@ -637,12 +638,12 @@ public class HDPowerViewWebTargets {
      * class instance
      *
      * @param shadeId id of the shade to be surveyed
-     * @return Survey class instance
+     * @return List of SurveyData class instances
      * @throws HubInvalidResponseException if response is invalid
      * @throws HubProcessingException if there is any processing error
      * @throws HubMaintenanceException if the hub is down for maintenance
      */
-    public Survey getShadeSurvey(int shadeId)
+    public List<SurveyData> getShadeSurvey(int shadeId)
             throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
         String jsonResponse = invoke(HttpMethod.GET, shades + Integer.toString(shadeId),
                 Query.of("survey", Boolean.toString(true)), null);
@@ -651,7 +652,11 @@ public class HDPowerViewWebTargets {
             if (survey == null) {
                 throw new HubInvalidResponseException("Missing survey response");
             }
-            return survey;
+            List<SurveyData> surveyData = survey.surveyData;
+            if (surveyData == null) {
+                throw new HubInvalidResponseException("Missing 'survey.surveyData' element");
+            }
+            return surveyData;
         } catch (JsonParseException e) {
             throw new HubInvalidResponseException("Error parsing survey response", e);
         }
index 268e035c70e97b7a64e60df2705bb6fe37b33e96..21709fe1a0b9a7e1c8f4635fb2c0bc3ec0b70630 100644 (file)
@@ -13,7 +13,6 @@
 package org.openhab.binding.hdpowerview.internal.api.responses;
 
 import java.util.List;
-import java.util.StringJoiner;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
@@ -30,18 +29,6 @@ import com.google.gson.annotations.SerializedName;
 public class Survey {
     @SerializedName("shade_id")
     public int shadeId;
-    @Nullable
     @SerializedName("survey")
-    public List<SurveyData> surveyData;
-
-    @Override
-    public String toString() {
-        List<SurveyData> surveyData = this.surveyData;
-        if (surveyData == null) {
-            return "{}";
-        }
-        StringJoiner joiner = new StringJoiner(", ");
-        surveyData.forEach(data -> joiner.add(data.toString()));
-        return joiner.toString();
-    }
+    public @Nullable List<SurveyData> surveyData;
 }
index 5a3e203af6c61526caf155a23e87e400a6e36281..35be15d3a9a5af20a31e97287756337bd8c9b714 100644 (file)
@@ -16,7 +16,9 @@ import static org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstan
 import static org.openhab.binding.hdpowerview.internal.api.CoordinateSystem.*;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.StringJoiner;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 
@@ -29,8 +31,8 @@ import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets;
 import org.openhab.binding.hdpowerview.internal.api.CoordinateSystem;
 import org.openhab.binding.hdpowerview.internal.api.Firmware;
 import org.openhab.binding.hdpowerview.internal.api.ShadePosition;
+import org.openhab.binding.hdpowerview.internal.api.SurveyData;
 import org.openhab.binding.hdpowerview.internal.api.responses.Shades.ShadeData;
-import org.openhab.binding.hdpowerview.internal.api.responses.Survey;
 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewShadeConfiguration;
 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase;
 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase.Capabilities;
@@ -257,7 +259,7 @@ public class HDPowerViewShadeHandler extends AbstractHubbedThingHandler {
             updatePositionStates(shadePosition);
         }
         updateBatteryStates(shadeData.batteryStatus, shadeData.batteryStrength);
-        updateState(CHANNEL_SHADE_SIGNAL_STRENGTH, new DecimalType(shadeData.signalStrength));
+        updateSignalStrengthState(shadeData.signalStrength);
     }
 
     private void updateCapabilities(ShadeData shade) {
@@ -423,6 +425,10 @@ public class HDPowerViewShadeHandler extends AbstractHubbedThingHandler {
         updateState(CHANNEL_SHADE_BATTERY_LEVEL, new DecimalType(mappedValue));
     }
 
+    private void updateSignalStrengthState(int signalStrength) {
+        updateState(CHANNEL_SHADE_SIGNAL_STRENGTH, new DecimalType(signalStrength));
+    }
+
     private void moveShade(CoordinateSystem coordSys, int newPercent, HDPowerViewWebTargets webTargets, int shadeId)
             throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException,
             HubShadeTimeoutException {
@@ -528,11 +534,22 @@ public class HDPowerViewShadeHandler extends AbstractHubbedThingHandler {
                     updateDetectedCapabilities(shadeData);
                     break;
                 case SURVEY:
-                    Survey survey = webTargets.getShadeSurvey(shadeId);
-                    if (survey.surveyData != null) {
-                        logger.debug("Survey response for shade {}: {}", survey.shadeId, survey.toString());
+                    List<SurveyData> surveyData = webTargets.getShadeSurvey(shadeId);
+                    if (!surveyData.isEmpty()) {
+                        if (logger.isDebugEnabled()) {
+                            StringJoiner joiner = new StringJoiner(", ");
+                            surveyData.forEach(data -> joiner.add(data.toString()));
+                            logger.debug("Survey response for shade {}: {}", shadeId, joiner.toString());
+                        }
+                        shadeData = webTargets.getShade(shadeId);
+                        updateSignalStrengthState(shadeData.signalStrength);
                     } else {
-                        logger.warn("No response from shade {} survey", shadeId);
+                        logger.info("No data from shade {} survey", shadeId);
+                        /*
+                         * Setting channel to UNDEF here would be reverted on next poll, since
+                         * signal strength is part of shade response. So leaving current value,
+                         * even though refreshing the value failed.
+                         */
                     }
                     break;
                 case BATTERY_LEVEL: