From 10686b08b13767dd395e03286a9416fd5418f515 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sat, 9 Nov 2024 19:50:21 +0100 Subject: [PATCH 01/52] Simplify DateTimeType handling for Automower Signed-off-by: Jacob Laursen --- .../internal/AutomowerHandlerFactory.java | 9 +++---- .../internal/things/AutomowerHandler.java | 26 +++---------------- 2 files changed, 7 insertions(+), 28 deletions(-) diff --git a/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/AutomowerHandlerFactory.java b/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/AutomowerHandlerFactory.java index 6f5b5108ddcac..70187832151ff 100644 --- a/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/AutomowerHandlerFactory.java +++ b/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/AutomowerHandlerFactory.java @@ -27,7 +27,6 @@ import org.openhab.binding.automower.internal.things.AutomowerHandler; import org.openhab.core.auth.client.oauth2.OAuthFactory; import org.openhab.core.config.discovery.DiscoveryService; -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; @@ -56,14 +55,12 @@ public class AutomowerHandlerFactory extends BaseThingHandlerFactory { private final OAuthFactory oAuthFactory; protected final @NonNullByDefault({}) HttpClient httpClient; private @Nullable ServiceRegistration automowerDiscoveryServiceRegistration; - private final TimeZoneProvider timeZoneProvider; @Activate - public AutomowerHandlerFactory(@Reference OAuthFactory oAuthFactory, @Reference HttpClientFactory httpClientFactory, - @Reference TimeZoneProvider timeZoneProvider) { + public AutomowerHandlerFactory(@Reference OAuthFactory oAuthFactory, + @Reference HttpClientFactory httpClientFactory) { this.oAuthFactory = oAuthFactory; this.httpClient = httpClientFactory.getCommonHttpClient(); - this.timeZoneProvider = timeZoneProvider; } @Override @@ -80,7 +77,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) { } if (AutomowerHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) { - return new AutomowerHandler(thing, timeZoneProvider); + return new AutomowerHandler(thing); } return null; diff --git a/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/things/AutomowerHandler.java b/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/things/AutomowerHandler.java index b0a87a253cd4b..1a8b9ef1faca1 100644 --- a/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/things/AutomowerHandler.java +++ b/bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/things/AutomowerHandler.java @@ -15,7 +15,6 @@ import static org.openhab.binding.automower.internal.AutomowerBindingConstants.*; import java.time.Instant; -import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Collection; import java.util.Map; @@ -36,7 +35,6 @@ import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.RestrictedReason; import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.State; import org.openhab.binding.automower.internal.rest.exceptions.AutomowerCommunicationException; -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; @@ -76,7 +74,6 @@ public class AutomowerHandler extends BaseThingHandler { private static final long DEFAULT_POLLING_INTERVAL_S = TimeUnit.MINUTES.toSeconds(10); private final Logger logger = LoggerFactory.getLogger(AutomowerHandler.class); - private final TimeZoneProvider timeZoneProvider; private AtomicReference automowerId = new AtomicReference<>(NO_ID); private long lastQueryTimeMs = 0L; @@ -97,9 +94,8 @@ public class AutomowerHandler extends BaseThingHandler { } }; - public AutomowerHandler(Thing thing, TimeZoneProvider timeZoneProvider) { + public AutomowerHandler(Thing thing) { super(thing); - this.timeZoneProvider = timeZoneProvider; } @Override @@ -287,7 +283,7 @@ private void updateChannelState(@Nullable Mower mower) { } updateState(CHANNEL_STATUS_LAST_UPDATE, - new DateTimeType(toZonedDateTime(mower.getAttributes().getMetadata().getStatusTimestamp()))); + new DateTimeType(Instant.ofEpochMilli(mower.getAttributes().getMetadata().getStatusTimestamp()))); updateState(CHANNEL_STATUS_BATTERY, new QuantityType<>(mower.getAttributes().getBattery().getBatteryPercent(), Units.PERCENT)); @@ -297,7 +293,7 @@ private void updateChannelState(@Nullable Mower mower) { if (errorCodeTimestamp == 0L) { updateState(CHANNEL_STATUS_ERROR_TIMESTAMP, UnDefType.NULL); } else { - updateState(CHANNEL_STATUS_ERROR_TIMESTAMP, new DateTimeType(toZonedDateTime(errorCodeTimestamp))); + updateState(CHANNEL_STATUS_ERROR_TIMESTAMP, new DateTimeType(Instant.ofEpochMilli(errorCodeTimestamp))); } long nextStartTimestamp = mower.getAttributes().getPlanner().getNextStartTimestamp(); @@ -305,7 +301,7 @@ private void updateChannelState(@Nullable Mower mower) { if (nextStartTimestamp == 0L) { updateState(CHANNEL_PLANNER_NEXT_START, UnDefType.NULL); } else { - updateState(CHANNEL_PLANNER_NEXT_START, new DateTimeType(toZonedDateTime(nextStartTimestamp))); + updateState(CHANNEL_PLANNER_NEXT_START, new DateTimeType(Instant.ofEpochMilli(nextStartTimestamp))); } updateState(CHANNEL_PLANNER_OVERRIDE_ACTION, new StringType(mower.getAttributes().getPlanner().getOverride().getAction())); @@ -339,18 +335,4 @@ private void initializeProperties(@Nullable Mower mower) { updateProperties(properties); } - - /** - * Converts timestamp returned by the Automower API into local time-zone. - * Timestamp returned by the API doesn't have offset and it always in the current time zone - it can be treated as - * UTC. - * Method builds a ZonedDateTime with same hour value but in the current system timezone. - * - * @param timestamp - Automower API timestamp - * @return ZonedDateTime in system timezone - */ - private ZonedDateTime toZonedDateTime(long timestamp) { - Instant timestampInstant = Instant.ofEpochMilli(timestamp); - return ZonedDateTime.ofInstant(timestampInstant, timeZoneProvider.getTimeZone()); - } } From ad21f636970ab302e265d3c3a3aa34ee8437205f Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Mon, 11 Nov 2024 20:28:16 +0100 Subject: [PATCH 02/52] Simplify DateTimeType handling for aWATTar Signed-off-by: Jacob Laursen --- .../org/openhab/binding/awattar/internal/AwattarUtil.java | 5 ----- .../awattar/internal/handler/AwattarBestPriceHandler.java | 6 +++--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/AwattarUtil.java b/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/AwattarUtil.java index e6ba28341c077..37ee5e6695585 100644 --- a/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/AwattarUtil.java +++ b/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/AwattarUtil.java @@ -21,7 +21,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.i18n.TimeZoneProvider; -import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.unit.Units; @@ -47,10 +46,6 @@ public static ZonedDateTime getCalendarForHour(int hour, ZoneId zone) { return ZonedDateTime.now(zone).truncatedTo(ChronoUnit.DAYS).plusHours(hour); } - public static DateTimeType getDateTimeType(long time, TimeZoneProvider tz) { - return new DateTimeType(ZonedDateTime.ofInstant(Instant.ofEpochMilli(time), tz.getTimeZone())); - } - public static QuantityType