Simplify DateTimeType handling for OpenWeatherMap

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-11-26 23:37:49 +01:00 committed by lsiepel
parent 541f8d4028
commit 225ebd9c83
7 changed files with 16 additions and 35 deletions

View File

@ -34,7 +34,6 @@ import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapWeather
import org.openhab.core.config.discovery.DiscoveryService;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.LocationProvider;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.thing.Bridge;
@ -67,17 +66,15 @@ 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 TimeZoneProvider timeZoneProvider) {
final @Reference TranslationProvider i18nProvider) {
this.httpClient = httpClientFactory.getCommonHttpClient();
this.localeProvider = localeProvider;
this.locationProvider = locationProvider;
this.i18nProvider = i18nProvider;
this.timeZoneProvider = timeZoneProvider;
}
@Override
@ -98,13 +95,13 @@ public class OpenWeatherMapHandlerFactory extends BaseThingHandlerFactory {
bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, null));
return handler;
} else if (THING_TYPE_WEATHER_AND_FORECAST.equals(thingTypeUID)) {
return new OpenWeatherMapWeatherAndForecastHandler(thing, timeZoneProvider);
return new OpenWeatherMapWeatherAndForecastHandler(thing);
} else if (THING_TYPE_AIR_POLLUTION.equals(thingTypeUID)) {
return new OpenWeatherMapAirPollutionHandler(thing, timeZoneProvider);
return new OpenWeatherMapAirPollutionHandler(thing);
} else if (THING_TYPE_ONECALL_WEATHER_AND_FORECAST.equals(thingTypeUID)) {
return new OpenWeatherMapOneCallHandler(thing, timeZoneProvider);
return new OpenWeatherMapOneCallHandler(thing);
} else if (THING_TYPE_ONECALL_HISTORY.equals(thingTypeUID)) {
return new OpenWeatherMapOneCallHistoryHandler(thing, timeZoneProvider);
return new OpenWeatherMapOneCallHistoryHandler(thing);
}
return null;

View File

@ -15,7 +15,6 @@ package org.openhab.binding.openweathermap.internal.handler;
import static org.openhab.binding.openweathermap.internal.OpenWeatherMapBindingConstants.*;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@ -28,7 +27,6 @@ import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapLocation
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection;
import org.openhab.core.i18n.CommunicationException;
import org.openhab.core.i18n.ConfigurationException;
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.PointType;
@ -69,14 +67,11 @@ public abstract class AbstractOpenWeatherMapHandler extends BaseThingHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_WEATHER_AND_FORECAST,
THING_TYPE_AIR_POLLUTION, THING_TYPE_ONECALL_WEATHER_AND_FORECAST, THING_TYPE_ONECALL_HISTORY);
private final TimeZoneProvider timeZoneProvider;
// keeps track of the parsed location
protected @Nullable PointType location;
public AbstractOpenWeatherMapHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
public AbstractOpenWeatherMapHandler(Thing thing) {
super(thing);
this.timeZoneProvider = timeZoneProvider;
}
@Override
@ -171,9 +166,7 @@ public abstract class AbstractOpenWeatherMapHandler extends BaseThingHandler {
protected abstract void updateChannel(ChannelUID channelUID);
protected State getDateTimeTypeState(@Nullable Integer value) {
return (value == null) ? UnDefType.UNDEF
: new DateTimeType(ZonedDateTime.ofInstant(Instant.ofEpochSecond(value.longValue()),
timeZoneProvider.getTimeZone()));
return (value == null) ? UnDefType.UNDEF : new DateTimeType(Instant.ofEpochSecond(value.longValue()));
}
protected State getDecimalTypeState(@Nullable Double value) {

View File

@ -26,7 +26,6 @@ import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConn
import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonAirPollutionData;
import org.openhab.core.i18n.CommunicationException;
import org.openhab.core.i18n.ConfigurationException;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.Channel;
@ -63,8 +62,8 @@ public class OpenWeatherMapAirPollutionHandler extends AbstractOpenWeatherMapHan
private @Nullable OpenWeatherMapJsonAirPollutionData airPollutionData;
private @Nullable OpenWeatherMapJsonAirPollutionData airPollutionForecastData;
public OpenWeatherMapAirPollutionHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
super(thing, timeZoneProvider);
public OpenWeatherMapAirPollutionHandler(Thing thing) {
super(thing);
}
@Override

View File

@ -37,7 +37,6 @@ import org.openhab.binding.openweathermap.internal.dto.onecall.Hourly;
import org.openhab.binding.openweathermap.internal.dto.onecall.Precipitation;
import org.openhab.core.i18n.CommunicationException;
import org.openhab.core.i18n.ConfigurationException;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
@ -90,8 +89,8 @@ public class OpenWeatherMapOneCallHandler extends AbstractOpenWeatherMapHandler
private int forecastDays = 8;
private int numberOfAlerts = 0;
public OpenWeatherMapOneCallHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
super(thing, timeZoneProvider);
public OpenWeatherMapOneCallHandler(Thing thing) {
super(thing);
}
@Override

View File

@ -30,7 +30,6 @@ import org.openhab.binding.openweathermap.internal.dto.onecall.Precipitation;
import org.openhab.binding.openweathermap.internal.dto.onecallhist.Hourly;
import org.openhab.core.i18n.CommunicationException;
import org.openhab.core.i18n.ConfigurationException;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
@ -62,8 +61,8 @@ public class OpenWeatherMapOneCallHistoryHandler extends AbstractOpenWeatherMapH
// the relative day in history.
private int day = 0;
public OpenWeatherMapOneCallHistoryHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
super(thing, timeZoneProvider);
public OpenWeatherMapOneCallHistoryHandler(Thing thing) {
super(thing);
}
@Override

View File

@ -36,7 +36,6 @@ import org.openhab.binding.openweathermap.internal.dto.forecast.daily.FeelsLikeT
import org.openhab.core.config.core.Configuration;
import org.openhab.core.i18n.CommunicationException;
import org.openhab.core.i18n.ConfigurationException;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
@ -77,8 +76,8 @@ public class OpenWeatherMapWeatherAndForecastHandler extends AbstractOpenWeather
private @Nullable OpenWeatherMapJsonHourlyForecastData hourlyForecastData;
private @Nullable OpenWeatherMapJsonDailyForecastData dailyForecastData;
public OpenWeatherMapWeatherAndForecastHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
super(thing, timeZoneProvider);
public OpenWeatherMapWeatherAndForecastHandler(Thing thing) {
super(thing);
}
@Override

View File

@ -25,7 +25,6 @@ import static org.openhab.binding.openweathermap.internal.TestObjectsUtil.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;
@ -36,7 +35,6 @@ import org.openhab.binding.openweathermap.internal.TestObjectsUtil;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection;
import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapOneCallHistAPIData;
import org.openhab.core.config.core.Configuration;
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.PointType;
@ -93,10 +91,7 @@ public class OpenWeatherMapOneCallHistoryHandlerTest {
private static OpenWeatherMapOneCallHistoryHandler createAndInitHandler(final ThingHandlerCallback callback,
final Thing thing) {
TimeZoneProvider timeZoneProvider = mock(TimeZoneProvider.class);
when(timeZoneProvider.getTimeZone()).thenReturn(ZoneId.of("UTC"));
final OpenWeatherMapOneCallHistoryHandler handler = spy(
new OpenWeatherMapOneCallHistoryHandler(thing, timeZoneProvider));
final OpenWeatherMapOneCallHistoryHandler handler = spy(new OpenWeatherMapOneCallHistoryHandler(thing));
when(callback.isChannelLinked(any())).thenReturn(true);