Skip to content

Commit

Permalink
[doorbird] fix controller id (#11190)
Browse files Browse the repository at this point in the history
Signed-off-by: True Random <rantruedom@gmail.com>
  • Loading branch information
TheTrueRandom committed Sep 7, 2021
1 parent 12fba3a commit 0a1f23f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
1 change: 1 addition & 0 deletions bundles/org.openhab.binding.doorbird/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The following configuration parameters are available on the Doorbird A1081 Contr
| Hostname | doorbirdHost | Required | The hostname or IP address of the Doorbird device. |
| User ID | userId | Required | User Id of a Doorbird user that has permissions to access the API. The User ID and Password must be created using the Doorbird smart phone application. |
| Password | userPassword | Required | Password of a Doorbird user. |
| Controller Id | controllerId | Optional | Doorbird Id of the controller to reliable target the relays of this device. E.g. "gggaaa" |

## Discovery

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.doorbird.internal.api;

import java.util.ArrayList;
import java.util.Arrays;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -35,7 +36,6 @@ public class DoorbirdInfo {
private @Nullable String primaryMacAddress;
private @Nullable String wifiMacAddress;
private @Nullable String deviceType;
private @Nullable String controllerId;
private ArrayList<String> relays = new ArrayList<>();

@SuppressWarnings("null")
Expand All @@ -51,13 +51,7 @@ public DoorbirdInfo(String infoJson) throws JsonSyntaxException {
primaryMacAddress = doorbirdInfo.primaryMacAddress;
wifiMacAddress = doorbirdInfo.wifiMacAddress;
deviceType = doorbirdInfo.deviceType;
for (String relay : doorbirdInfo.relays) {
relays.add(relay);
String[] parts = relay.split("@");
if (parts.length == 2) {
controllerId = parts[0];
}
}
relays.addAll(Arrays.asList(doorbirdInfo.relays));
}
}
}
Expand Down Expand Up @@ -86,15 +80,12 @@ public DoorbirdInfo(String infoJson) throws JsonSyntaxException {
return deviceType;
}

public @Nullable String getControllerId() {
return controllerId;
public @Nullable String getControllerId(@Nullable String configId) {
return relays.stream().map(relay -> relay.split("@")).filter(parts -> parts.length == 2).map(parts -> parts[0])
.filter(id -> configId == null || id.equals(configId)).reduce((first, second) -> second).orElse(null);
}

public ArrayList<String> getRelays() {
return relays;
}

public void addRelay(String relay) {
relays.add(relay);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public class ControllerConfiguration {
* Password of the Doorbird doorbell to which the controller is assigned
*/
public @Nullable String userPassword;

/**
* Id of the Doorbird device
*/
public @Nullable String controllerId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void initialize() {
api.setAuthorization(host, user, password);

// Get the Id of the controller for use in the open door API
controllerId = getControllerId();
controllerId = getControllerId(config.controllerId);
if (controllerId != null) {
updateStatus(ThingStatus.ONLINE);
} else {
Expand Down Expand Up @@ -105,8 +105,8 @@ private void handleOpenDoor(Command command, String doorNumber) {
}
}

private @Nullable String getControllerId() {
private @Nullable String getControllerId(@Nullable String configId) {
DoorbirdInfo info = api.getDoorbirdInfo();
return info == null ? null : info.getControllerId();
return info == null ? null : info.getControllerId(configId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void testParsingWithoutControllerId() {
public void testGetControllerId() {
DoorbirdInfo info = new DoorbirdInfo(infoWithControllerId);

assertEquals("gggaaa", info.getControllerId());
assertEquals("gggaaa", info.getControllerId(null));

assertTrue(info.getRelays().contains("gggaaa@1"));
assertTrue(info.getRelays().contains("gggaaa@2"));
Expand All @@ -92,6 +92,6 @@ public void testGetControllerId() {
public void testControllerIdIsNull() {
DoorbirdInfo info = new DoorbirdInfo(infoWithoutControllerId);

assertNull(info.getControllerId());
assertNull(info.getControllerId(null));
}
}

0 comments on commit 0a1f23f

Please sign in to comment.