[netatmo] Avoid sending refresh requests twice (#20286)

* Avoids sending refresh requests twice

Signed-off-by: gael@lhopital.org <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital
2026-02-28 13:32:57 +01:00
committed by GitHub
parent def04d09f0
commit 3e5597d538
3 changed files with 27 additions and 15 deletions
@@ -94,8 +94,8 @@ public interface CommonInterface {
: null;
}
default Optional<ScheduledFuture<?>> schedule(Runnable arg0, Duration delay) {
return Optional.of(getScheduler().schedule(arg0, delay.getSeconds(), TimeUnit.SECONDS));
default ScheduledFuture<?> schedule(Runnable arg0, Duration delay) {
return getScheduler().schedule(arg0, delay.getSeconds(), TimeUnit.SECONDS);
}
default @Nullable ApiBridgeHandler getAccountHandler() {
@@ -12,7 +12,6 @@
*/
package org.openhab.binding.netatmo.internal.handler.capability;
import java.time.Duration;
import java.util.concurrent.ScheduledFuture;
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -29,9 +28,8 @@ import org.slf4j.LoggerFactory;
*/
@NonNullByDefault
public class ParentUpdateCapability extends Capability {
private static final Duration DEFAULT_DELAY = Duration.ofSeconds(2);
private final Logger logger = LoggerFactory.getLogger(ParentUpdateCapability.class);
private @Nullable ScheduledFuture<?> job;
public ParentUpdateCapability(CommonInterface handler) {
@@ -40,22 +38,36 @@ public class ParentUpdateCapability extends Capability {
@Override
public void initialize() {
handler.schedule(() -> {
if (job != null) {
logger.debug("Data update is already requested for '{}'", thingUID);
return;
}
this.job = handler.schedule(() -> {
logger.debug("Requesting parents data update for Thing '{}'", thingUID);
CommonInterface bridgeHandler = handler.getBridgeHandler();
if (bridgeHandler != null) {
if (handler.getBridgeHandler() instanceof CommonInterface bridgeHandler) {
bridgeHandler.expireData();
}
}, DEFAULT_DELAY).ifPresent(job -> this.job = job);
job = null;
}, RefreshCapability.ASAP);
}
@Override
protected void beforeNewData() {
super.beforeNewData();
cancelJob();
}
private void cancelJob() {
if (job instanceof ScheduledFuture local) {
local.cancel(true);
this.job = null;
}
}
@Override
public void dispose() {
ScheduledFuture<?> job = this.job;
if (job != null) {
job.cancel(true);
}
this.job = null;
cancelJob();
super.dispose();
}
}
@@ -112,7 +112,7 @@ public class RefreshCapability extends Capability {
stopJob();
}
logger.debug("'{}' next refresh in {}", thingUID, delay);
handler.schedule(this::proceedWithUpdate, delay).ifPresent(job -> this.refreshJob = job);
this.refreshJob = handler.schedule(this::proceedWithUpdate, delay);
}
private void stopJob() {