review timezone handling

Signed-off-by: Laurent ARNAL <laurent@clae.net>
This commit is contained in:
Laurent ARNAL 2025-01-08 11:36:43 +01:00
parent 1670c52981
commit 7931a14bf2
4 changed files with 42 additions and 11 deletions

View File

@ -13,6 +13,7 @@
package org.openhab.binding.linky.internal;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.config.core.Configuration;
/**
* The {@link LinkyConfiguration} is the class used to match the
@ -22,10 +23,11 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
* @author Laurent Arnal - Rewrite addon to use official dataconect API
*/
@NonNullByDefault
public class LinkyConfiguration {
public class LinkyConfiguration extends Configuration {
public static final String INTERNAL_AUTH_ID = "internalAuthId";
public String token = "";
public String timezone = "";
public String prmId = "";
public String clientId = "";
public String clientSecret = "";

View File

@ -29,6 +29,7 @@ import org.openhab.binding.linky.internal.handler.LinkyHandler;
import org.openhab.binding.linky.internal.handler.MyElectricalDataBridgeHandler;
import org.openhab.core.auth.client.oauth2.OAuthFactory;
import org.openhab.core.i18n.LocaleProvider;
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;
@ -80,6 +81,7 @@ public class LinkyHandlerFactory extends BaseThingHandlerFactory {
private final HttpService httpService;
private final ThingRegistry thingRegistry;
private final ComponentContext componentContext;
private final TimeZoneProvider timeZoneProvider;
private final Gson gson = new GsonBuilder()
.registerTypeAdapter(ZonedDateTime.class,
@ -106,9 +108,9 @@ public class LinkyHandlerFactory extends BaseThingHandlerFactory {
public LinkyHandlerFactory(final @Reference LocaleProvider localeProvider,
final @Reference HttpClientFactory httpClientFactory, final @Reference OAuthFactory oAuthFactory,
final @Reference HttpService httpService, final @Reference ThingRegistry thingRegistry,
ComponentContext componentContext) {
ComponentContext componentContext, final @Reference TimeZoneProvider timeZoneProvider) {
this.localeProvider = localeProvider;
this.timeZoneProvider = timeZoneProvider;
this.httpClientFactory = httpClientFactory;
this.oAuthFactory = oAuthFactory;
this.httpService = httpService;
@ -141,7 +143,7 @@ public class LinkyHandlerFactory extends BaseThingHandlerFactory {
this.httpClientFactory, this.oAuthFactory, this.httpService, thingRegistry, componentContext, gson);
return handler;
} else if (THING_TYPE_LINKY.equals(thing.getThingTypeUID())) {
LinkyHandler handler = new LinkyHandler(thing, localeProvider);
LinkyHandler handler = new LinkyHandler(thing, localeProvider, timeZoneProvider);
return handler;
}

View File

@ -20,7 +20,6 @@ import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.WeekFields;
@ -50,6 +49,7 @@ import org.openhab.binding.linky.internal.dto.UsagePoint;
import org.openhab.binding.linky.internal.dto.UserInfo;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.i18n.LocaleProvider;
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.QuantityType;
@ -84,6 +84,9 @@ import org.threeten.extra.Years;
@NonNullByDefault
public class LinkyHandler extends BaseThingHandler {
private final TimeZoneProvider timeZoneProvider;
private ZoneId zoneId = ZoneId.systemDefault();
private static final int REFRESH_FIRST_HOUR_OF_DAY = 1;
private static final int REFRESH_INTERVAL_IN_MIN = 120;
@ -109,10 +112,11 @@ public class LinkyHandler extends BaseThingHandler {
ALL
}
public LinkyHandler(Thing thing, LocaleProvider localeProvider) {
public LinkyHandler(Thing thing, LocaleProvider localeProvider, TimeZoneProvider timeZoneProvider) {
super(thing);
config = getConfigAs(LinkyConfiguration.class);
this.timeZoneProvider = timeZoneProvider;
this.dailyConsumption = new ExpiringDayCache<>("dailyConsumption", REFRESH_FIRST_HOUR_OF_DAY, () -> {
LocalDate today = LocalDate.now();
@ -166,6 +170,22 @@ public class LinkyHandler extends BaseThingHandler {
public synchronized void initialize() {
logger.debug("Initializing Linky handler.");
// update the timezone if not set to default to openhab default timezone
Configuration thingConfig = getConfig();
Object val = thingConfig.get("timezone");
if (val == null || "".equals(val)) {
zoneId = this.timeZoneProvider.getTimeZone();
thingConfig.put("timezone", zoneId.getId());
} else {
zoneId = ZoneId.of((String) val);
}
saveConfiguration(thingConfig);
// reread config to update timezone field
config = getConfigAs(LinkyConfiguration.class);
Bridge bridge = getBridge();
if (bridge == null) {
return;
@ -386,15 +406,15 @@ public class LinkyHandler extends BaseThingHandler {
updatekVAChannel(DAILY_GROUP, PEAK_POWER_DAY_MINUS_1, values.baseValue[dSize - 1].value);
updateState(DAILY_GROUP, PEAK_POWER_TS_DAY_MINUS_1,
new DateTimeType(values.baseValue[dSize - 1].date.atZone(ZoneId.systemDefault())));
new DateTimeType(values.baseValue[dSize - 1].date.atZone(zoneId)));
updatekVAChannel(DAILY_GROUP, PEAK_POWER_DAY_MINUS_2, values.baseValue[dSize - 2].value);
updateState(DAILY_GROUP, PEAK_POWER_TS_DAY_MINUS_2,
new DateTimeType(values.baseValue[dSize - 2].date.atZone(ZoneId.systemDefault())));
new DateTimeType(values.baseValue[dSize - 2].date.atZone(zoneId)));
updatekVAChannel(DAILY_GROUP, PEAK_POWER_DAY_MINUS_3, values.baseValue[dSize - 3].value);
updateState(DAILY_GROUP, PEAK_POWER_TS_DAY_MINUS_3,
new DateTimeType(values.baseValue[dSize - 3].date.atZone(ZoneId.systemDefault())));
new DateTimeType(values.baseValue[dSize - 3].date.atZone(zoneId)));
updatePowerTimeSeries(DAILY_GROUP, MAX_POWER_CHANNEL, values.baseValue);
updatePowerTimeSeries(WEEKLY_GROUP, MAX_POWER_CHANNEL, values.weekValue);
@ -482,7 +502,7 @@ public class LinkyHandler extends BaseThingHandler {
continue;
}
Instant timestamp = iv[i].date.toInstant(ZoneOffset.UTC);
Instant timestamp = iv[i].date.atZone(zoneId).toInstant();
if (Double.isNaN(iv[i].value)) {
continue;
@ -504,7 +524,7 @@ public class LinkyHandler extends BaseThingHandler {
continue;
}
Instant timestamp = iv[i].date.toInstant(ZoneOffset.UTC);
Instant timestamp = iv[i].date.atZone(zoneId).toInstant();
if (Double.isNaN(iv[i].value)) {
continue;

View File

@ -97,6 +97,13 @@
<label>prmId</label>
<description>Your prmId</description>
</parameter>
<parameter name="timezone" type="text" required="false">
<label>timezone</label>
<description>The timezone associated with your Point of delivery.
Will default to openhab default timezone.
You will need to change this if your linky is located in a different timezone that your openhab location.
You can use an offset, or a label like Europe/Paris</description>
</parameter>
<parameter name="token" type="text" required="false">
<label>Token</label>
<description>Your Enedis token (can be left empty, use the connection page to automatically fill it