From 225ebd9c83831e9bc459dbc2c246628834ce1755 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Tue, 26 Nov 2024 23:37:49 +0100 Subject: [PATCH] Simplify DateTimeType handling for OpenWeatherMap Signed-off-by: Jacob Laursen --- .../factory/OpenWeatherMapHandlerFactory.java | 13 +++++-------- .../handler/AbstractOpenWeatherMapHandler.java | 11 ++--------- .../handler/OpenWeatherMapAirPollutionHandler.java | 5 ++--- .../handler/OpenWeatherMapOneCallHandler.java | 5 ++--- .../OpenWeatherMapOneCallHistoryHandler.java | 5 ++--- .../OpenWeatherMapWeatherAndForecastHandler.java | 5 ++--- .../OpenWeatherMapOneCallHistoryHandlerTest.java | 7 +------ 7 files changed, 16 insertions(+), 35 deletions(-) diff --git a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/factory/OpenWeatherMapHandlerFactory.java b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/factory/OpenWeatherMapHandlerFactory.java index 44857d1651f..51e0d040068 100644 --- a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/factory/OpenWeatherMapHandlerFactory.java +++ b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/factory/OpenWeatherMapHandlerFactory.java @@ -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; diff --git a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/AbstractOpenWeatherMapHandler.java b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/AbstractOpenWeatherMapHandler.java index e503de5c406..acd2a23909b 100644 --- a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/AbstractOpenWeatherMapHandler.java +++ b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/AbstractOpenWeatherMapHandler.java @@ -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 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) { diff --git a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapAirPollutionHandler.java b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapAirPollutionHandler.java index 099a3478fcd..3a72d4a5db7 100644 --- a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapAirPollutionHandler.java +++ b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapAirPollutionHandler.java @@ -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 diff --git a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapOneCallHandler.java b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapOneCallHandler.java index 3a702778e8b..77d59da39c2 100644 --- a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapOneCallHandler.java +++ b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapOneCallHandler.java @@ -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 diff --git a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapOneCallHistoryHandler.java b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapOneCallHistoryHandler.java index 09dde6d42f0..b54a47e8680 100644 --- a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapOneCallHistoryHandler.java +++ b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapOneCallHistoryHandler.java @@ -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 diff --git a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapWeatherAndForecastHandler.java b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapWeatherAndForecastHandler.java index 61534d7e523..9a722ce8255 100644 --- a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapWeatherAndForecastHandler.java +++ b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapWeatherAndForecastHandler.java @@ -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 diff --git a/bundles/org.openhab.binding.openweathermap/src/test/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapOneCallHistoryHandlerTest.java b/bundles/org.openhab.binding.openweathermap/src/test/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapOneCallHistoryHandlerTest.java index b75d5a56f1c..b93f3a677b8 100644 --- a/bundles/org.openhab.binding.openweathermap/src/test/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapOneCallHistoryHandlerTest.java +++ b/bundles/org.openhab.binding.openweathermap/src/test/java/org/openhab/binding/openweathermap/internal/handler/OpenWeatherMapOneCallHistoryHandlerTest.java @@ -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);