]> git.basschouten.com Git - openhab-addons.git/commitdiff
[hue] Retrieve scenes without 10 minutes initial delay (#14289)
authorlolodomo <lg.hc@free.fr>
Fri, 3 Feb 2023 23:02:56 +0000 (00:02 +0100)
committerGitHub <noreply@github.com>
Fri, 3 Feb 2023 23:02:56 +0000 (00:02 +0100)
Fix #14137

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java

index 7893572eea21af531b6bfa14d90321a667e8ff9d..2f37def51ddc91cb7d3a9b34204db895bb31e783 100644 (file)
@@ -16,6 +16,7 @@ import static org.openhab.binding.hue.internal.HueBindingConstants.*;
 import static org.openhab.core.thing.Thing.*;
 
 import java.io.IOException;
+import java.time.Instant;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -24,6 +25,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.Future;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
@@ -119,6 +121,9 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
     private final Map<String, SensorStatusListener> sensorStatusListeners = new ConcurrentHashMap<>();
     private final Map<String, GroupStatusListener> groupStatusListeners = new ConcurrentHashMap<>();
 
+    private List<Scene> lastScenes = new CopyOnWriteArrayList<>();
+    private Instant lastScenesRetrieval = Instant.MIN;
+
     final ReentrantLock pollingLock = new ReentrantLock();
 
     abstract class PollingRunnable implements Runnable {
@@ -242,6 +247,10 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
         protected void doConnectedRun() throws IOException, ApiException {
             updateLights();
             updateGroups();
+            if (lastScenesRetrieval.isBefore(Instant.now().minusSeconds(SCENE_POLLING_INTERVAL))) {
+                updateScenes();
+                lastScenesRetrieval = Instant.now();
+            }
         }
 
         private void updateLights() throws IOException, ApiException {
@@ -381,16 +390,13 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
                 }
             });
         }
-    };
 
-    private final Runnable scenePollingRunnable = new PollingRunnable() {
-        @Override
-        protected void doConnectedRun() throws IOException, ApiException {
-            List<Scene> scenes = hueBridge.getScenes();
-            logger.trace("Scenes detected: {}", scenes);
+        private void updateScenes() throws IOException, ApiException {
+            lastScenes = hueBridge.getScenes();
+            logger.trace("Scenes detected: {}", lastScenes);
 
-            setBridgeSceneChannelStateOptions(scenes, lastGroupStates);
-            notifyGroupSceneUpdate(scenes);
+            setBridgeSceneChannelStateOptions(lastScenes, lastGroupStates);
+            notifyGroupSceneUpdate(lastScenes);
         }
 
         private void setBridgeSceneChannelStateOptions(List<Scene> scenes, Map<String, FullGroup> groups) {
@@ -412,7 +418,6 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
     private @Nullable Future<?> initJob;
     private @Nullable ScheduledFuture<?> lightPollingJob;
     private @Nullable ScheduledFuture<?> sensorPollingJob;
-    private @Nullable ScheduledFuture<?> scenePollingJob;
 
     private @NonNullByDefault({}) HueBridge hueBridge = null;
     private @NonNullByDefault({}) HueBridgeConfig hueBridgeConfig = null;
@@ -660,23 +665,6 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
         sensorPollingJob = null;
     }
 
-    private void startScenePolling() {
-        ScheduledFuture<?> job = scenePollingJob;
-        if (job == null || job.isCancelled()) {
-            // Delay the first execution to give a chance to have all group things registered
-            scenePollingJob = scheduler.scheduleWithFixedDelay(scenePollingRunnable, 5, SCENE_POLLING_INTERVAL,
-                    TimeUnit.SECONDS);
-        }
-    }
-
-    private void stopScenePolling() {
-        ScheduledFuture<?> job = scenePollingJob;
-        if (job != null) {
-            job.cancel(true);
-        }
-        scenePollingJob = null;
-    }
-
     @Override
     public void dispose() {
         logger.debug("Disposing Hue Bridge handler ...");
@@ -686,7 +674,6 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
         }
         stopLightPolling();
         stopSensorPolling();
-        stopScenePolling();
         if (hueBridge != null) {
             hueBridge = null;
         }
@@ -749,7 +736,6 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
     private synchronized void onUpdate() {
         startLightPolling();
         startSensorPolling();
-        startScenePolling();
     }
 
     /**
@@ -967,6 +953,9 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
             final FullGroup lastGroupState = lastGroupStates.get(groupId);
             if (lastGroupState != null) {
                 groupStatusListener.onGroupAdded(lastGroupState);
+                if (!lastScenes.isEmpty()) {
+                    groupStatusListener.onScenesUpdated(lastScenes);
+                }
             }
             return true;
         }