Simplify DateTimeType handling for Bosch Smart Home

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-11-28 22:13:15 +01:00 committed by lsiepel
parent 7feafba285
commit c02a7345e1
6 changed files with 10 additions and 36 deletions

View File

@ -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<Thing, BaseThingHandler> 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),

View File

@ -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);
}
}

View File

@ -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));
}
}

View File

@ -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

View File

@ -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

View File

@ -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));