Skip to content

Commit

Permalink
[innogysmarthome] VariableActuators are now recognized again (#11741) (
Browse files Browse the repository at this point in the history
…#12008)

Signed-off-by: Sven Strohschein <[email protected]>
  • Loading branch information
Novanic authored Jan 10, 2022
1 parent 35d7d9d commit ab1ce7f
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.openhab.binding.innogysmarthome.internal.InnogyBindingConstants;
import org.openhab.binding.innogysmarthome.internal.client.entity.StatusResponse;
import org.openhab.binding.innogysmarthome.internal.client.entity.action.Action;
import org.openhab.binding.innogysmarthome.internal.client.entity.action.ShutterAction;
Expand Down Expand Up @@ -318,6 +319,8 @@ public void setAlarmActuatorState(final String capabilityId, final boolean alarm

/**
* Load the device and returns a {@link List} of {@link Device}s..
* VariableActuators are returned additionally (independent from the device ids),
* because VariableActuators are everytime available and never have a device state.
*
* @param deviceIds Ids of the devices to return
* @return List of Devices
Expand All @@ -326,7 +329,7 @@ public List<Device> getDevices(Collection<String> deviceIds)
throws IOException, ApiException, AuthenticationException {
logger.debug("Loading innogy devices...");
List<Device> devices = executeGetList(API_URL_DEVICE, Device[].class);
return devices.stream().filter(d -> deviceIds.contains(d.getId())).collect(Collectors.toList());
return devices.stream().filter(d -> isDeviceUsable(d, deviceIds)).collect(Collectors.toList());
}

/**
Expand Down Expand Up @@ -406,4 +409,16 @@ public List<Message> getMessages() throws IOException, ApiException, Authenticat
public String getConfigVersion() {
return configVersion;
}

/**
* Decides if a (discovered) device is usable (available and supported).
*
* @param device device to check
* @param activeDeviceIds active device id (devices with an according available device state)
* @return true when usable, otherwise false
*/
private static boolean isDeviceUsable(Device device, Collection<String> activeDeviceIds) {
return activeDeviceIds.contains(device.getId())
|| InnogyBindingConstants.DEVICE_VARIABLE_ACTUATOR.equals(device.getType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.innogysmarthome.internal.client;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.Arrays;
import java.util.Collections;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
import org.openhab.core.auth.client.oauth2.OAuthClientService;

/**
* @author Sven Strohschein - Initial contribution
*/
@ExtendWith(MockitoExtension.class)
public class InnogyClientTest {

private static final String DEVICES_URL = "https://api.services-smarthome.de/API/1.1/device";

private InnogyClient client;
@Mock
private OAuthClientService oAuthClient;
@Mock
private HttpClient httpClient;

@BeforeEach
public void before() throws Exception {
AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
accessTokenResponse.setAccessToken("accessToken");
when(oAuthClient.getAccessTokenResponse()).thenReturn(accessTokenResponse);

client = new InnogyClient(oAuthClient, httpClient);
}

@Test
public void testGetDevices() throws Exception {
mockRequest(DEVICES_URL, "[ { id: 123 }, { id: 789, type: 'VariableActuator' } ]");
assertEquals(2, client.getDevices(Arrays.asList("123", "456")).size());
}

@Test
public void testGetDevicesNoDeviceIds() throws Exception {
mockRequest(DEVICES_URL, "[ { id: 123 } ]");
assertEquals(0, client.getDevices(Collections.emptyList()).size());
}

@Test
public void testGetDevicesFalseDeviceIds() throws Exception {
mockRequest(DEVICES_URL, "[ { id: 789 }]");
assertEquals(0, client.getDevices(Arrays.asList("123", "456")).size());
}

@Test
public void testGetDevicesNoDevicesNoDeviceIds() throws Exception {
mockRequest(DEVICES_URL, "[]");
assertEquals(0, client.getDevices(Collections.emptyList()).size());
}

@Test
public void testGetDevicesNoDevicesDeviceIds() throws Exception {
mockRequest(DEVICES_URL, "[]");
assertEquals(0, client.getDevices(Arrays.asList("123", "456")).size());
}

private void mockRequest(String url, String responseContent) throws Exception {
ContentResponse response = mock(ContentResponse.class);
when(response.getStatus()).thenReturn(HttpStatus.OK_200);
when(response.getContentAsString()).thenReturn(responseContent);

Request requestMock = mock(Request.class);
when(httpClient.newRequest(url)).thenReturn(requestMock);
when(requestMock.method(any(HttpMethod.class))).thenReturn(requestMock);
when(requestMock.header(any(HttpHeader.class), any())).thenReturn(requestMock);
when(requestMock.idleTimeout(anyLong(), any())).thenReturn(requestMock);
when(requestMock.timeout(anyLong(), any())).thenReturn(requestMock);
when(requestMock.send()).thenReturn(response);
}
}

0 comments on commit ab1ce7f

Please sign in to comment.