From c02a7345e1fef85d19850d92ed59e79e5aeae510 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Thu, 28 Nov 2024 22:13:15 +0100 Subject: [PATCH] Simplify DateTimeType handling for Bosch Smart Home Signed-off-by: Jacob Laursen --- .../internal/devices/BoschSHCHandlerFactory.java | 16 ++-------------- .../universalswitch/UniversalSwitch2Handler.java | 5 ++--- .../universalswitch/UniversalSwitchHandler.java | 10 ++-------- .../devices/BoschSHCHandlerFactoryTest.java | 4 +--- .../UniversalSwitchHandler2Test.java | 4 +--- .../UniversalSwitchHandlerTest.java | 7 ++----- 6 files changed, 10 insertions(+), 36 deletions(-) diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandlerFactory.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandlerFactory.java index c16495c5a9b..85fc1225391 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandlerFactory.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandlerFactory.java @@ -68,7 +68,6 @@ import org.openhab.binding.boschshc.internal.devices.wallthermostat.WallThermost import org.openhab.binding.boschshc.internal.devices.waterleakage.WaterLeakageSensorHandler; import org.openhab.binding.boschshc.internal.devices.windowcontact.WindowContact2Handler; import org.openhab.binding.boschshc.internal.devices.windowcontact.WindowContactHandler; -import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingTypeUID; @@ -76,9 +75,7 @@ import org.openhab.core.thing.binding.BaseThingHandler; import org.openhab.core.thing.binding.BaseThingHandlerFactory; import org.openhab.core.thing.binding.ThingHandler; import org.openhab.core.thing.binding.ThingHandlerFactory; -import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.annotations.Reference; /** * The {@link BoschSHCHandlerFactory} is responsible for creating things and @@ -94,13 +91,6 @@ import org.osgi.service.component.annotations.Reference; @Component(configurationPid = "binding.boschshc", service = ThingHandlerFactory.class) public class BoschSHCHandlerFactory extends BaseThingHandlerFactory { - private TimeZoneProvider timeZoneProvider; - - @Activate - public BoschSHCHandlerFactory(final @Reference TimeZoneProvider timeZoneProvider) { - this.timeZoneProvider = timeZoneProvider; - } - private static class ThingTypeHandlerMapping { public ThingTypeUID thingTypeUID; public Function handlerSupplier; @@ -130,10 +120,8 @@ public class BoschSHCHandlerFactory extends BaseThingHandlerFactory { new ThingTypeHandlerMapping(THING_TYPE_SMART_BULB, SmartBulbHandler::new), new ThingTypeHandlerMapping(THING_TYPE_SMOKE_DETECTOR, SmokeDetectorHandler::new), new ThingTypeHandlerMapping(THING_TYPE_USER_DEFINED_STATE, UserStateHandler::new), - new ThingTypeHandlerMapping(THING_TYPE_UNIVERSAL_SWITCH, - thing -> new UniversalSwitchHandler(thing, timeZoneProvider)), - new ThingTypeHandlerMapping(THING_TYPE_UNIVERSAL_SWITCH_2, - thing -> new UniversalSwitch2Handler(thing, timeZoneProvider)), + new ThingTypeHandlerMapping(THING_TYPE_UNIVERSAL_SWITCH, thing -> new UniversalSwitchHandler(thing)), + new ThingTypeHandlerMapping(THING_TYPE_UNIVERSAL_SWITCH_2, thing -> new UniversalSwitch2Handler(thing)), new ThingTypeHandlerMapping(THING_TYPE_SMOKE_DETECTOR_2, SmokeDetector2Handler::new), new ThingTypeHandlerMapping(THING_TYPE_LIGHT_CONTROL_2, LightControl2Handler::new), new ThingTypeHandlerMapping(THING_TYPE_DIMMER, DimmerHandler::new), diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitch2Handler.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitch2Handler.java index 86628d3e337..d9c572a3071 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitch2Handler.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitch2Handler.java @@ -13,7 +13,6 @@ package org.openhab.binding.boschshc.internal.devices.universalswitch; import org.eclipse.jdt.annotation.NonNullByDefault; -import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.thing.Thing; /** @@ -25,7 +24,7 @@ import org.openhab.core.thing.Thing; @NonNullByDefault public class UniversalSwitch2Handler extends UniversalSwitchHandler { - public UniversalSwitch2Handler(Thing thing, TimeZoneProvider timeZoneProvider) { - super(thing, timeZoneProvider); + public UniversalSwitch2Handler(Thing thing) { + super(thing); } } diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitchHandler.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitchHandler.java index fc15f72222a..615daeea442 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitchHandler.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitchHandler.java @@ -18,7 +18,6 @@ import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConst import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_KEY_NAME; import java.time.Instant; -import java.time.ZonedDateTime; import java.util.List; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -26,7 +25,6 @@ import org.openhab.binding.boschshc.internal.devices.AbstractBatteryPoweredDevic import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException; import org.openhab.binding.boschshc.internal.services.keypad.KeypadService; import org.openhab.binding.boschshc.internal.services.keypad.dto.KeypadServiceState; -import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.StringType; @@ -41,11 +39,8 @@ import org.openhab.core.thing.Thing; @NonNullByDefault public class UniversalSwitchHandler extends AbstractBatteryPoweredDeviceHandler { - private TimeZoneProvider timeZoneProvider; - - public UniversalSwitchHandler(Thing thing, TimeZoneProvider timeZoneProvider) { + public UniversalSwitchHandler(Thing thing) { super(thing); - this.timeZoneProvider = timeZoneProvider; } @Override @@ -62,7 +57,6 @@ public class UniversalSwitchHandler extends AbstractBatteryPoweredDeviceHandler updateState(CHANNEL_KEY_EVENT_TYPE, new StringType(keypadServiceState.eventType.toString())); Instant instant = Instant.ofEpochMilli(keypadServiceState.eventTimestamp); - updateState(CHANNEL_KEY_EVENT_TIMESTAMP, - new DateTimeType(ZonedDateTime.ofInstant(instant, timeZoneProvider.getTimeZone()))); + updateState(CHANNEL_KEY_EVENT_TIMESTAMP, new DateTimeType(instant)); } } diff --git a/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandlerFactoryTest.java b/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandlerFactoryTest.java index 6a526a22ced..a52eb104193 100644 --- a/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandlerFactoryTest.java +++ b/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandlerFactoryTest.java @@ -17,8 +17,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.time.ZoneId; - import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -39,7 +37,7 @@ class BoschSHCHandlerFactoryTest { @BeforeEach public void setUp() throws Exception { - fixture = new BoschSHCHandlerFactory(() -> ZoneId.systemDefault()); + fixture = new BoschSHCHandlerFactory(); } @Test diff --git a/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitchHandler2Test.java b/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitchHandler2Test.java index df1bf9d7ad6..e88681e3de9 100644 --- a/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitchHandler2Test.java +++ b/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitchHandler2Test.java @@ -12,8 +12,6 @@ */ package org.openhab.binding.boschshc.internal.devices.universalswitch; -import java.time.ZoneId; - import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants; import org.openhab.core.thing.ThingTypeUID; @@ -29,7 +27,7 @@ class UniversalSwitchHandler2Test extends UniversalSwitchHandlerTest { @Override protected UniversalSwitchHandler createFixture() { - return new UniversalSwitch2Handler(getThing(), () -> ZoneId.systemDefault()); + return new UniversalSwitch2Handler(getThing()); } @Override diff --git a/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitchHandlerTest.java b/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitchHandlerTest.java index 8e37ed93516..2df330b2d6f 100644 --- a/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitchHandlerTest.java +++ b/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/universalswitch/UniversalSwitchHandlerTest.java @@ -15,8 +15,6 @@ package org.openhab.binding.boschshc.internal.devices.universalswitch; import static org.mockito.Mockito.verify; import java.time.Instant; -import java.time.ZoneId; -import java.time.ZonedDateTime; import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.Test; @@ -42,7 +40,7 @@ class UniversalSwitchHandlerTest extends AbstractBatteryPoweredDeviceHandlerTest @Override protected UniversalSwitchHandler createFixture() { - return new UniversalSwitchHandler(getThing(), () -> ZoneId.systemDefault()); + return new UniversalSwitchHandler(getThing()); } @Override @@ -80,8 +78,7 @@ class UniversalSwitchHandlerTest extends AbstractBatteryPoweredDeviceHandlerTest new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_KEY_EVENT_TYPE), new StringType("PRESS_SHORT")); - ZonedDateTime expectedTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1705130891435l), - ZoneId.systemDefault()); + Instant expectedTime = Instant.ofEpochMilli(1705130891435l); verify(getCallback()).stateUpdated( new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_KEY_EVENT_TIMESTAMP), new DateTimeType(expectedTime));