Simplify DateTimeType handling for Automower

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-11-09 19:50:21 +01:00 committed by lsiepel
parent e3a1abd191
commit 0ee5fd952e
2 changed files with 7 additions and 28 deletions

View File

@ -27,7 +27,6 @@ import org.openhab.binding.automower.internal.discovery.AutomowerDiscoveryServic
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 class AutomowerHandlerFactory extends BaseThingHandlerFactory {
}
if (AutomowerHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
return new AutomowerHandler(thing, timeZoneProvider);
return new AutomowerHandler(thing);
}
return null;

View File

@ -15,7 +15,6 @@ package org.openhab.binding.automower.internal.things;
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.Posi
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<String> 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 @@ public class AutomowerHandler extends BaseThingHandler {
}
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 @@ public class AutomowerHandler extends BaseThingHandler {
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 @@ public class AutomowerHandler extends BaseThingHandler {
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 @@ public class AutomowerHandler extends BaseThingHandler {
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());
}
}