]> git.basschouten.com Git - openhab-addons.git/commitdiff
[homeconnect] Returns an empty list of options for an unsupported program (#10694)
authorlolodomo <lg.hc@free.fr>
Sun, 23 May 2021 17:06:52 +0000 (19:06 +0200)
committerGitHub <noreply@github.com>
Sun, 23 May 2021 17:06:52 +0000 (19:06 +0200)
* [homeconnect] Returns an empty list of options for an unsupported program

Fix #10689

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
* Review comment: List.of

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/client/HomeConnectApiClient.java
bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/handler/HomeConnectHoodHandler.java

index 4ea338e7b703594e6eddc88ac6eb964e6c866ced..8405f6685df8e147879dd7867e9e668df0c58fef 100644 (file)
  */
 package org.openhab.binding.homeconnect.internal.client;
 
-import static java.util.Arrays.asList;
 import static java.util.Collections.singletonList;
 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.*;
-import static org.openhab.binding.homeconnect.internal.client.HttpHelper.formatJsonBody;
-import static org.openhab.binding.homeconnect.internal.client.HttpHelper.getAuthorizationHeader;
-import static org.openhab.binding.homeconnect.internal.client.HttpHelper.parseString;
-import static org.openhab.binding.homeconnect.internal.client.HttpHelper.sendRequest;
+import static org.openhab.binding.homeconnect.internal.client.HttpHelper.*;
 
 import java.time.ZonedDateTime;
 import java.util.ArrayList;
@@ -633,12 +629,21 @@ public class HomeConnectApiClient {
         Request request = createRequest(HttpMethod.GET, BASE_PATH + haId + "/programs/available/" + programKey);
         try {
             ContentResponse response = sendRequest(request, apiBridgeConfiguration.getClientId());
-            checkResponseCode(HttpStatus.OK_200, request, response, haId, null);
+            checkResponseCode(List.of(HttpStatus.OK_200, HttpStatus.NOT_FOUND_404), request, response, haId, null);
 
             String responseBody = response.getContentAsString();
             trackAndLogApiRequest(haId, request, null, response, responseBody);
 
-            List<AvailableProgramOption> availableProgramOptions = mapToAvailableProgramOption(responseBody, haId);
+            // Code 404 accepted only if the returned error is "SDK.Error.UnsupportedProgram"
+            if (response.getStatus() == HttpStatus.NOT_FOUND_404
+                    && (responseBody == null || !responseBody.contains("SDK.Error.UnsupportedProgram"))) {
+                throw new CommunicationException(HttpStatus.NOT_FOUND_404, response.getReason(),
+                        responseBody == null ? "" : responseBody);
+            }
+
+            List<AvailableProgramOption> availableProgramOptions = response.getStatus() == HttpStatus.OK_200
+                    ? mapToAvailableProgramOption(responseBody, haId)
+                    : List.of();
             availableProgramOptionsCache.put(programKey, availableProgramOptions);
             return availableProgramOptions;
         } catch (InterruptedException | TimeoutException | ExecutionException e) {
@@ -728,7 +733,7 @@ public class HomeConnectApiClient {
         Request request = createRequest(HttpMethod.GET, path);
         try {
             ContentResponse response = sendRequest(request, apiBridgeConfiguration.getClientId());
-            checkResponseCode(asList(HttpStatus.OK_200, HttpStatus.NOT_FOUND_404), request, response, haId, null);
+            checkResponseCode(List.of(HttpStatus.OK_200, HttpStatus.NOT_FOUND_404), request, response, haId, null);
 
             String responseBody = response.getContentAsString();
             trackAndLogApiRequest(haId, request, null, response, responseBody);
index eef8e5eb63e61adaac8df3a00fef72480ca17099..f0f0beae8dcbe8fda69d9f8b37e9b7e5c533f74d 100644 (file)
@@ -17,6 +17,7 @@ import static java.util.Collections.emptyList;
 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.*;
 
 import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 
@@ -25,6 +26,7 @@ import org.openhab.binding.homeconnect.internal.client.HomeConnectApiClient;
 import org.openhab.binding.homeconnect.internal.client.exception.ApplianceOfflineException;
 import org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException;
 import org.openhab.binding.homeconnect.internal.client.exception.CommunicationException;
+import org.openhab.binding.homeconnect.internal.client.model.AvailableProgramOption;
 import org.openhab.binding.homeconnect.internal.client.model.Data;
 import org.openhab.binding.homeconnect.internal.type.HomeConnectDynamicStateDescriptionProvider;
 import org.openhab.core.library.types.OnOffType;
@@ -217,7 +219,12 @@ public class HomeConnectHoodHandler extends AbstractHomeConnectThingHandler {
                                 new StateOption(COMMAND_DELAYED_SHUT_OFF, mapStringType(availableProgram.getKey())));
                     } else if (PROGRAM_HOOD_VENTING.equals(availableProgram.getKey())) {
                         try {
-                            apiClient.get().getProgramOptions(getThingHaId(), PROGRAM_HOOD_VENTING).forEach(option -> {
+                            List<AvailableProgramOption> availableProgramOptions = apiClient.get()
+                                    .getProgramOptions(getThingHaId(), PROGRAM_HOOD_VENTING);
+                            if (availableProgramOptions.isEmpty()) {
+                                throw new CommunicationException("Program " + PROGRAM_HOOD_VENTING + " is unsupported");
+                            }
+                            availableProgramOptions.forEach(option -> {
                                 if (OPTION_HOOD_VENTING_LEVEL.equalsIgnoreCase(option.getKey())) {
                                     option.getAllowedValues().stream().filter(s -> !STAGE_FAN_OFF.equalsIgnoreCase(s))
                                             .forEach(s -> stateOptions.add(createVentingStateOption(s)));