From 520bda33a9c63489cb1c77ee01a4b15df8d10729 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sun, 17 Nov 2024 21:11:16 +0100 Subject: [PATCH] Simplify DateTimeType handling for Lutron Signed-off-by: Jacob Laursen --- .../lutron/internal/grxprg/PrgBridgeHandler.java | 5 +++-- .../lutron/internal/grxprg/PrgProtocolHandler.java | 11 +++-------- .../lutron/internal/handler/TimeclockHandler.java | 8 ++------ 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/bundles/org.openhab.binding.lutron/src/main/java/org/openhab/binding/lutron/internal/grxprg/PrgBridgeHandler.java b/bundles/org.openhab.binding.lutron/src/main/java/org/openhab/binding/lutron/internal/grxprg/PrgBridgeHandler.java index 72983b4e20e..886567b8d65 100644 --- a/bundles/org.openhab.binding.lutron/src/main/java/org/openhab/binding/lutron/internal/grxprg/PrgBridgeHandler.java +++ b/bundles/org.openhab.binding.lutron/src/main/java/org/openhab/binding/lutron/internal/grxprg/PrgBridgeHandler.java @@ -13,6 +13,7 @@ package org.openhab.binding.lutron.internal.grxprg; import java.io.IOException; +import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.GregorianCalendar; import java.util.concurrent.ScheduledFuture; @@ -165,8 +166,8 @@ public class PrgBridgeHandler extends BaseBridgeHandler { } else if (id.equals(PrgConstants.CHANNEL_ZONERAISESTOP)) { protocolHandler.setZoneRaiseStop(); } else if (id.equals(PrgConstants.CHANNEL_TIMECLOCK)) { - if (command instanceof DateTimeType dateTime) { - final ZonedDateTime zdt = dateTime.getZonedDateTime(); + if (command instanceof DateTimeType dateTimeCommand) { + final ZonedDateTime zdt = dateTimeCommand.getZonedDateTime(ZoneId.systemDefault()); protocolHandler.setTime(GregorianCalendar.from(zdt)); } else { logger.error("Received a TIMECLOCK channel command with a non DateTimeType: {}", command); diff --git a/bundles/org.openhab.binding.lutron/src/main/java/org/openhab/binding/lutron/internal/grxprg/PrgProtocolHandler.java b/bundles/org.openhab.binding.lutron/src/main/java/org/openhab/binding/lutron/internal/grxprg/PrgProtocolHandler.java index a1d12c763df..90f2f419c54 100644 --- a/bundles/org.openhab.binding.lutron/src/main/java/org/openhab/binding/lutron/internal/grxprg/PrgProtocolHandler.java +++ b/bundles/org.openhab.binding.lutron/src/main/java/org/openhab/binding/lutron/internal/grxprg/PrgProtocolHandler.java @@ -13,8 +13,6 @@ package org.openhab.binding.lutron.internal.grxprg; import java.io.IOException; -import java.time.ZoneId; -import java.time.ZonedDateTime; import java.util.Calendar; import java.util.HashMap; import java.util.Map; @@ -885,8 +883,7 @@ class PrgProtocolHandler { final int yr = Integer.parseInt(m.group(5)); c.set(Calendar.YEAR, yr + (yr < 50 ? 1900 : 2000)); - phCallback.stateChanged(PrgConstants.CHANNEL_TIMECLOCK, - new DateTimeType(ZonedDateTime.ofInstant(c.toInstant(), ZoneId.systemDefault()))); + phCallback.stateChanged(PrgConstants.CHANNEL_TIMECLOCK, new DateTimeType(c.toInstant())); } catch (NumberFormatException e) { logger.error("Invalid time response (can't parse number): '{}'", resp); } @@ -936,14 +933,12 @@ class PrgProtocolHandler { final Calendar sunrise = Calendar.getInstance(); sunrise.set(Calendar.HOUR_OF_DAY, Integer.parseInt(m.group(1))); sunrise.set(Calendar.MINUTE, Integer.parseInt(m.group(2))); - phCallback.stateChanged(PrgConstants.CHANNEL_SUNRISE, - new DateTimeType(ZonedDateTime.ofInstant(sunrise.toInstant(), ZoneId.systemDefault()))); + phCallback.stateChanged(PrgConstants.CHANNEL_SUNRISE, new DateTimeType(sunrise.toInstant())); final Calendar sunset = Calendar.getInstance(); sunset.set(Calendar.HOUR_OF_DAY, Integer.parseInt(m.group(3))); sunset.set(Calendar.MINUTE, Integer.parseInt(m.group(4))); - phCallback.stateChanged(PrgConstants.CHANNEL_SUNSET, - new DateTimeType(ZonedDateTime.ofInstant(sunset.toInstant(), ZoneId.systemDefault()))); + phCallback.stateChanged(PrgConstants.CHANNEL_SUNSET, new DateTimeType(sunset.toInstant())); } catch (NumberFormatException e) { logger.error("Invalid sunrise/sunset response (can't parse number): '{}'", resp); } diff --git a/bundles/org.openhab.binding.lutron/src/main/java/org/openhab/binding/lutron/internal/handler/TimeclockHandler.java b/bundles/org.openhab.binding.lutron/src/main/java/org/openhab/binding/lutron/internal/handler/TimeclockHandler.java index 703442f3414..8708b661bea 100644 --- a/bundles/org.openhab.binding.lutron/src/main/java/org/openhab/binding/lutron/internal/handler/TimeclockHandler.java +++ b/bundles/org.openhab.binding.lutron/src/main/java/org/openhab/binding/lutron/internal/handler/TimeclockHandler.java @@ -14,8 +14,6 @@ package org.openhab.binding.lutron.internal.handler; import static org.openhab.binding.lutron.internal.LutronBindingConstants.*; -import java.time.ZoneId; -import java.time.ZonedDateTime; import java.util.Calendar; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -176,15 +174,13 @@ public class TimeclockHandler extends LutronHandler { } else if (parameters.length >= 2 && TimeclockCommand.ACTION_SUNRISE.toString().equals(parameters[0])) { Calendar calendar = parseLutronTime(parameters[1]); if (calendar != null) { - updateState(CHANNEL_SUNRISE, - new DateTimeType(ZonedDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault()))); + updateState(CHANNEL_SUNRISE, new DateTimeType(calendar.toInstant())); } } else if (parameters.length >= 2 && TimeclockCommand.ACTION_SUNSET.toString().equals(parameters[0])) { Calendar calendar = parseLutronTime(parameters[1]); if (calendar != null) { - updateState(CHANNEL_SUNSET, - new DateTimeType(ZonedDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault()))); + updateState(CHANNEL_SUNSET, new DateTimeType(calendar.toInstant())); } } else if (parameters.length >= 2 && TimeclockCommand.ACTION_EXECEVENT.toString().equals(parameters[0])) {