From 18d85e68bf9286094f38bb94dbf617cd2c3033e2 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Tue, 10 Dec 2024 08:03:41 +0100 Subject: [PATCH] Simplify DateTimeType handling for Robonect (#17872) Signed-off-by: Jacob Laursen --- .../robonect/internal/handler/RobonectHandler.java | 5 +---- .../internal/handler/RobonectHandlerTest.java | 12 +++++++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/bundles/org.openhab.binding.robonect/src/main/java/org/openhab/binding/robonect/internal/handler/RobonectHandler.java b/bundles/org.openhab.binding.robonect/src/main/java/org/openhab/binding/robonect/internal/handler/RobonectHandler.java index ad6090c9e99..67734ab94dd 100644 --- a/bundles/org.openhab.binding.robonect/src/main/java/org/openhab/binding/robonect/internal/handler/RobonectHandler.java +++ b/bundles/org.openhab.binding.robonect/src/main/java/org/openhab/binding/robonect/internal/handler/RobonectHandler.java @@ -18,7 +18,6 @@ import java.time.DateTimeException; import java.time.Instant; import java.time.ZoneId; import java.time.ZoneOffset; -import java.time.ZonedDateTime; import java.util.List; import java.util.Map; import java.util.concurrent.ScheduledFuture; @@ -304,9 +303,7 @@ public class RobonectHandler extends BaseThingHandler { long adjustedTime = rawInstant.getEpochSecond() - offsetToConfiguredZone.getTotalSeconds(); Instant adjustedInstant = Instant.ofEpochMilli(adjustedTime * 1000); - // we provide the time in the format as configured in the openHAB settings - ZonedDateTime zdt = adjustedInstant.atZone(timeZoneProvider.getTimeZone()); - return new DateTimeType(zdt); + return new DateTimeType(adjustedInstant); } private void refreshVersionInfo() { diff --git a/bundles/org.openhab.binding.robonect/src/test/java/org/openhab/binding/robonect/internal/handler/RobonectHandlerTest.java b/bundles/org.openhab.binding.robonect/src/test/java/org/openhab/binding/robonect/internal/handler/RobonectHandlerTest.java index 359d67f5a69..b5d8337400a 100644 --- a/bundles/org.openhab.binding.robonect/src/test/java/org/openhab/binding/robonect/internal/handler/RobonectHandlerTest.java +++ b/bundles/org.openhab.binding.robonect/src/test/java/org/openhab/binding/robonect/internal/handler/RobonectHandlerTest.java @@ -64,20 +64,22 @@ import org.openhab.core.types.UnDefType; @MockitoSettings(strictness = Strictness.LENIENT) public class RobonectHandlerTest { + private static final ZoneId TIME_ZONE = ZoneId.of("Europe/Berlin"); + private RobonectHandler subject; private @Mock Thing robonectThingMock; private @Mock RobonectClient robonectClientMock; private @Mock ThingHandlerCallback callbackMock; private @Mock HttpClientFactory httpClientFactoryMock; - private @Mock TimeZoneProvider timezoneProvider; + private @Mock TimeZoneProvider timeZoneProvider; @BeforeEach public void setUp() { Mockito.when(robonectThingMock.getUID()).thenReturn(new ThingUID("1:2:3")); - Mockito.when(timezoneProvider.getTimeZone()).thenReturn(ZoneId.of("Europe/Berlin")); + Mockito.when(timeZoneProvider.getTimeZone()).thenReturn(TIME_ZONE); - subject = new RobonectHandler(robonectThingMock, httpClientFactoryMock, timezoneProvider); + subject = new RobonectHandler(robonectThingMock, httpClientFactoryMock, timeZoneProvider); subject.setCallback(callbackMock); subject.setRobonectClient(robonectClientMock); } @@ -110,7 +112,7 @@ public class RobonectHandlerTest { State value = stateCaptor.getValue(); assertTrue(value instanceof DateTimeType); - ZonedDateTime zdt = ((DateTimeType) value).getZonedDateTime(); + ZonedDateTime zdt = ((DateTimeType) value).getZonedDateTime(TIME_ZONE); assertEquals(1, zdt.getDayOfMonth()); assertEquals(2017, zdt.getYear()); assertEquals(Month.MAY, zdt.getMonth()); @@ -159,7 +161,7 @@ public class RobonectHandlerTest { State errorDate = errorDateCaptor.getValue(); assertTrue(errorDate instanceof DateTimeType); - ZonedDateTime zdt = ((DateTimeType) errorDate).getZonedDateTime(); + ZonedDateTime zdt = ((DateTimeType) errorDate).getZonedDateTime(TIME_ZONE); assertEquals(1, zdt.getDayOfMonth()); assertEquals(2017, zdt.getYear()); assertEquals(Month.MAY, zdt.getMonth());