From fdad7a40db6b95ae340a35cef86a9b9ee2de4575 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Tue, 10 Dec 2024 21:03:33 +0100 Subject: [PATCH] Simplify DateTimeType handling for aha Waste Collection Signed-off-by: Jacob Laursen Signed-off-by: Ciprian Pascu --- .../internal/AhaWasteCollectionHandler.java | 10 ++-------- .../internal/AhaWasteCollectionHandlerFactory.java | 9 ++------- .../internal/AhaWasteCollectionHandlerTest.java | 7 ++----- 3 files changed, 6 insertions(+), 20 deletions(-) diff --git a/bundles/org.openhab.binding.ahawastecollection/src/main/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandler.java b/bundles/org.openhab.binding.ahawastecollection/src/main/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandler.java index f4b46208cb8..d40aab4c854 100644 --- a/bundles/org.openhab.binding.ahawastecollection/src/main/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandler.java +++ b/bundles/org.openhab.binding.ahawastecollection/src/main/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandler.java @@ -14,7 +14,6 @@ package org.openhab.binding.ahawastecollection.internal; import java.io.IOException; import java.time.Duration; -import java.time.ZonedDateTime; import java.util.Collections; import java.util.Date; import java.util.Map; @@ -26,7 +25,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.ahawastecollection.internal.CollectionDate.WasteType; import org.openhab.core.cache.ExpiringCache; -import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.library.types.DateTimeType; import org.openhab.core.scheduler.CronScheduler; import org.openhab.core.scheduler.ScheduledCompletableFuture; @@ -57,7 +55,6 @@ public class AhaWasteCollectionHandler extends BaseThingHandler { private final Lock monitor = new ReentrantLock(); private final ExpiringCache> cache; - private final TimeZoneProvider timeZoneProvider; private final Logger logger = LoggerFactory.getLogger(AhaWasteCollectionHandler.class); private @Nullable AhaCollectionSchedule collectionSchedule; @@ -69,11 +66,10 @@ public class AhaWasteCollectionHandler extends BaseThingHandler { private final ScheduledExecutorService executorService; public AhaWasteCollectionHandler(final Thing thing, final CronScheduler scheduler, - final TimeZoneProvider timeZoneProvider, final AhaCollectionScheduleFactory scheduleFactory, + final AhaCollectionScheduleFactory scheduleFactory, @Nullable final ScheduledExecutorService executorService) { super(thing); this.cronScheduler = scheduler; - this.timeZoneProvider = timeZoneProvider; this.scheduleFactory = scheduleFactory; this.cache = new ExpiringCache<>(Duration.ofMinutes(5), this::loadCollectionDates); this.executorService = executorService == null ? this.scheduler : executorService; @@ -190,9 +186,7 @@ public class AhaWasteCollectionHandler extends BaseThingHandler { final Date nextCollectionDate = collectionDate.getDates().get(0); - final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(nextCollectionDate.toInstant(), - this.timeZoneProvider.getTimeZone()); - this.updateState(channel.getUID(), new DateTimeType(zonedDateTime)); + this.updateState(channel.getUID(), new DateTimeType(nextCollectionDate.toInstant())); } } diff --git a/bundles/org.openhab.binding.ahawastecollection/src/main/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandlerFactory.java b/bundles/org.openhab.binding.ahawastecollection/src/main/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandlerFactory.java index 5ad4febf969..62fb52a0d8c 100644 --- a/bundles/org.openhab.binding.ahawastecollection/src/main/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandlerFactory.java +++ b/bundles/org.openhab.binding.ahawastecollection/src/main/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandlerFactory.java @@ -18,7 +18,6 @@ import java.util.Set; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.scheduler.CronScheduler; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingTypeUID; @@ -40,7 +39,6 @@ public class AhaWasteCollectionHandlerFactory extends BaseThingHandlerFactory { private static final Set SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_SCHEDULE); private final CronScheduler scheduler; - private final TimeZoneProvider timeZoneProvider; @Override public boolean supportsThingType(final ThingTypeUID thingTypeUID) { @@ -48,10 +46,8 @@ public class AhaWasteCollectionHandlerFactory extends BaseThingHandlerFactory { } @Activate - public AhaWasteCollectionHandlerFactory(final @Reference CronScheduler scheduler, - final @Reference TimeZoneProvider timeZoneProvider) { + public AhaWasteCollectionHandlerFactory(final @Reference CronScheduler scheduler) { this.scheduler = scheduler; - this.timeZoneProvider = timeZoneProvider; } @Override @@ -59,8 +55,7 @@ public class AhaWasteCollectionHandlerFactory extends BaseThingHandlerFactory { final ThingTypeUID thingTypeUID = thing.getThingTypeUID(); if (THING_TYPE_SCHEDULE.equals(thingTypeUID)) { - return new AhaWasteCollectionHandler(thing, this.scheduler, this.timeZoneProvider, - AhaCollectionScheduleImpl::new, null); + return new AhaWasteCollectionHandler(thing, this.scheduler, AhaCollectionScheduleImpl::new, null); } return null; } diff --git a/bundles/org.openhab.binding.ahawastecollection/src/test/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandlerTest.java b/bundles/org.openhab.binding.ahawastecollection/src/test/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandlerTest.java index 2ae23d2b409..f23069cdde5 100644 --- a/bundles/org.openhab.binding.ahawastecollection/src/test/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandlerTest.java +++ b/bundles/org.openhab.binding.ahawastecollection/src/test/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandlerTest.java @@ -15,8 +15,6 @@ package org.openhab.binding.ahawastecollection.internal; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; -import java.time.ZoneId; -import java.time.ZonedDateTime; import java.util.Arrays; import java.util.Date; import java.util.Map; @@ -136,15 +134,14 @@ public class AhaWasteCollectionHandlerTest { }).when(executorStub).execute(any(Runnable.class)); final AhaWasteCollectionHandler handler = new AhaWasteCollectionHandler(thing, createStubScheduler(), - ZoneId::systemDefault, new AhaCollectionScheduleStubFactory(), executorStub); + new AhaCollectionScheduleStubFactory(), executorStub); handler.setCallback(callback); handler.initialize(); return handler; } private static State getDateTime(final Date day) { - final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(day.toInstant(), ZoneId.systemDefault()); - return new DateTimeType(zonedDateTime); + return new DateTimeType(day.toInstant()); } @Test