[bticinosmarter] Fix activationdate parsing (#15474)

* Fix DateTime format
* Switch to TimeZoneProvider

Signed-off-by: lsiepel <leosiepel@gmail.com>
This commit is contained in:
lsiepel 2023-08-27 16:11:33 +02:00 committed by GitHub
parent d1a691498a
commit d9235da5db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 8 deletions

View File

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

View File

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

View File

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