]> git.basschouten.com Git - openhab-addons.git/commitdiff
[nuki] Fixed configuration reload on initialization (#12276)
authorJan Vybíral <jan.vybiral1@gmail.com>
Sun, 27 Feb 2022 18:46:32 +0000 (19:46 +0100)
committerGitHub <noreply@github.com>
Sun, 27 Feb 2022 18:46:32 +0000 (19:46 +0100)
* Fixed bug where thing configuration was not reloaded after reinitialization
* Added better logging when processing bridge callback

Signed-off-by: Jan Vybíral <jan.vybiral1@gmail.com>
bundles/org.openhab.binding.nuki/src/main/java/org/openhab/binding/nuki/internal/NukiHandlerFactory.java
bundles/org.openhab.binding.nuki/src/main/java/org/openhab/binding/nuki/internal/dataexchange/NukiApiServlet.java
bundles/org.openhab.binding.nuki/src/main/java/org/openhab/binding/nuki/internal/handler/AbstractNukiDeviceHandler.java
bundles/org.openhab.binding.nuki/src/main/java/org/openhab/binding/nuki/internal/handler/NukiOpenerHandler.java
bundles/org.openhab.binding.nuki/src/main/java/org/openhab/binding/nuki/internal/handler/NukiSmartLockHandler.java

index ab4cd67514d765c8c6a50923a0374127ad9be38b..8e92155b1618ba9aea54eb739f4bd70062e20b76 100644 (file)
@@ -21,7 +21,6 @@ import org.openhab.binding.nuki.internal.dataexchange.NukiApiServlet;
 import org.openhab.binding.nuki.internal.handler.NukiBridgeHandler;
 import org.openhab.binding.nuki.internal.handler.NukiOpenerHandler;
 import org.openhab.binding.nuki.internal.handler.NukiSmartLockHandler;
-import org.openhab.core.config.core.Configuration;
 import org.openhab.core.id.InstanceUUID;
 import org.openhab.core.io.net.http.HttpClientFactory;
 import org.openhab.core.net.HttpServiceUtil;
@@ -88,11 +87,6 @@ public class NukiHandlerFactory extends BaseThingHandlerFactory {
         return null;
     }
 
-    @Override
-    protected @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID) {
-        return super.createThing(thingTypeUID, configuration, thingUID);
-    }
-
     @Override
     public void removeThing(ThingUID thingUID) {
         super.removeThing(thingUID);
index 597d508533a6ec1b96055a50e0f9ca4d2a1b3b5b..675efd4d505b1fc92bc811e03b4f07336b028fe7 100644 (file)
@@ -114,7 +114,12 @@ public class NukiApiServlet extends HttpServlet {
             responseEntity = new ResponseEntity(HttpStatus.BAD_REQUEST_400,
                     new NukiHttpServerStatusResponseDto("Invalid BCB-Request!"));
         } else {
-            responseEntity = doHandle(bridgeApiLockStateRequestDto, request.getParameter("bridgeId"));
+            try {
+                responseEntity = doHandle(bridgeApiLockStateRequestDto);
+            } catch (Exception e) {
+                logger.warn("Error processing request '{}'", gson.toJson(bridgeApiLockStateRequestDto), e);
+                throw e;
+            }
         }
 
         setHeaders(response);
@@ -122,7 +127,7 @@ public class NukiApiServlet extends HttpServlet {
         response.getWriter().write(gson.toJson(responseEntity.getData()));
     }
 
-    private ResponseEntity doHandle(BridgeApiLockStateRequestDto request, @Nullable String bridgeId) {
+    private ResponseEntity doHandle(BridgeApiLockStateRequestDto request) {
         String nukiId = request.getNukiId().toString();
         for (NukiBridgeHandler nukiBridgeHandler : nukiBridgeHandlers) {
             logger.trace("Searching Bridge[{}] with NukiBridgeHandler[{}] for nukiId[{}].",
index 7e572721da69d0276a5b3e7a96bb87d10b8bc6fd..23387dcd763c4749b4f0b1247cda31b0f4f88e60 100644 (file)
@@ -32,6 +32,7 @@ import org.openhab.binding.nuki.internal.dataexchange.NukiBaseResponse;
 import org.openhab.binding.nuki.internal.dataexchange.NukiHttpClient;
 import org.openhab.binding.nuki.internal.dto.BridgeApiDeviceStateDto;
 import org.openhab.binding.nuki.internal.dto.BridgeApiListDeviceDto;
+import org.openhab.core.config.core.Configuration;
 import org.openhab.core.library.types.DateTimeType;
 import org.openhab.core.library.types.DecimalType;
 import org.openhab.core.library.types.OnOffType;
@@ -70,6 +71,11 @@ public abstract class AbstractNukiDeviceHandler<T extends NukiDeviceConfiguratio
     @Nullable
     private NukiHttpClient nukiHttpClient;
 
+    public AbstractNukiDeviceHandler(Thing thing) {
+        super(thing);
+        this.configuration = getConfigAs(getConfigurationClass());
+    }
+
     private static String hexToDecimal(String hexString) {
         return String.valueOf(Integer.parseInt(hexString, 16));
     }
@@ -92,23 +98,34 @@ public abstract class AbstractNukiDeviceHandler<T extends NukiDeviceConfiguratio
         }
     }
 
-    public AbstractNukiDeviceHandler(Thing thing) {
-        super(thing);
-        this.configuration = getConfigAs(getConfigurationClass());
+    /**
+     * Performs migration of old device configuration
+     * 
+     * @return true if configuration was change and reload is needed
+     */
+    protected boolean migrateConfiguration() {
+        String nukiId = getConfig().get(NukiBindingConstants.PROPERTY_NUKI_ID).toString();
         // legacy support - check if nukiId is hexadecimal (which might have been set by previous binding version)
         // and convert it to decimal
-        if (NUKI_ID_HEX_PATTERN.matcher(this.configuration.nukiId).matches()) {
+        if (NUKI_ID_HEX_PATTERN.matcher(nukiId).matches()) {
             logger.warn(
                     "SmartLock '{}' was created by old version of binding. It is recommended to delete it and discover again",
                     thing.getUID());
-            this.thing.getConfiguration().put(NukiBindingConstants.PROPERTY_NUKI_ID,
-                    hexToDecimal(configuration.nukiId));
-            this.configuration = getConfigAs(getConfigurationClass());
+            Configuration newConfig = editConfiguration();
+            newConfig.put(NukiBindingConstants.PROPERTY_NUKI_ID, hexToDecimal(nukiId));
+            updateConfiguration(newConfig);
+            return true;
+        } else {
+            return false;
         }
     }
 
     @Override
     public void initialize() {
+        this.configuration = getConfigAs(getConfigurationClass());
+        if (migrateConfiguration()) {
+            this.configuration = getConfigAs(getConfigurationClass());
+        }
         scheduler.execute(this::initializeHandler);
     }
 
index 196f8882e337e272c431f159155b638c58c45dc1..662c5806146da61c7397eb030b7b2f8c9773104b 100644 (file)
@@ -14,6 +14,7 @@ package org.openhab.binding.nuki.internal.handler;
 
 import java.time.Duration;
 import java.time.Instant;
+import java.util.Objects;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.binding.nuki.internal.configuration.NukiDeviceConfiguration;
@@ -49,7 +50,8 @@ public class NukiOpenerHandler extends AbstractNukiDeviceHandler<NukiDeviceConfi
         updateState(NukiBindingConstants.CHANNEL_OPENER_RING_ACTION_TIMESTAMP, state.getRingactionTimestamp(),
                 this::toDateTime);
 
-        if (state.getRingactionState() && Duration.between(lastRingAction, Instant.now()).getSeconds() > 30) {
+        if (Objects.equals(state.getRingactionState(), true)
+                && Duration.between(lastRingAction, Instant.now()).getSeconds() > 30) {
             triggerChannel(NukiBindingConstants.CHANNEL_OPENER_RING_ACTION_STATE, NukiBindingConstants.EVENT_RINGING);
             lastRingAction = Instant.now();
         }
index 50caba669c554f546be17e018ca6c77e3c6e2411..39d2db1fe21f974b4dd7dc17330a9aee2dec7fb5 100644 (file)
@@ -39,11 +39,6 @@ public class NukiSmartLockHandler extends AbstractNukiDeviceHandler<NukiSmartLoc
         super(thing);
     }
 
-    @Override
-    public void initialize() {
-        super.initialize();
-    }
-
     @Override
     public void refreshState(BridgeApiDeviceStateDto state) {
         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_LOCK,