Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DateTimeTrigger not triggering on restored state #3277

Merged
merged 1 commit into from
Jan 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!");
}
Expand All @@ -129,7 +127,7 @@ public CronAdjuster getTemporalAdjuster() {

@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ItemStateEvent.TYPE, ItemCommandEvent.TYPE);
return Set.of(ItemStateChangedEvent.TYPE);
}

@Override
Expand All @@ -139,14 +137,9 @@ public Set<String> 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());
}
}

Expand All @@ -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 {
Expand Down