[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 <lg.hc@free.fr>

* Review comment: List.of

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2021-05-23 19:06:52 +02:00 committed by GitHub
parent 266b4d81a4
commit c68d920387
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View File

@ -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<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);

View 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)));