From d9235da5db545f9a57743ab968d17b345039268c Mon Sep 17 00:00:00 2001 From: lsiepel Date: Sun, 27 Aug 2023 16:11:33 +0200 Subject: [PATCH] [bticinosmarter] Fix activationdate parsing (#15474) * Fix DateTime format * Switch to TimeZoneProvider Signed-off-by: lsiepel --- .../internal/api/dto/Chronothermostat.java | 11 ++++++++--- .../internal/factory/SmartherHandlerFactory.java | 8 ++++++-- .../internal/handler/SmartherModuleHandler.java | 10 +++++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.binding.bticinosmarther/src/main/java/org/openhab/binding/bticinosmarther/internal/api/dto/Chronothermostat.java b/bundles/org.openhab.binding.bticinosmarther/src/main/java/org/openhab/binding/bticinosmarther/internal/api/dto/Chronothermostat.java index 6520ea31c7b..266570e67e6 100644 --- a/bundles/org.openhab.binding.bticinosmarther/src/main/java/org/openhab/binding/bticinosmarther/internal/api/dto/Chronothermostat.java +++ b/bundles/org.openhab.binding.bticinosmarther/src/main/java/org/openhab/binding/bticinosmarther/internal/api/dto/Chronothermostat.java @@ -24,6 +24,7 @@ import org.openhab.binding.bticinosmarther.internal.api.dto.Enums.LoadState; import org.openhab.binding.bticinosmarther.internal.api.dto.Enums.MeasureUnit; import org.openhab.binding.bticinosmarther.internal.api.exception.SmartherIllegalPropertyValueException; import org.openhab.binding.bticinosmarther.internal.util.DateUtil; +import org.openhab.core.i18n.TimeZoneProvider; import com.google.gson.annotations.SerializedName; @@ -146,12 +147,16 @@ public class Chronothermostat { * @return a string containing the module operational activation time label, or {@code null} if the activation time * cannot be parsed to a valid date/time */ - public @Nullable String getActivationTimeLabel() { + public @Nullable String getActivationTimeLabel(TimeZoneProvider timeZoneProvider) { String timeLabel = TIME_FOREVER; if (activationTime != null) { try { - final ZonedDateTime dateActivationTime = DateUtil.parseZonedTime(activationTime, DTF_DATETIME_EXT); - final ZonedDateTime dateTomorrow = DateUtil.getZonedStartOfDay(1, dateActivationTime.getZone()); + boolean dateActivationTimeIsZoned = activationTime.length() > 19; + + final ZonedDateTime dateActivationTime = DateUtil.parseZonedTime(activationTime, + dateActivationTimeIsZoned ? DTF_DATETIME_EXT : DTF_DATETIME); + final ZonedDateTime dateTomorrow = DateUtil.getZonedStartOfDay(1, + dateActivationTimeIsZoned ? dateActivationTime.getZone() : timeZoneProvider.getTimeZone()); if (dateActivationTime.isBefore(dateTomorrow)) { timeLabel = DateUtil.format(dateActivationTime, DTF_TODAY); diff --git a/bundles/org.openhab.binding.bticinosmarther/src/main/java/org/openhab/binding/bticinosmarther/internal/factory/SmartherHandlerFactory.java b/bundles/org.openhab.binding.bticinosmarther/src/main/java/org/openhab/binding/bticinosmarther/internal/factory/SmartherHandlerFactory.java index 636e0d318da..ab88c78fd34 100644 --- a/bundles/org.openhab.binding.bticinosmarther/src/main/java/org/openhab/binding/bticinosmarther/internal/factory/SmartherHandlerFactory.java +++ b/bundles/org.openhab.binding.bticinosmarther/src/main/java/org/openhab/binding/bticinosmarther/internal/factory/SmartherHandlerFactory.java @@ -21,6 +21,7 @@ import org.openhab.binding.bticinosmarther.internal.handler.SmartherBridgeHandle import org.openhab.binding.bticinosmarther.internal.handler.SmartherDynamicStateDescriptionProvider; import org.openhab.binding.bticinosmarther.internal.handler.SmartherModuleHandler; 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.scheduler.CronScheduler; import org.openhab.core.thing.Bridge; @@ -51,16 +52,19 @@ public class SmartherHandlerFactory extends BaseThingHandlerFactory { private final HttpClient httpClient; private final CronScheduler cronScheduler; private final SmartherDynamicStateDescriptionProvider dynamicStateDescriptionProvider; + private final TimeZoneProvider timeZoneProvider; @Activate public SmartherHandlerFactory(@Reference OAuthFactory oAuthFactory, @Reference SmartherAccountService authService, @Reference HttpClientFactory httpClientFactory, @Reference CronScheduler cronScheduler, - @Reference SmartherDynamicStateDescriptionProvider dynamicStateDescriptionProvider) { + @Reference SmartherDynamicStateDescriptionProvider dynamicStateDescriptionProvider, + final @Reference TimeZoneProvider timeZoneProvider) { this.oAuthFactory = oAuthFactory; this.authService = authService; this.httpClient = httpClientFactory.getCommonHttpClient(); this.cronScheduler = cronScheduler; this.dynamicStateDescriptionProvider = dynamicStateDescriptionProvider; + this.timeZoneProvider = timeZoneProvider; } @Override @@ -77,7 +81,7 @@ public class SmartherHandlerFactory extends BaseThingHandlerFactory { this.authService.addSmartherAccountHandler(handler); return handler; } else if (SmartherBindingConstants.THING_TYPE_MODULE.equals(thingTypeUID)) { - return new SmartherModuleHandler(thing, cronScheduler, dynamicStateDescriptionProvider); + return new SmartherModuleHandler(thing, cronScheduler, dynamicStateDescriptionProvider, timeZoneProvider); } else { logger.debug("Unsupported thing {}", thing.getThingTypeUID()); return null; diff --git a/bundles/org.openhab.binding.bticinosmarther/src/main/java/org/openhab/binding/bticinosmarther/internal/handler/SmartherModuleHandler.java b/bundles/org.openhab.binding.bticinosmarther/src/main/java/org/openhab/binding/bticinosmarther/internal/handler/SmartherModuleHandler.java index 48da20713fc..0ae5cd4c0c0 100644 --- a/bundles/org.openhab.binding.bticinosmarther/src/main/java/org/openhab/binding/bticinosmarther/internal/handler/SmartherModuleHandler.java +++ b/bundles/org.openhab.binding.bticinosmarther/src/main/java/org/openhab/binding/bticinosmarther/internal/handler/SmartherModuleHandler.java @@ -34,6 +34,7 @@ import org.openhab.binding.bticinosmarther.internal.config.SmartherModuleConfigu import org.openhab.binding.bticinosmarther.internal.model.ModuleSettings; import org.openhab.binding.bticinosmarther.internal.util.StringUtil; import org.openhab.core.cache.ExpiringCache; +import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.QuantityType; @@ -74,6 +75,7 @@ public class SmartherModuleHandler extends BaseThingHandler { private final SmartherDynamicStateDescriptionProvider dynamicStateDescriptionProvider; private final ChannelUID programChannelUID; private final ChannelUID endDateChannelUID; + private final TimeZoneProvider timeZoneProvider; // Module configuration private SmartherModuleConfiguration config; @@ -98,11 +100,12 @@ public class SmartherModuleHandler extends BaseThingHandler { * @param provider * the {@link SmartherDynamicStateDescriptionProvider} dynamic state description provider to be used */ - public SmartherModuleHandler(Thing thing, CronScheduler scheduler, - SmartherDynamicStateDescriptionProvider provider) { + public SmartherModuleHandler(Thing thing, CronScheduler scheduler, SmartherDynamicStateDescriptionProvider provider, + final TimeZoneProvider timeZoneProvider) { super(thing); this.cronScheduler = scheduler; this.dynamicStateDescriptionProvider = provider; + this.timeZoneProvider = timeZoneProvider; this.programChannelUID = new ChannelUID(thing.getUID(), CHANNEL_SETTINGS_PROGRAM); this.endDateChannelUID = new ChannelUID(thing.getUID(), CHANNEL_SETTINGS_ENDDATE); this.config = new SmartherModuleConfiguration(); @@ -710,7 +713,8 @@ public class SmartherModuleHandler extends BaseThingHandler { updateChannelState(CHANNEL_STATUS_MODE, new StringType(StringUtil.capitalize(localChrono.getMode().toLowerCase()))); updateChannelState(CHANNEL_STATUS_TEMPERATURE, localChrono.getSetPointTemperature().toState()); - updateChannelState(CHANNEL_STATUS_ENDTIME, new StringType(localChrono.getActivationTimeLabel())); + updateChannelState(CHANNEL_STATUS_ENDTIME, + new StringType(localChrono.getActivationTimeLabel(timeZoneProvider))); updateChannelState(CHANNEL_STATUS_TEMP_FORMAT, new StringType(localChrono.getTemperatureFormat())); final Program localProgram = localChrono.getProgram(); if (localProgram != null) {