Simplify DateTimeType handling for Nest

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-11-16 12:48:43 +01:00 committed by Ciprian Pascu
parent 9413faa922
commit fd2c06715d
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.SDMThermostatHandler;
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.thing.Bridge;
import org.openhab.core.thing.Thing;
@ -44,14 +43,12 @@ public class SDMThingHandlerFactory extends BaseThingHandlerFactory {
private HttpClientFactory httpClientFactory;
private OAuthFactory oAuthFactory;
private final TimeZoneProvider timeZoneProvider;
@Activate
public SDMThingHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
final @Reference OAuthFactory oAuthFactory, final @Reference TimeZoneProvider timeZoneProvider) {
final @Reference OAuthFactory oAuthFactory) {
this.httpClientFactory = httpClientFactory;
this.oAuthFactory = oAuthFactory;
this.timeZoneProvider = timeZoneProvider;
}
@Override
@ -66,13 +63,13 @@ public class SDMThingHandlerFactory extends BaseThingHandlerFactory {
if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) {
return new SDMAccountHandler((Bridge) thing, httpClientFactory, oAuthFactory);
} else if (thingTypeUID.equals(THING_TYPE_CAMERA)) {
return new SDMCameraHandler(thing, timeZoneProvider);
return new SDMCameraHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_DISPLAY)) {
return new SDMCameraHandler(thing, timeZoneProvider);
return new SDMCameraHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_DOORBELL)) {
return new SDMCameraHandler(thing, timeZoneProvider);
return new SDMCameraHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_THERMOSTAT)) {
return new SDMThermostatHandler(thing, timeZoneProvider);
return new SDMThermostatHandler(thing);
}
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.InvalidSDMAccessTokenException;
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.ChannelUID;
import org.openhab.core.thing.Thing;
@ -75,11 +74,9 @@ public abstract class SDMBaseHandler extends BaseThingHandler implements SDMIden
protected String deviceId = "";
protected @Nullable ZonedDateTime lastRefreshDateTime;
protected @Nullable ScheduledFuture<?> refreshJob;
protected final TimeZoneProvider timeZoneProvider;
public SDMBaseHandler(Thing thing, TimeZoneProvider timeZoneProvider) {
public SDMBaseHandler(Thing thing) {
super(thing);
this.timeZoneProvider = timeZoneProvider;
}
@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.InvalidSDMAccessTokenException;
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.RawType;
import org.openhab.core.library.types.StringType;
@ -64,8 +63,8 @@ public class SDMCameraHandler extends SDMBaseHandler {
private @Nullable ZonedDateTime lastPersonEventTimestamp;
private @Nullable ZonedDateTime lastSoundEventTimestamp;
public SDMCameraHandler(Thing thing, TimeZoneProvider timeZoneProvider) {
super(thing, timeZoneProvider);
public SDMCameraHandler(Thing thing) {
super(thing);
}
private void updateLiveStreamChannels() throws FailedSendingSDMDataException, InvalidSDMAccessTokenException {
@ -87,8 +86,7 @@ public class SDMCameraHandler extends SDMBaseHandler {
SDMGenerateCameraRtspStreamResults results = response.results;
if (results != null) {
updateState(CHANNEL_LIVE_STREAM_CURRENT_TOKEN, new StringType(results.streamToken));
updateState(CHANNEL_LIVE_STREAM_EXPIRATION_TIMESTAMP,
new DateTimeType(results.expiresAt.withZoneSameInstant(timeZoneProvider.getTimeZone())));
updateState(CHANNEL_LIVE_STREAM_EXPIRATION_TIMESTAMP, new DateTimeType(results.expiresAt));
updateState(CHANNEL_LIVE_STREAM_EXTENSION_TOKEN, new StringType(results.streamExtensionToken));
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(timeChannelName,
new DateTimeType(eventTimestamp.withZoneSameInstant(timeZoneProvider.getTimeZone())));
updateState(timeChannelName, new DateTimeType(eventTimestamp));
logger.debug("Updated {} channel and {} with image of event at {}", imageChannelName, timeChannelName,
eventTimestamp);

View File

@ -19,7 +19,7 @@ import static org.openhab.core.library.unit.Units.PERCENT;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.time.Instant;
import javax.measure.Unit;
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.InvalidSDMAccessTokenException;
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.OnOffType;
import org.openhab.core.library.types.QuantityType;
@ -75,8 +74,8 @@ public class SDMThermostatHandler extends SDMBaseHandler {
private final Logger logger = LoggerFactory.getLogger(SDMThermostatHandler.class);
public SDMThermostatHandler(Thing thing, TimeZoneProvider timeZoneProvider) {
super(thing, timeZoneProvider);
public SDMThermostatHandler(Thing thing) {
super(thing);
}
@SuppressWarnings("unchecked")
@ -108,7 +107,7 @@ public class SDMThermostatHandler extends SDMBaseHandler {
}
} else if (CHANNEL_FAN_TIMER_TIMEOUT.equals(channelUID.getId())) {
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));
delayedRefresh();
}
@ -169,8 +168,8 @@ public class SDMThermostatHandler extends SDMBaseHandler {
SDMFanTrait fan = traits.fan;
if (fan != null) {
updateState(CHANNEL_FAN_TIMER_MODE, OnOffType.from(fan.timerMode == SDMFanTimerMode.ON));
updateState(CHANNEL_FAN_TIMER_TIMEOUT, fan.timerTimeout == null ? UnDefType.NULL
: new DateTimeType(fan.timerTimeout.withZoneSameInstant(timeZoneProvider.getTimeZone())));
updateState(CHANNEL_FAN_TIMER_TIMEOUT,
fan.timerTimeout == null ? UnDefType.NULL : new DateTimeType(fan.timerTimeout));
}
SDMThermostatHvacTrait thermostatHvac = traits.thermostatHvac;