From c68d92038765f311bb6c7cf3c56bb619a1ecdbbd Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sun, 23 May 2021 19:06:52 +0200 Subject: [PATCH] [homeconnect] Returns an empty list of options for an unsupported program (#10694) * [homeconnect] Returns an empty list of options for an unsupported program Fix #10689 Signed-off-by: Laurent Garnier * Review comment: List.of Signed-off-by: Laurent Garnier --- .../internal/client/HomeConnectApiClient.java | 21 ++++++++++++------- .../handler/HomeConnectHoodHandler.java | 9 +++++++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/client/HomeConnectApiClient.java b/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/client/HomeConnectApiClient.java index 4ea338e7b..8405f6685 100644 --- a/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/client/HomeConnectApiClient.java +++ b/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/client/HomeConnectApiClient.java @@ -12,13 +12,9 @@ */ 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 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 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); diff --git a/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/handler/HomeConnectHoodHandler.java b/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/handler/HomeConnectHoodHandler.java index eef8e5eb6..f0f0beae8 100644 --- a/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/handler/HomeConnectHoodHandler.java +++ b/bundles/org.openhab.binding.homeconnect/src/main/java/org/openhab/binding/homeconnect/internal/handler/HomeConnectHoodHandler.java @@ -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 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)));