[nuki] Fixed configuration reload on initialization (#12276)

* 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>
This commit is contained in:
Jan Vybíral 2022-02-27 19:46:32 +01:00 committed by GitHub
parent b2765a3895
commit 923f720a5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 21 deletions

View 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.NukiBridgeHandler;
import org.openhab.binding.nuki.internal.handler.NukiOpenerHandler; import org.openhab.binding.nuki.internal.handler.NukiOpenerHandler;
import org.openhab.binding.nuki.internal.handler.NukiSmartLockHandler; 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.id.InstanceUUID;
import org.openhab.core.io.net.http.HttpClientFactory; import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.net.HttpServiceUtil; import org.openhab.core.net.HttpServiceUtil;
@ -88,11 +87,6 @@ public class NukiHandlerFactory extends BaseThingHandlerFactory {
return null; return null;
} }
@Override
protected @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID) {
return super.createThing(thingTypeUID, configuration, thingUID);
}
@Override @Override
public void removeThing(ThingUID thingUID) { public void removeThing(ThingUID thingUID) {
super.removeThing(thingUID); super.removeThing(thingUID);

View File

@ -114,7 +114,12 @@ public class NukiApiServlet extends HttpServlet {
responseEntity = new ResponseEntity(HttpStatus.BAD_REQUEST_400, responseEntity = new ResponseEntity(HttpStatus.BAD_REQUEST_400,
new NukiHttpServerStatusResponseDto("Invalid BCB-Request!")); new NukiHttpServerStatusResponseDto("Invalid BCB-Request!"));
} else { } 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); setHeaders(response);
@ -122,7 +127,7 @@ public class NukiApiServlet extends HttpServlet {
response.getWriter().write(gson.toJson(responseEntity.getData())); 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(); String nukiId = request.getNukiId().toString();
for (NukiBridgeHandler nukiBridgeHandler : nukiBridgeHandlers) { for (NukiBridgeHandler nukiBridgeHandler : nukiBridgeHandlers) {
logger.trace("Searching Bridge[{}] with NukiBridgeHandler[{}] for nukiId[{}].", logger.trace("Searching Bridge[{}] with NukiBridgeHandler[{}] for nukiId[{}].",

View 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.dataexchange.NukiHttpClient;
import org.openhab.binding.nuki.internal.dto.BridgeApiDeviceStateDto; import org.openhab.binding.nuki.internal.dto.BridgeApiDeviceStateDto;
import org.openhab.binding.nuki.internal.dto.BridgeApiListDeviceDto; 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.DateTimeType;
import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.OnOffType;
@ -70,6 +71,11 @@ public abstract class AbstractNukiDeviceHandler<T extends NukiDeviceConfiguratio
@Nullable @Nullable
private NukiHttpClient nukiHttpClient; private NukiHttpClient nukiHttpClient;
public AbstractNukiDeviceHandler(Thing thing) {
super(thing);
this.configuration = getConfigAs(getConfigurationClass());
}
private static String hexToDecimal(String hexString) { private static String hexToDecimal(String hexString) {
return String.valueOf(Integer.parseInt(hexString, 16)); return String.valueOf(Integer.parseInt(hexString, 16));
} }
@ -92,23 +98,34 @@ public abstract class AbstractNukiDeviceHandler<T extends NukiDeviceConfiguratio
} }
} }
public AbstractNukiDeviceHandler(Thing thing) { /**
super(thing); * Performs migration of old device configuration
this.configuration = getConfigAs(getConfigurationClass()); *
* @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) // legacy support - check if nukiId is hexadecimal (which might have been set by previous binding version)
// and convert it to decimal // 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( logger.warn(
"SmartLock '{}' was created by old version of binding. It is recommended to delete it and discover again", "SmartLock '{}' was created by old version of binding. It is recommended to delete it and discover again",
thing.getUID()); thing.getUID());
this.thing.getConfiguration().put(NukiBindingConstants.PROPERTY_NUKI_ID, Configuration newConfig = editConfiguration();
hexToDecimal(configuration.nukiId)); newConfig.put(NukiBindingConstants.PROPERTY_NUKI_ID, hexToDecimal(nukiId));
this.configuration = getConfigAs(getConfigurationClass()); updateConfiguration(newConfig);
return true;
} else {
return false;
} }
} }
@Override @Override
public void initialize() { public void initialize() {
this.configuration = getConfigAs(getConfigurationClass());
if (migrateConfiguration()) {
this.configuration = getConfigAs(getConfigurationClass());
}
scheduler.execute(this::initializeHandler); scheduler.execute(this::initializeHandler);
} }

View File

@ -14,6 +14,7 @@ package org.openhab.binding.nuki.internal.handler;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.Objects;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.nuki.internal.configuration.NukiDeviceConfiguration; 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(), updateState(NukiBindingConstants.CHANNEL_OPENER_RING_ACTION_TIMESTAMP, state.getRingactionTimestamp(),
this::toDateTime); 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); triggerChannel(NukiBindingConstants.CHANNEL_OPENER_RING_ACTION_STATE, NukiBindingConstants.EVENT_RINGING);
lastRingAction = Instant.now(); lastRingAction = Instant.now();
} }

View File

@ -39,11 +39,6 @@ public class NukiSmartLockHandler extends AbstractNukiDeviceHandler<NukiSmartLoc
super(thing); super(thing);
} }
@Override
public void initialize() {
super.initialize();
}
@Override @Override
public void refreshState(BridgeApiDeviceStateDto state) { public void refreshState(BridgeApiDeviceStateDto state) {
updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_LOCK, updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_LOCK,