From 14e7109a252ba9c6182a4e31d1323f5b2e463cef Mon Sep 17 00:00:00 2001 From: lolodomo Date: Thu, 11 May 2023 22:01:49 +0200 Subject: [PATCH] [automower] Remove the access token when the thing is removed (#14971) Related to #14818 Signed-off-by: Laurent Garnier --- .../bridge/AutomowerBridgeHandler.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridgeHandler.java b/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridgeHandler.java index 6eafeb2f6..414c2847e 100644 --- a/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridgeHandler.java +++ b/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridgeHandler.java @@ -54,7 +54,7 @@ public class AutomowerBridgeHandler extends BaseBridgeHandler { private final OAuthFactory oAuthFactory; - private @NonNullByDefault({}) OAuthClientService oAuthService; + private @Nullable OAuthClientService oAuthService; private @Nullable ScheduledFuture automowerBridgePollingJob; private @Nullable AutomowerBridge bridge; private final HttpClient httpClient; @@ -85,7 +85,11 @@ public class AutomowerBridgeHandler extends BaseBridgeHandler { stopAutomowerBridgePolling(currentBridge); bridge = null; } - oAuthFactory.ungetOAuthService(thing.getUID().getAsString()); + OAuthClientService oAuthService = this.oAuthService; + if (oAuthService != null) { + oAuthFactory.ungetOAuthService(thing.getUID().getAsString()); + this.oAuthService = null; + } } @Override @@ -104,8 +108,9 @@ public class AutomowerBridgeHandler extends BaseBridgeHandler { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/conf-error-invalid-polling-interval"); } else { - oAuthService = oAuthFactory.createOAuthClientService(thing.getUID().getAsString(), HUSQVARNA_API_TOKEN_URL, - null, appKey, appSecret, null, null); + OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(thing.getUID().getAsString(), + HUSQVARNA_API_TOKEN_URL, null, appKey, appSecret, null, null); + this.oAuthService = oAuthService; if (bridge == null) { AutomowerBridge currentBridge = new AutomowerBridge(oAuthService, appKey, httpClient, scheduler); @@ -116,6 +121,12 @@ public class AutomowerBridgeHandler extends BaseBridgeHandler { } } + @Override + public void handleRemoval() { + oAuthFactory.deleteServiceAndAccessToken(thing.getUID().getAsString()); + super.handleRemoval(); + } + private void startAutomowerBridgePolling(AutomowerBridge bridge, @Nullable Integer pollingIntervalS) { ScheduledFuture currentPollingJob = automowerBridgePollingJob; if (currentPollingJob == null) {