From e26d66463eff2734975329e57bc2c8691fb04cd6 Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Wed, 28 Dec 2022 20:52:30 +0100 Subject: [PATCH] Fix DateTimeTrigger not triggering on restored state During startup most likely the item state is `UndefType.NULL` which results in no triggering. States are restored from persistence by directly setting the item state, not by an `ItemStateEvent` and were therefore missed. Since changing the trigger is not necessary when the state updates but does not change, the event listening was changed to `ItemStateChangedEvent` only. Signed-off-by: Jan N. Klug --- .../handler/DateTimeTriggerHandler.java | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/DateTimeTriggerHandler.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/DateTimeTriggerHandler.java index 83110aa601c..756a09c600b 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/DateTimeTriggerHandler.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/DateTimeTriggerHandler.java @@ -30,9 +30,7 @@ import org.openhab.core.events.TopicPrefixEventFilter; import org.openhab.core.items.ItemNotFoundException; import org.openhab.core.items.ItemRegistry; -import org.openhab.core.items.events.ItemCommandEvent; -import org.openhab.core.items.events.ItemEvent; -import org.openhab.core.items.events.ItemStateEvent; +import org.openhab.core.items.events.ItemStateChangedEvent; import org.openhab.core.library.types.DateTimeType; import org.openhab.core.scheduler.CronAdjuster; import org.openhab.core.scheduler.CronScheduler; @@ -115,8 +113,8 @@ public synchronized void setCallback(ModuleHandlerCallback callback) { @Override public void run() { ModuleHandlerCallback callback = this.callback; - if (callback instanceof TriggerHandlerCallback) { - ((TriggerHandlerCallback) callback).triggered(module); + if (callback instanceof TriggerHandlerCallback triggerHandlerCallback) { + triggerHandlerCallback.triggered(module); } else { logger.debug("Tried to trigger, but callback isn't available!"); } @@ -129,7 +127,7 @@ public CronAdjuster getTemporalAdjuster() { @Override public Set getSubscribedEventTypes() { - return Set.of(ItemStateEvent.TYPE, ItemCommandEvent.TYPE); + return Set.of(ItemStateChangedEvent.TYPE); } @Override @@ -139,14 +137,9 @@ public Set getSubscribedEventTypes() { @Override public void receive(Event event) { - if (event instanceof ItemEvent && (((ItemEvent) event).getItemName().equals(itemName))) { - if (event instanceof ItemStateEvent) { - process(((ItemStateEvent) event).getItemState()); - } else if (event instanceof ItemCommandEvent) { - process(((ItemCommandEvent) event).getItemCommand()); - } else { - logger.debug("Don't know how to process event {}, discarding", event); - } + if (event instanceof ItemStateChangedEvent itemStateChangedEvent + && (itemStateChangedEvent.getItemName().equals(itemName))) { + process(itemStateChangedEvent.getItemState()); } } @@ -172,9 +165,9 @@ private void process(Type value) { cancelScheduler(); if (value instanceof UnDefType) { cronExpression = CronAdjuster.REBOOT; - } else if (value instanceof DateTimeType) { - boolean itemIsTimeOnly = ((DateTimeType) value).toString().startsWith("1970-01-01T"); - cronExpression = ((DateTimeType) value).getZonedDateTime().withZoneSameInstant(ZoneId.systemDefault()) + } else if (value instanceof DateTimeType dateTimeType) { + boolean itemIsTimeOnly = dateTimeType.toString().startsWith("1970-01-01T"); + cronExpression = dateTimeType.getZonedDateTime().withZoneSameInstant(ZoneId.systemDefault()) .format(timeOnly || itemIsTimeOnly ? CRON_TIMEONLY_FORMATTER : CRON_FORMATTER); startScheduler(); } else {