Simplify DateTimeType handling for Nest

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-11-16 12:48:43 +01:00 committed by lsiepel
parent d871b66d3c
commit e60fc3e40c
4 changed files with 16 additions and 26 deletions

View File

@ -20,7 +20,6 @@ import org.openhab.binding.nest.internal.sdm.handler.SDMAccountHandler;
import org.openhab.binding.nest.internal.sdm.handler.SDMCameraHandler; import org.openhab.binding.nest.internal.sdm.handler.SDMCameraHandler;
import org.openhab.binding.nest.internal.sdm.handler.SDMThermostatHandler; import org.openhab.binding.nest.internal.sdm.handler.SDMThermostatHandler;
import org.openhab.core.auth.client.oauth2.OAuthFactory; import org.openhab.core.auth.client.oauth2.OAuthFactory;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.io.net.http.HttpClientFactory; import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
@ -44,14 +43,12 @@ public class SDMThingHandlerFactory extends BaseThingHandlerFactory {
private HttpClientFactory httpClientFactory; private HttpClientFactory httpClientFactory;
private OAuthFactory oAuthFactory; private OAuthFactory oAuthFactory;
private final TimeZoneProvider timeZoneProvider;
@Activate @Activate
public SDMThingHandlerFactory(final @Reference HttpClientFactory httpClientFactory, public SDMThingHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
final @Reference OAuthFactory oAuthFactory, final @Reference TimeZoneProvider timeZoneProvider) { final @Reference OAuthFactory oAuthFactory) {
this.httpClientFactory = httpClientFactory; this.httpClientFactory = httpClientFactory;
this.oAuthFactory = oAuthFactory; this.oAuthFactory = oAuthFactory;
this.timeZoneProvider = timeZoneProvider;
} }
@Override @Override
@ -66,13 +63,13 @@ public class SDMThingHandlerFactory extends BaseThingHandlerFactory {
if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) { if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) {
return new SDMAccountHandler((Bridge) thing, httpClientFactory, oAuthFactory); return new SDMAccountHandler((Bridge) thing, httpClientFactory, oAuthFactory);
} else if (thingTypeUID.equals(THING_TYPE_CAMERA)) { } else if (thingTypeUID.equals(THING_TYPE_CAMERA)) {
return new SDMCameraHandler(thing, timeZoneProvider); return new SDMCameraHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_DISPLAY)) { } else if (thingTypeUID.equals(THING_TYPE_DISPLAY)) {
return new SDMCameraHandler(thing, timeZoneProvider); return new SDMCameraHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_DOORBELL)) { } else if (thingTypeUID.equals(THING_TYPE_DOORBELL)) {
return new SDMCameraHandler(thing, timeZoneProvider); return new SDMCameraHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_THERMOSTAT)) { } else if (thingTypeUID.equals(THING_TYPE_THERMOSTAT)) {
return new SDMThermostatHandler(thing, timeZoneProvider); return new SDMThermostatHandler(thing);
} }
return null; return null;

View File

@ -46,7 +46,6 @@ import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMResolution;
import org.openhab.binding.nest.internal.sdm.exception.FailedSendingSDMDataException; import org.openhab.binding.nest.internal.sdm.exception.FailedSendingSDMDataException;
import org.openhab.binding.nest.internal.sdm.exception.InvalidSDMAccessTokenException; import org.openhab.binding.nest.internal.sdm.exception.InvalidSDMAccessTokenException;
import org.openhab.binding.nest.internal.sdm.listener.SDMEventListener; import org.openhab.binding.nest.internal.sdm.listener.SDMEventListener;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
@ -75,11 +74,9 @@ public abstract class SDMBaseHandler extends BaseThingHandler implements SDMIden
protected String deviceId = ""; protected String deviceId = "";
protected @Nullable ZonedDateTime lastRefreshDateTime; protected @Nullable ZonedDateTime lastRefreshDateTime;
protected @Nullable ScheduledFuture<?> refreshJob; protected @Nullable ScheduledFuture<?> refreshJob;
protected final TimeZoneProvider timeZoneProvider;
public SDMBaseHandler(Thing thing, TimeZoneProvider timeZoneProvider) { public SDMBaseHandler(Thing thing) {
super(thing); super(thing);
this.timeZoneProvider = timeZoneProvider;
} }
@Override @Override

View File

