diff --git a/bundles/org.openhab.binding.livisismarthome/src/main/java/org/openhab/binding/livisismarthome/internal/handler/LivisiBridgeHandler.java b/bundles/org.openhab.binding.livisismarthome/src/main/java/org/openhab/binding/livisismarthome/internal/handler/LivisiBridgeHandler.java index 8609c285f..5de2f8135 100644 --- a/bundles/org.openhab.binding.livisismarthome/src/main/java/org/openhab/binding/livisismarthome/internal/handler/LivisiBridgeHandler.java +++ b/bundles/org.openhab.binding.livisismarthome/src/main/java/org/openhab/binding/livisismarthome/internal/handler/LivisiBridgeHandler.java @@ -114,7 +114,7 @@ public class LivisiBridgeHandler extends BaseBridgeHandler private @Nullable ScheduledFuture reInitJob; private @Nullable ScheduledFuture bridgeRefreshJob; private @NonNullByDefault({}) LivisiBridgeConfiguration bridgeConfiguration; - private @NonNullByDefault({}) OAuthClientService oAuthService; + private @Nullable OAuthClientService oAuthService; private String configVersion = ""; /** @@ -153,8 +153,9 @@ public class LivisiBridgeHandler extends BaseBridgeHandler */ private void initializeClient() { String tokenURL = URLCreator.createTokenURL(bridgeConfiguration.host); - oAuthService = oAuthFactory.createOAuthClientService(thing.getUID().getAsString(), tokenURL, tokenURL, - "clientId", "clientPass", null, true); + OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(thing.getUID().getAsString(), tokenURL, + tokenURL, "clientId", "clientPass", null, true); + this.oAuthService = oAuthService; client = createClient(oAuthService); deviceStructMan = new DeviceStructureManager(createFullDeviceManager(client)); oAuthService.addAccessTokenRefreshListener(this); @@ -349,6 +350,12 @@ public class LivisiBridgeHandler extends BaseBridgeHandler unregisterDeviceStatusListener(bridgeId); cancelJobs(); stopWebSocket(); + OAuthClientService oAuthService = this.oAuthService; + if (oAuthService != null) { + oAuthService.removeAccessTokenRefreshListener(this); + oAuthFactory.ungetOAuthService(thing.getUID().getAsString()); + this.oAuthService = null; + } client = null; deviceStructMan = null; @@ -356,6 +363,15 @@ public class LivisiBridgeHandler extends BaseBridgeHandler logger.debug("LIVISI SmartHome bridge handler shut down."); } + @Override + public void handleRemoval() { + OAuthClientService oAuthService = this.oAuthService; + if (oAuthService != null) { + oAuthFactory.deleteServiceAndAccessToken(thing.getUID().getAsString()); + } + super.handleRemoval(); + } + private synchronized void cancelJobs() { if (cancelJob(reInitJob)) { reInitJob = null; @@ -884,6 +900,10 @@ public class LivisiBridgeHandler extends BaseBridgeHandler } private void requestAccessToken() throws OAuthException, IOException, OAuthResponseException { + OAuthClientService oAuthService = this.oAuthService; + if (oAuthService == null) { + throw new OAuthException("OAuth service is not initialized"); + } oAuthService.getAccessTokenByResourceOwnerPasswordCredentials(LivisiBindingConstants.USERNAME, bridgeConfiguration.password, null); }