Simplify DateTimeType handling for WeMo

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-11-09 22:49:56 +01:00 committed by lsiepel
parent 82813cdcd4
commit 328e91a7fc
4 changed files with 6 additions and 17 deletions

View File

@ -15,8 +15,6 @@ package org.openhab.binding.wemo.internal;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
@ -116,12 +114,10 @@ public class InsightParser {
return UnDefType.UNDEF;
}
State lastChangedAtState = new DateTimeType(
ZonedDateTime.ofInstant(Instant.ofEpochSecond(lastChangedAt), ZoneId.systemDefault()));
if (lastChangedAt == 0) {
return UnDefType.UNDEF;
}
return lastChangedAtState;
return new DateTimeType(Instant.ofEpochSecond(lastChangedAt));
}
private State getNumber(String value) {

View File

@ -17,9 +17,7 @@ import static org.openhab.binding.wemo.internal.WemoUtil.*;
import java.io.StringReader;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@ -345,8 +343,7 @@ public class WemoCoffeeHandler extends WemoBaseThingHandler {
getThing().getUID());
return null;
}
ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochSecond(value), TimeZone.getDefault().toZoneId());
State dateTimeState = new DateTimeType(zoned);
State dateTimeState = new DateTimeType(Instant.ofEpochSecond(value));
logger.trace("New attribute brewed '{}' received", dateTimeState);
return dateTimeState;
}

View File

@ -16,12 +16,10 @@ import static org.openhab.binding.wemo.internal.WemoBindingConstants.*;
import static org.openhab.binding.wemo.internal.WemoUtil.*;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@ -502,8 +500,7 @@ public class WemoDimmerHandler extends WemoBaseThingHandler {
getThing().getUID());
return null;
}
ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochSecond(value), TimeZone.getDefault().toZoneId());
return new DateTimeType(zoned);
return new DateTimeType(Instant.ofEpochSecond(value));
}
public void setBinaryState(String action, String argument, String value) {

View File

@ -15,7 +15,6 @@ package org.openhab.binding.wemo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.time.ZoneId;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -47,7 +46,7 @@ public class InsightParserTest {
"1|1645800647|109676|80323|1196960|1209600|44|41400|30288361|483361410|8000");
Map<String, State> result = parser.parse();
assertEquals(OnOffType.ON, result.get(WemoBindingConstants.CHANNEL_STATE));
assertEquals(DateTimeType.valueOf("2022-02-25T15:50:47.000+0100").toZone(ZoneId.systemDefault()),
assertEquals(DateTimeType.valueOf("2022-02-25T15:50:47.000+0100"),
result.get(WemoBindingConstants.CHANNEL_LAST_CHANGED_AT));
assertEquals(new DecimalType(109_676), result.get(WemoBindingConstants.CHANNEL_LAST_ON_FOR));
assertEquals(new DecimalType(80_323), result.get(WemoBindingConstants.CHANNEL_ON_TODAY));
@ -69,7 +68,7 @@ public class InsightParserTest {
InsightParser parser = new InsightParser("8|1645967627|0|0|0|1209600|13|0|0|0.000000|8000");
Map<String, State> result = parser.parse();
assertEquals(OnOffType.ON, result.get(WemoBindingConstants.CHANNEL_STATE));
assertEquals(DateTimeType.valueOf("2022-02-27T14:13:47.000+0100").toZone(ZoneId.systemDefault()),
assertEquals(DateTimeType.valueOf("2022-02-27T14:13:47.000+0100"),
result.get(WemoBindingConstants.CHANNEL_LAST_CHANGED_AT));
assertEquals(new DecimalType(0), result.get(WemoBindingConstants.CHANNEL_LAST_ON_FOR));
assertEquals(new DecimalType(0), result.get(WemoBindingConstants.CHANNEL_ON_TODAY));
@ -92,7 +91,7 @@ public class InsightParserTest {
"1|1645800647|109676|80323|1196960|1209600|44|41400|30288361|483361410");
Map<String, State> result = parser.parse();
assertEquals(OnOffType.ON, result.get(WemoBindingConstants.CHANNEL_STATE));
assertEquals(DateTimeType.valueOf("2022-02-25T15:50:47.000+0100").toZone(ZoneId.systemDefault()),
assertEquals(DateTimeType.valueOf("2022-02-25T15:50:47.000+0100"),
result.get(WemoBindingConstants.CHANNEL_LAST_CHANGED_AT));
assertEquals(new DecimalType(109_676), result.get(WemoBindingConstants.CHANNEL_LAST_ON_FOR));
assertEquals(new DecimalType(80_323), result.get(WemoBindingConstants.CHANNEL_ON_TODAY));