Simplify DateTimeType handling for aha Waste Collection

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Jacob Laursen 2024-12-10 21:03:33 +01:00 committed by Ciprian Pascu
parent 838b97ff8f
commit fdad7a40db
3 changed files with 6 additions and 20 deletions

View File

@ -14,7 +14,6 @@ package org.openhab.binding.ahawastecollection.internal;
import java.io.IOException; import java.io.IOException;
import java.time.Duration; import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
@ -26,7 +25,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.ahawastecollection.internal.CollectionDate.WasteType; import org.openhab.binding.ahawastecollection.internal.CollectionDate.WasteType;
import org.openhab.core.cache.ExpiringCache; import org.openhab.core.cache.ExpiringCache;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.scheduler.CronScheduler; import org.openhab.core.scheduler.CronScheduler;
import org.openhab.core.scheduler.ScheduledCompletableFuture; import org.openhab.core.scheduler.ScheduledCompletableFuture;
@ -57,7 +55,6 @@ public class AhaWasteCollectionHandler extends BaseThingHandler {
private final Lock monitor = new ReentrantLock(); private final Lock monitor = new ReentrantLock();
private final ExpiringCache<Map<WasteType, CollectionDate>> cache; private final ExpiringCache<Map<WasteType, CollectionDate>> cache;
private final TimeZoneProvider timeZoneProvider;
private final Logger logger = LoggerFactory.getLogger(AhaWasteCollectionHandler.class); private final Logger logger = LoggerFactory.getLogger(AhaWasteCollectionHandler.class);
private @Nullable AhaCollectionSchedule collectionSchedule; private @Nullable AhaCollectionSchedule collectionSchedule;
@ -69,11 +66,10 @@ public class AhaWasteCollectionHandler extends BaseThingHandler {
private final ScheduledExecutorService executorService; private final ScheduledExecutorService executorService;
public AhaWasteCollectionHandler(final Thing thing, final CronScheduler scheduler, public AhaWasteCollectionHandler(final Thing thing, final CronScheduler scheduler,
final TimeZoneProvider timeZoneProvider, final AhaCollectionScheduleFactory scheduleFactory, final AhaCollectionScheduleFactory scheduleFactory,
@Nullable final ScheduledExecutorService executorService) { @Nullable final ScheduledExecutorService executorService) {
super(thing); super(thing);
this.cronScheduler = scheduler; this.cronScheduler = scheduler;
this.timeZoneProvider = timeZoneProvider;
this.scheduleFactory = scheduleFactory; this.scheduleFactory = scheduleFactory;
this.cache = new ExpiringCache<>(Duration.ofMinutes(5), this::loadCollectionDates); this.cache = new ExpiringCache<>(Duration.ofMinutes(5), this::loadCollectionDates);
this.executorService = executorService == null ? this.scheduler : executorService; this.executorService = executorService == null ? this.scheduler : executorService;
@ -190,9 +186,7 @@ public class AhaWasteCollectionHandler extends BaseThingHandler {
final Date nextCollectionDate = collectionDate.getDates().get(0); final Date nextCollectionDate = collectionDate.getDates().get(0);
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(nextCollectionDate.toInstant(), this.updateState(channel.getUID(), new DateTimeType(nextCollectionDate.toInstant()));
this.timeZoneProvider.getTimeZone());
this.updateState(channel.getUID(), new DateTimeType(zonedDateTime));
} }
} }

View File

@ -18,7 +18,6 @@ import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.scheduler.CronScheduler; import org.openhab.core.scheduler.CronScheduler;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.ThingTypeUID;
@ -40,7 +39,6 @@ public class AhaWasteCollectionHandlerFactory extends BaseThingHandlerFactory {
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_SCHEDULE); private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_SCHEDULE);
private final CronScheduler scheduler; private final CronScheduler scheduler;
private final TimeZoneProvider timeZoneProvider;
@Override @Override
public boolean supportsThingType(final ThingTypeUID thingTypeUID) { public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
@ -48,10 +46,8 @@ public class AhaWasteCollectionHandlerFactory extends BaseThingHandlerFactory {
} }
@Activate @Activate
public AhaWasteCollectionHandlerFactory(final @Reference CronScheduler scheduler, public AhaWasteCollectionHandlerFactory(final @Reference CronScheduler scheduler) {
final @Reference TimeZoneProvider timeZoneProvider) {
this.scheduler = scheduler; this.scheduler = scheduler;
this.timeZoneProvider = timeZoneProvider;
} }
@Override @Override
@ -59,8 +55,7 @@ public class AhaWasteCollectionHandlerFactory extends BaseThingHandlerFactory {
final ThingTypeUID thingTypeUID = thing.getThingTypeUID(); final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (THING_TYPE_SCHEDULE.equals(thingTypeUID)) { if (THING_TYPE_SCHEDULE.equals(thingTypeUID)) {
return new AhaWasteCollectionHandler(thing, this.scheduler, this.timeZoneProvider, return new AhaWasteCollectionHandler(thing, this.scheduler, AhaCollectionScheduleImpl::new, null);
AhaCollectionScheduleImpl::new, null);
} }
return null; return null;
} }

View File

@ -15,8 +15,6 @@ package org.openhab.binding.ahawastecollection.internal;
import static org.mockito.ArgumentMatchers.*; import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
@ -136,15 +134,14 @@ public class AhaWasteCollectionHandlerTest {
}).when(executorStub).execute(any(Runnable.class)); }).when(executorStub).execute(any(Runnable.class));
final AhaWasteCollectionHandler handler = new AhaWasteCollectionHandler(thing, createStubScheduler(), final AhaWasteCollectionHandler handler = new AhaWasteCollectionHandler(thing, createStubScheduler(),
ZoneId::systemDefault, new AhaCollectionScheduleStubFactory(), executorStub); new AhaCollectionScheduleStubFactory(), executorStub);
handler.setCallback(callback); handler.setCallback(callback);
handler.initialize(); handler.initialize();
return handler; return handler;
} }
private static State getDateTime(final Date day) { private static State getDateTime(final Date day) {
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(day.toInstant(), ZoneId.systemDefault()); return new DateTimeType(day.toInstant());
return new DateTimeType(zonedDateTime);
} }
@Test @Test