diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java index 7893572ee..2f37def51 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java @@ -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 sensorStatusListeners = new ConcurrentHashMap<>(); private final Map groupStatusListeners = new ConcurrentHashMap<>(); + private List 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 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 scenes, Map 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; }