@ -37,7 +37,6 @@ import org.openhab.binding.nest.internal.sdm.dto.SDMEvent.SDMResourceUpdateEvent
import org.openhab.binding.nest.internal.sdm.exception.FailedSendingSDMDataException; import org.openhab.binding.nest.internal.sdm.exception.FailedSendingSDMDataException;
import org.openhab.binding.nest.internal.sdm.exception.InvalidSDMAccessTokenException; import org.openhab.binding.nest.internal.sdm.exception.InvalidSDMAccessTokenException;
import org.openhab.core.config.core.Configuration; import org.openhab.core.config.core.Configuration;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.RawType; import org.openhab.core.library.types.RawType;
import org.openhab.core.library.types.StringType; import org.openhab.core.library.types.StringType;
@ -64,8 +63,8 @@ public class SDMCameraHandler extends SDMBaseHandler {
private @Nullable ZonedDateTime lastPersonEventTimestamp; private @Nullable ZonedDateTime lastPersonEventTimestamp;
private @Nullable ZonedDateTime lastSoundEventTimestamp; private @Nullable ZonedDateTime lastSoundEventTimestamp;
public SDMCameraHandler(Thing thing, TimeZoneProvider timeZoneProvider) { public SDMCameraHandler(Thing thing) {
super(thing, timeZoneProvider); super(thing);
} }
private void updateLiveStreamChannels() throws FailedSendingSDMDataException, InvalidSDMAccessTokenException { private void updateLiveStreamChannels() throws FailedSendingSDMDataException, InvalidSDMAccessTokenException {
@ -87,8 +86,7 @@ public class SDMCameraHandler extends SDMBaseHandler {
SDMGenerateCameraRtspStreamResults results = response.results; SDMGenerateCameraRtspStreamResults results = response.results;
if (results != null) { if (results != null) {
updateState(CHANNEL_LIVE_STREAM_CURRENT_TOKEN, new StringType(results.streamToken)); updateState(CHANNEL_LIVE_STREAM_CURRENT_TOKEN, new StringType(results.streamToken));
updateState(CHANNEL_LIVE_STREAM_EXPIRATION_TIMESTAMP, updateState(CHANNEL_LIVE_STREAM_EXPIRATION_TIMESTAMP, new DateTimeType(results.expiresAt));
new DateTimeType(results.expiresAt.withZoneSameInstant(timeZoneProvider.getTimeZone())));
updateState(CHANNEL_LIVE_STREAM_EXTENSION_TOKEN, new StringType(results.streamExtensionToken)); updateState(CHANNEL_LIVE_STREAM_EXTENSION_TOKEN, new StringType(results.streamExtensionToken));
updateState(CHANNEL_LIVE_STREAM_URL, new StringType(results.streamUrls.rtspUrl)); updateState(CHANNEL_LIVE_STREAM_URL, new StringType(results.streamUrls.rtspUrl));
} }
@ -167,8 +165,7 @@ public class SDMCameraHandler extends SDMBaseHandler {
updateState(imageChannelName, getCameraImage(event.eventId, imageWidth, imageHeight)); updateState(imageChannelName, getCameraImage(event.eventId, imageWidth, imageHeight));
} }
updateState(timeChannelName, updateState(timeChannelName, new DateTimeType(eventTimestamp));
new DateTimeType(eventTimestamp.withZoneSameInstant(timeZoneProvider.getTimeZone())));
logger.debug("Updated {} channel and {} with image of event at {}", imageChannelName, timeChannelName, logger.debug("Updated {} channel and {} with image of event at {}", imageChannelName, timeChannelName,
eventTimestamp); eventTimestamp);

View File

@ -19,7 +19,7 @@ import static org.openhab.core.library.unit.Units.PERCENT;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.Duration; import java.time.Duration;
import java.time.ZonedDateTime; import java.time.Instant;
import javax.measure.Unit; import javax.measure.Unit;
import javax.measure.quantity.Temperature; import javax.measure.quantity.Temperature;
@ -49,7 +49,6 @@ import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMThermostatTemperat
import org.openhab.binding.nest.internal.sdm.exception.FailedSendingSDMDataException; import org.openhab.binding.nest.internal.sdm.exception.FailedSendingSDMDataException;
import org.openhab.binding.nest.internal.sdm.exception.InvalidSDMAccessTokenException; import org.openhab.binding.nest.internal.sdm.exception.InvalidSDMAccessTokenException;
import org.openhab.core.config.core.Configuration; import org.openhab.core.config.core.Configuration;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.QuantityType;
@ -75,8 +74,8 @@ public class SDMThermostatHandler extends SDMBaseHandler {
private final Logger logger = LoggerFactory.getLogger(SDMThermostatHandler.class); private final Logger logger = LoggerFactory.getLogger(SDMThermostatHandler.class);
public SDMThermostatHandler(Thing thing, TimeZoneProvider timeZoneProvider) { public SDMThermostatHandler(Thing thing) {
super(thing, timeZoneProvider); super(thing);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -108,7 +107,7 @@ public class SDMThermostatHandler extends SDMBaseHandler {
} }
} else if (CHANNEL_FAN_TIMER_TIMEOUT.equals(channelUID.getId())) { } else if (CHANNEL_FAN_TIMER_TIMEOUT.equals(channelUID.getId())) {
if (command instanceof DateTimeType dateTimeCommand) { if (command instanceof DateTimeType dateTimeCommand) {
Duration duration = Duration.between(ZonedDateTime.now(), dateTimeCommand.getZonedDateTime()); Duration duration = Duration.between(Instant.now(), dateTimeCommand.getInstant());
executeDeviceCommand(new SDMSetFanTimerRequest(SDMFanTimerMode.ON, duration)); executeDeviceCommand(new SDMSetFanTimerRequest(SDMFanTimerMode.ON, duration));
delayedRefresh(); delayedRefresh();
} }
@ -169,8 +168,8 @@ public class SDMThermostatHandler extends SDMBaseHandler {
SDMFanTrait fan = traits.fan; SDMFanTrait fan = traits.fan;
if (fan != null) { if (fan != null) {
updateState(CHANNEL_FAN_TIMER_MODE, OnOffType.from(fan.timerMode == SDMFanTimerMode.ON)); updateState(CHANNEL_FAN_TIMER_MODE, OnOffType.from(fan.timerMode == SDMFanTimerMode.ON));
updateState(CHANNEL_FAN_TIMER_TIMEOUT, fan.timerTimeout == null ? UnDefType.NULL updateState(CHANNEL_FAN_TIMER_TIMEOUT,
: new DateTimeType(fan.timerTimeout.withZoneSameInstant(timeZoneProvider.getTimeZone()))); fan.timerTimeout == null ? UnDefType.NULL : new DateTimeType(fan.timerTimeout));
} }
SDMThermostatHvacTrait thermostatHvac = traits.thermostatHvac; SDMThermostatHvacTrait thermostatHvac = traits.thermostatHvac;