Skip to content

Commit

Permalink
[netatmo] Consider TimeZoneProvider (openhab#7894)
Browse files Browse the repository at this point in the history
* [netatmo] Consider TimeZoneProvider
* Add a @nullable
* Avoid storing timeZoneProvider in a static variable

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored and andrewfg committed Aug 31, 2020
1 parent 66c240e commit b261d1f
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 100 deletions.
3 changes: 2 additions & 1 deletion bundles/org.openhab.binding.netatmo/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/swagger/src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ public static State toStringType(@Nullable String value) {
return (value == null) ? UnDefType.NULL : new StringType(value);
}

public static ZonedDateTime toZonedDateTime(Integer netatmoTS) {
public static ZonedDateTime toZonedDateTime(Integer netatmoTS, ZoneId zoneId) {
Instant i = Instant.ofEpochSecond(netatmoTS);
return ZonedDateTime.ofInstant(i, ZoneId.systemDefault());
return ZonedDateTime.ofInstant(i, zoneId);
}

public static State toDateTimeType(@Nullable Float netatmoTS) {
return netatmoTS == null ? UnDefType.NULL : toDateTimeType(toZonedDateTime(netatmoTS.intValue()));
public static State toDateTimeType(@Nullable Float netatmoTS, ZoneId zoneId) {
return netatmoTS == null ? UnDefType.NULL : toDateTimeType(toZonedDateTime(netatmoTS.intValue(), zoneId));
}

public static State toDateTimeType(@Nullable Integer netatmoTS) {
return netatmoTS == null ? UnDefType.NULL : toDateTimeType(toZonedDateTime(netatmoTS));
public static State toDateTimeType(@Nullable Integer netatmoTS, ZoneId zoneId) {
return netatmoTS == null ? UnDefType.NULL : toDateTimeType(toZonedDateTime(netatmoTS, zoneId));
}

public static State toDateTimeType(@Nullable ZonedDateTime zonedDateTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.discovery.DiscoveryService;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
Expand Down Expand Up @@ -66,12 +67,15 @@ public class NetatmoHandlerFactory extends BaseThingHandlerFactory {
private final Map<ThingUID, ServiceRegistration<?>> webHookServiceRegs = new HashMap<>();
private final HttpService httpService;
private final NATherm1StateDescriptionProvider stateDescriptionProvider;
private final TimeZoneProvider timeZoneProvider;

@Activate
public NetatmoHandlerFactory(final @Reference HttpService httpService,
final @Reference NATherm1StateDescriptionProvider stateDescriptionProvider) {
final @Reference NATherm1StateDescriptionProvider stateDescriptionProvider,
final @Reference TimeZoneProvider timeZoneProvider) {
this.httpService = httpService;
this.stateDescriptionProvider = stateDescriptionProvider;
this.timeZoneProvider = timeZoneProvider;
}

@Override
Expand All @@ -88,27 +92,27 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
registerDeviceDiscoveryService(bridgeHandler);
return bridgeHandler;
} else if (thingTypeUID.equals(MODULE1_THING_TYPE)) {
return new NAModule1Handler(thing);
return new NAModule1Handler(thing, timeZoneProvider);
} else if (thingTypeUID.equals(MODULE2_THING_TYPE)) {
return new NAModule2Handler(thing);
return new NAModule2Handler(thing, timeZoneProvider);
} else if (thingTypeUID.equals(MODULE3_THING_TYPE)) {
return new NAModule3Handler(thing);
return new NAModule3Handler(thing, timeZoneProvider);
} else if (thingTypeUID.equals(MODULE4_THING_TYPE)) {
return new NAModule4Handler(thing);
return new NAModule4Handler(thing, timeZoneProvider);
} else if (thingTypeUID.equals(MAIN_THING_TYPE)) {
return new NAMainHandler(thing);
return new NAMainHandler(thing, timeZoneProvider);
} else if (thingTypeUID.equals(HOMECOACH_THING_TYPE)) {
return new NAHealthyHomeCoachHandler(thing);
return new NAHealthyHomeCoachHandler(thing, timeZoneProvider);
} else if (thingTypeUID.equals(PLUG_THING_TYPE)) {
return new NAPlugHandler(thing);
return new NAPlugHandler(thing, timeZoneProvider);
} else if (thingTypeUID.equals(THERM1_THING_TYPE)) {
return new NATherm1Handler(thing, stateDescriptionProvider);
return new NATherm1Handler(thing, stateDescriptionProvider, timeZoneProvider);
} else if (thingTypeUID.equals(WELCOME_HOME_THING_TYPE)) {
return new NAWelcomeHomeHandler(thing);
return new NAWelcomeHomeHandler(thing, timeZoneProvider);
} else if (thingTypeUID.equals(WELCOME_CAMERA_THING_TYPE) || thingTypeUID.equals(PRESENCE_CAMERA_THING_TYPE)) {
return new NAWelcomeCameraHandler(thing);
return new NAWelcomeCameraHandler(thing, timeZoneProvider);
} else if (thingTypeUID.equals(WELCOME_PERSON_THING_TYPE)) {
return new NAWelcomePersonHandler(thing);
return new NAWelcomePersonHandler(thing, timeZoneProvider);
} else {
logger.warn("ThingHandler not found for {}", thing.getThingTypeUID());
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.binding.netatmo.internal;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
Expand Down Expand Up @@ -57,7 +58,7 @@ public RefreshStrategy(int dataValidityPeriod) {
}

@SuppressWarnings("null")
public void setDataTimeStamp(Integer dataTimestamp) {
public void setDataTimeStamp(Integer dataTimestamp, ZoneId zoneId) {
if (searchRefreshInterval) {
if (dataTimestamp0 == null) {
dataTimestamp0 = dataTimestamp;
Expand All @@ -70,7 +71,7 @@ public void setDataTimeStamp(Integer dataTimestamp) {
logger.debug("Data validity period not yet found - data timestamp unchanged");
}
}
this.dataTimeStamp = ChannelTypeUtils.toZonedDateTime(dataTimestamp).toInstant().toEpochMilli();
this.dataTimeStamp = ChannelTypeUtils.toZonedDateTime(dataTimestamp, zoneId).toInstant().toEpochMilli();
}

public long dataAge() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
*/
package org.openhab.binding.netatmo.internal.camera;

import static org.openhab.binding.netatmo.internal.ChannelTypeUtils.toOnOffType;
import static org.openhab.binding.netatmo.internal.ChannelTypeUtils.toStringType;
import static org.openhab.binding.netatmo.internal.ChannelTypeUtils.*;
import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.types.State;
Expand All @@ -37,8 +37,8 @@ public class CameraHandler extends NetatmoModuleHandler<NAWelcomeCamera> {

private static final String LIVE_PICTURE = "/live/snapshot_720.jpg";

public CameraHandler(@NonNull Thing thing) {
super(thing);
public CameraHandler(@NonNull Thing thing, final TimeZoneProvider timeZoneProvider) {
super(thing, timeZoneProvider);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.library.unit.SIUnits;
import org.eclipse.smarthome.core.library.unit.SmartHomeUnits;
import org.eclipse.smarthome.core.thing.*;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.ThingStatusInfo;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.thing.binding.BridgeHandler;
import org.eclipse.smarthome.core.thing.type.ChannelKind;
Expand Down Expand Up @@ -66,14 +72,16 @@ public abstract class AbstractNetatmoThingHandler extends BaseThingHandler {
public static final Unit<Dimensionless> API_CO2_UNIT = SmartHomeUnits.PARTS_PER_MILLION;
public static final Unit<Dimensionless> API_NOISE_UNIT = SmartHomeUnits.DECIBEL;

protected final TimeZoneProvider timeZoneProvider;
protected final MeasurableChannels measurableChannels = new MeasurableChannels();
protected Optional<RadioHelper> radioHelper;
protected Optional<BatteryHelper> batteryHelper;
protected Configuration config;
protected NetatmoBridgeHandler bridgeHandler;

AbstractNetatmoThingHandler(@NonNull Thing thing) {
AbstractNetatmoThingHandler(@NonNull Thing thing, final TimeZoneProvider timeZoneProvider) {
super(thing);
this.timeZoneProvider = timeZoneProvider;
}

@Override
Expand Down Expand Up @@ -141,14 +149,14 @@ private void updateDataChannels() {
getThing().getChannels().stream().filter(channel -> !channel.getKind().equals(ChannelKind.TRIGGER))
.forEach(channel -> {

String channelId = channel.getUID().getId();
if (isLinked(channelId)) {
State state = getNAThingProperty(channelId);
if (state != null) {
updateState(channel.getUID(), state);
}
}
});
String channelId = channel.getUID().getId();
if (isLinked(channelId)) {
State state = getNAThingProperty(channelId);
if (state != null) {
updateState(channel.getUID(), state);
}
}
});
}

/**
Expand All @@ -162,6 +170,7 @@ private void triggerEventChannels() {

/**
* Triggers the trigger channel with the given channel id when required (when an update is available)
*
* @param channelId channel id
*/
protected void triggerChannelIfRequired(@NonNull String channelId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.library.types.DecimalType;
import org.eclipse.smarthome.core.library.types.PointType;
import org.eclipse.smarthome.core.thing.Thing;
Expand Down Expand Up @@ -55,8 +56,8 @@ public abstract class NetatmoDeviceHandler<DEVICE> extends AbstractNetatmoThingH
protected DEVICE device;
protected Map<String, Object> childs = new ConcurrentHashMap<>();

public NetatmoDeviceHandler(Thing thing) {
super(thing);
public NetatmoDeviceHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
super(thing, timeZoneProvider);
}

@Override
Expand Down Expand Up @@ -123,7 +124,7 @@ protected void updateChannels() {
updateProperties(device);
Integer dataTimeStamp = getDataTimestamp();
if (dataTimeStamp != null) {
refreshStrategy.setDataTimeStamp(dataTimeStamp);
refreshStrategy.setDataTimeStamp(dataTimeStamp, timeZoneProvider.getTimeZone());
}
radioHelper.ifPresent(helper -> helper.setModule(device));
NetatmoBridgeHandler handler = getBridgeHandler();
Expand Down Expand Up @@ -156,7 +157,7 @@ protected State getNAThingProperty(@NonNull String channelId) {
if (device != null) {
Method getLastStatusStore = device.getClass().getMethod("getLastStatusStore");
Integer lastStatusStore = (Integer) getLastStatusStore.invoke(device);
return ChannelTypeUtils.toDateTimeType(lastStatusStore);
return ChannelTypeUtils.toDateTimeType(lastStatusStore, timeZoneProvider.getTimeZone());
} else {
return UnDefType.UNDEF;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.types.State;
Expand All @@ -43,8 +44,8 @@ public class NetatmoModuleHandler<MODULE> extends AbstractNetatmoThingHandler {
protected MODULE module;
private boolean refreshRequired;

protected NetatmoModuleHandler(Thing thing) {
super(thing);
protected NetatmoModuleHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
super(thing, timeZoneProvider);
}

@Override
Expand Down Expand Up @@ -77,7 +78,7 @@ protected State getNAThingProperty(@NonNull String channelId) {
if (channelId.equalsIgnoreCase(CHANNEL_LAST_MESSAGE) && module != null) {
Method getLastMessage = module.getClass().getMethod("getLastMessage");
Integer lastMessage = (Integer) getLastMessage.invoke(module);
return ChannelTypeUtils.toDateTimeType(lastMessage);
return ChannelTypeUtils.toDateTimeType(lastMessage, timeZoneProvider.getTimeZone());
}
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.types.State;
import org.openhab.binding.netatmo.internal.handler.NetatmoDeviceHandler;
Expand All @@ -33,8 +34,8 @@
*/
public class NAHealthyHomeCoachHandler extends NetatmoDeviceHandler<NAHealthyHomeCoach> {

public NAHealthyHomeCoachHandler(@NonNull Thing thing) {
super(thing);
public NAHealthyHomeCoachHandler(@NonNull Thing thing, final TimeZoneProvider timeZoneProvider) {
super(thing, timeZoneProvider);
}

@Override
Expand Down Expand Up @@ -78,11 +79,11 @@ protected State getNAThingProperty(@NonNull String channelId) {
case CHANNEL_ABSOLUTE_PRESSURE:
return toQuantityType(dashboardData.getAbsolutePressure(), API_PRESSURE_UNIT);
case CHANNEL_TIMEUTC:
return toDateTimeType(dashboardData.getTimeUtc());
return toDateTimeType(dashboardData.getTimeUtc(), timeZoneProvider.getTimeZone());
case CHANNEL_DATE_MIN_TEMP:
return toDateTimeType(dashboardData.getDateMinTemp());
return toDateTimeType(dashboardData.getDateMinTemp(), timeZoneProvider.getTimeZone());
case CHANNEL_DATE_MAX_TEMP:
return toDateTimeType(dashboardData.getDateMaxTemp());
return toDateTimeType(dashboardData.getDateMaxTemp(), timeZoneProvider.getTimeZone());
case CHANNEL_HUMIDITY:
return toQuantityType(dashboardData.getHumidity(), API_HUMIDITY_UNIT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.types.State;
import org.openhab.binding.netatmo.internal.WeatherUtils;
Expand All @@ -45,8 +46,8 @@
public class NAMainHandler extends NetatmoDeviceHandler<NAMain> {
private Map<String, Float> channelMeasurements = new ConcurrentHashMap<>();

public NAMainHandler(Thing thing) {
super(thing);
public NAMainHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
super(thing, timeZoneProvider);
}

@Override
Expand Down Expand Up @@ -190,11 +191,11 @@ protected State getNAThingProperty(@NonNull String channelId) {
case CHANNEL_ABSOLUTE_PRESSURE:
return toQuantityType(dashboardData.getAbsolutePressure(), API_PRESSURE_UNIT);
case CHANNEL_TIMEUTC:
return toDateTimeType(dashboardData.getTimeUtc());
return toDateTimeType(dashboardData.getTimeUtc(), timeZoneProvider.getTimeZone());
case CHANNEL_DATE_MIN_TEMP:
return toDateTimeType(dashboardData.getDateMinTemp());
return toDateTimeType(dashboardData.getDateMinTemp(), timeZoneProvider.getTimeZone());
case CHANNEL_DATE_MAX_TEMP:
return toDateTimeType(dashboardData.getDateMaxTemp());
return toDateTimeType(dashboardData.getDateMaxTemp(), timeZoneProvider.getTimeZone());
case CHANNEL_HUMIDITY:
return toQuantityType(dashboardData.getHumidity(), API_HUMIDITY_UNIT);
case CHANNEL_HUMIDEX:
Expand Down Expand Up @@ -279,7 +280,7 @@ protected State getNAThingProperty(@NonNull String channelId) {
case CHANNEL_DATE_MIN_TEMP_THIS_MONTH:
case CHANNEL_DATE_MAX_TEMP_THIS_WEEK:
case CHANNEL_DATE_MAX_TEMP_THIS_MONTH:
return toDateTimeType(channelMeasurements.get(channelId));
return toDateTimeType(channelMeasurements.get(channelId), timeZoneProvider.getTimeZone());
}

return super.getNAThingProperty(channelId);
Expand Down
Loading

0 comments on commit b261d1f

Please sign in to comment.