Simplify DateTimeType handling for Lutron

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Jacob Laursen 2024-11-17 21:11:16 +01:00 committed by Ciprian Pascu
parent 665a4e752f
commit 6e90350c1f
3 changed files with 8 additions and 16 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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])) {