Skip to content

Commit

Permalink
[openweathermap] Consider TimeZoneProvider to build state for time-st…
Browse files Browse the repository at this point in the history
…amp channels (#7885)

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored Jun 9, 2020
1 parent 67b11c0 commit 0614d69
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.smarthome.config.discovery.DiscoveryService;
import org.eclipse.smarthome.core.i18n.LocaleProvider;
import org.eclipse.smarthome.core.i18n.LocationProvider;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.i18n.TranslationProvider;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Thing;
Expand Down Expand Up @@ -65,15 +66,17 @@ public class OpenWeatherMapHandlerFactory extends BaseThingHandlerFactory {
private final LocaleProvider localeProvider;
private final LocationProvider locationProvider;
private final TranslationProvider i18nProvider;
private final TimeZoneProvider timeZoneProvider;

@Activate
public OpenWeatherMapHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
final @Reference LocaleProvider localeProvider, final @Reference LocationProvider locationProvider,
final @Reference TranslationProvider i18nProvider) {
final @Reference TranslationProvider i18nProvider, final @Reference TimeZoneProvider timeZoneProvider) {
this.httpClient = httpClientFactory.getCommonHttpClient();
this.localeProvider = localeProvider;
this.locationProvider = locationProvider;
this.i18nProvider = i18nProvider;
this.timeZoneProvider = timeZoneProvider;
}

@Override
Expand All @@ -94,9 +97,9 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
return handler;
} else if (THING_TYPE_WEATHER_AND_FORECAST.equals(thingTypeUID)) {
return new OpenWeatherMapWeatherAndForecastHandler(thing);
return new OpenWeatherMapWeatherAndForecastHandler(thing, timeZoneProvider);
} else if (THING_TYPE_UVINDEX.equals(thingTypeUID)) {
return new OpenWeatherMapUVIndexHandler(thing);
return new OpenWeatherMapUVIndexHandler(thing, timeZoneProvider);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static org.openhab.binding.openweathermap.internal.OpenWeatherMapBindingConstants.*;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -28,6 +27,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.library.types.DateTimeType;
import org.eclipse.smarthome.core.library.types.DecimalType;
import org.eclipse.smarthome.core.library.types.PointType;
Expand Down Expand Up @@ -72,11 +72,14 @@ public abstract class AbstractOpenWeatherMapHandler extends BaseThingHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.unmodifiableSet(
Stream.of(THING_TYPE_WEATHER_AND_FORECAST, THING_TYPE_UVINDEX).collect(Collectors.toSet()));

private final TimeZoneProvider timeZoneProvider;

// keeps track of the parsed location
protected @Nullable PointType location;

public AbstractOpenWeatherMapHandler(Thing thing) {
public AbstractOpenWeatherMapHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
super(thing);
this.timeZoneProvider = timeZoneProvider;
}

@Override
Expand Down Expand Up @@ -176,8 +179,8 @@ && isLinked(channelUID)) {

protected State getDateTimeTypeState(@Nullable Integer value) {
return (value == null) ? UnDefType.UNDEF
: new DateTimeType(
ZonedDateTime.ofInstant(Instant.ofEpochSecond(value.longValue()), ZoneId.systemDefault()));
: new DateTimeType(ZonedDateTime.ofInstant(Instant.ofEpochSecond(value.longValue()),
timeZoneProvider.getTimeZone()));
}

protected State getDecimalTypeState(@Nullable Double value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
Expand Down Expand Up @@ -60,8 +61,8 @@ public class OpenWeatherMapUVIndexHandler extends AbstractOpenWeatherMapHandler
private @Nullable OpenWeatherMapJsonUVIndexData uvindexData;
private @Nullable List<OpenWeatherMapJsonUVIndexData> uvindexForecastData;

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpResponseException;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.library.types.QuantityType;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
Expand Down Expand Up @@ -77,8 +78,8 @@ public class OpenWeatherMapWeatherAndForecastHandler extends AbstractOpenWeather
private @Nullable OpenWeatherMapJsonHourlyForecastData hourlyForecastData;
private @Nullable OpenWeatherMapJsonDailyForecastData dailyForecastData;

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

@Override
Expand Down

0 comments on commit 0614d69

Please sign in to comment.