]> git.basschouten.com Git - openhab-addons.git/commitdiff
[doorbird] fix controller id (#11190)
authorTheTrueRandom <43322965+TheTrueRandom@users.noreply.github.com>
Tue, 7 Sep 2021 20:48:33 +0000 (22:48 +0200)
committerGitHub <noreply@github.com>
Tue, 7 Sep 2021 20:48:33 +0000 (22:48 +0200)
Signed-off-by: True Random <rantruedom@gmail.com>
bundles/org.openhab.binding.doorbird/README.md
bundles/org.openhab.binding.doorbird/src/main/java/org/openhab/binding/doorbird/internal/api/DoorbirdInfo.java
bundles/org.openhab.binding.doorbird/src/main/java/org/openhab/binding/doorbird/internal/config/ControllerConfiguration.java
bundles/org.openhab.binding.doorbird/src/main/java/org/openhab/binding/doorbird/internal/handler/ControllerHandler.java
bundles/org.openhab.binding.doorbird/src/test/java/org/openhab/binding/doorbird/internal/DoorbirdInfoTest.java

index b0fd966ff1284734350a55f3022d448fefd5466b..52aa88eefa8f3731d7ff56e83539a92dca17c97c 100644 (file)
@@ -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
 
index 26dbec9d5134fcce9447efd624e1fa632a9b0eb8..0edb0d43c440f351c1fa9f1e6bef5ee48e7dd1d4 100644 (file)
@@ -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;
@@ -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")
@@ -51,13 +51,7 @@ public class DoorbirdInfo {
                 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));
             }
         }
     }
@@ -86,15 +80,12 @@ public class DoorbirdInfo {
         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);
-    }
 }
index 3c172607faaec2255e35d499ffa2e8846d9dfe95..bddab107014def9b439bc22b23279b2c8d126e96 100644 (file)
@@ -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;
 }
index dd8869145062c45374599fc8ea0232486a21301e..f9780b352e65082409e9ae4da77e89233cb629df 100644 (file)
@@ -68,7 +68,7 @@ public class ControllerHandler extends BaseThingHandler {
         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 {
@@ -105,8 +105,8 @@ public class ControllerHandler extends BaseThingHandler {
         }
     }
 
-    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);
     }
 }
index 7397eaa94d9ea5f0efdf3c59e82ff47a168119ae..3941578acf395b4c713e9d3da60ee20b1b13be8d 100644 (file)
@@ -81,7 +81,7 @@ public class DoorbirdInfoTest {
     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"));
@@ -92,6 +92,6 @@ public class DoorbirdInfoTest {
     public void testControllerIdIsNull() {
         DoorbirdInfo info = new DoorbirdInfo(infoWithoutControllerId);
 
-        assertNull(info.getControllerId());
+        assertNull(info.getControllerId(null));
     }
 }