From a963c79e8b87710a53dc5966fe8ac47bc07c1306 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Tue, 13 Sep 2022 21:53:52 +0200 Subject: [PATCH] Consider TimeZoneProvider to build states for time-stamp channels (#13386) Signed-off-by: Christoph Weitkamp --- bundles/org.openhab.binding.groupepsa/README.md | 2 +- .../groupepsa/internal/GroupePSAHandlerFactory.java | 12 ++++++++---- .../groupepsa/internal/things/GroupePSAHandler.java | 10 +++++++--- .../internal/things/GroupePSAHandlerTest.java | 7 +++++-- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/bundles/org.openhab.binding.groupepsa/README.md b/bundles/org.openhab.binding.groupepsa/README.md index 58a825641..60a2e426a 100644 --- a/bundles/org.openhab.binding.groupepsa/README.md +++ b/bundles/org.openhab.binding.groupepsa/README.md @@ -70,7 +70,7 @@ The Polling Interval and Online Timeout can be adjusted. | emergency | String | Emergency call status | | service | String | Service Type | | preconditioning | String | Air conditioning status | -| preconditioningFailure | String | Airr conditioning failure cause | +| preconditioningFailure | String | Air conditioning failure cause | | level | Number:Dimensionless | Fuel level | | autonomy | Number:Length | Remaining distance | | consumption | Number:VolumetricFlowRate | Fuel consumption | diff --git a/bundles/org.openhab.binding.groupepsa/src/main/java/org/openhab/binding/groupepsa/internal/GroupePSAHandlerFactory.java b/bundles/org.openhab.binding.groupepsa/src/main/java/org/openhab/binding/groupepsa/internal/GroupePSAHandlerFactory.java index 4db404a27..ad4ef0775 100644 --- a/bundles/org.openhab.binding.groupepsa/src/main/java/org/openhab/binding/groupepsa/internal/GroupePSAHandlerFactory.java +++ b/bundles/org.openhab.binding.groupepsa/src/main/java/org/openhab/binding/groupepsa/internal/GroupePSAHandlerFactory.java @@ -22,6 +22,7 @@ import org.eclipse.jetty.client.HttpClient; import org.openhab.binding.groupepsa.internal.bridge.GroupePSABridgeHandler; import org.openhab.binding.groupepsa.internal.things.GroupePSAHandler; import org.openhab.core.auth.client.oauth2.OAuthFactory; +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; @@ -46,13 +47,16 @@ public class GroupePSAHandlerFactory extends BaseThingHandlerFactory { private static final Set SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_BRIDGE, THING_TYPE_VEHICLE); private final OAuthFactory oAuthFactory; - protected final HttpClient httpClient; + private final HttpClient httpClient; + private final TimeZoneProvider timeZoneProvider; @Activate - public GroupePSAHandlerFactory(@Reference OAuthFactory oAuthFactory, - @Reference HttpClientFactory httpClientFactory) { + public GroupePSAHandlerFactory(final @Reference OAuthFactory oAuthFactory, // + final @Reference HttpClientFactory httpClientFactory, // + final @Reference TimeZoneProvider timeZoneProvider) { this.oAuthFactory = oAuthFactory; this.httpClient = httpClientFactory.getCommonHttpClient(); + this.timeZoneProvider = timeZoneProvider; } @Override @@ -65,7 +69,7 @@ public class GroupePSAHandlerFactory extends BaseThingHandlerFactory { ThingTypeUID thingTypeUID = thing.getThingTypeUID(); if (THING_TYPE_VEHICLE.equals(thingTypeUID)) { - return new GroupePSAHandler(thing); + return new GroupePSAHandler(thing, timeZoneProvider); } else if (THING_TYPE_BRIDGE.equals(thingTypeUID)) { return new GroupePSABridgeHandler((Bridge) thing, oAuthFactory, httpClient); } diff --git a/bundles/org.openhab.binding.groupepsa/src/main/java/org/openhab/binding/groupepsa/internal/things/GroupePSAHandler.java b/bundles/org.openhab.binding.groupepsa/src/main/java/org/openhab/binding/groupepsa/internal/things/GroupePSAHandler.java index 30a1b706c..cf5fa4b43 100644 --- a/bundles/org.openhab.binding.groupepsa/src/main/java/org/openhab/binding/groupepsa/internal/things/GroupePSAHandler.java +++ b/bundles/org.openhab.binding.groupepsa/src/main/java/org/openhab/binding/groupepsa/internal/things/GroupePSAHandler.java @@ -50,6 +50,7 @@ import org.openhab.binding.groupepsa.internal.rest.api.dto.Safety; import org.openhab.binding.groupepsa.internal.rest.api.dto.Service; import org.openhab.binding.groupepsa.internal.rest.api.dto.VehicleStatus; import org.openhab.binding.groupepsa.internal.rest.exceptions.GroupePSACommunicationException; +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.OpenClosedType; @@ -92,6 +93,8 @@ public class GroupePSAHandler extends BaseThingHandler { private final Logger logger = LoggerFactory.getLogger(GroupePSAHandler.class); + private final TimeZoneProvider timeZoneProvider; + private @Nullable String id = null; private long lastQueryTimeNs = 0L; @@ -99,8 +102,9 @@ public class GroupePSAHandler extends BaseThingHandler { private long maxQueryFrequencyNanos = TimeUnit.MINUTES.toNanos(1); private long onlineIntervalM; - public GroupePSAHandler(Thing thing) { + public GroupePSAHandler(Thing thing, TimeZoneProvider timeZoneProvider) { super(thing); + this.timeZoneProvider = timeZoneProvider; } @Override @@ -295,7 +299,7 @@ public class GroupePSAHandler extends BaseThingHandler { if (lastPosition != null) { Geometry geometry = lastPosition.getGeometry(); if (geometry != null) { - SinglePosition position = (SinglePosition) geometry.positions(); + SinglePosition position = geometry.positions(); if (Double.isFinite(position.alt())) { updateState(CHANNEL_POSITION_POSITION, new PointType(new DecimalType(position.lat()), new DecimalType(position.lon()), new DecimalType(position.alt()))); @@ -422,7 +426,7 @@ public class GroupePSAHandler extends BaseThingHandler { protected void updateState(String channelID, @Nullable ZonedDateTime date) { if (date != null) { - updateState(channelID, new DateTimeType(date)); + updateState(channelID, new DateTimeType(date).toZone(timeZoneProvider.getTimeZone())); } else { updateState(channelID, UnDefType.UNDEF); } diff --git a/bundles/org.openhab.binding.groupepsa/src/test/java/org/openhab/binding/groupepsa/internal/things/GroupePSAHandlerTest.java b/bundles/org.openhab.binding.groupepsa/src/test/java/org/openhab/binding/groupepsa/internal/things/GroupePSAHandlerTest.java index bb672f80a..d1609b5af 100644 --- a/bundles/org.openhab.binding.groupepsa/src/test/java/org/openhab/binding/groupepsa/internal/things/GroupePSAHandlerTest.java +++ b/bundles/org.openhab.binding.groupepsa/src/test/java/org/openhab/binding/groupepsa/internal/things/GroupePSAHandlerTest.java @@ -18,6 +18,7 @@ import static org.mockito.Mockito.*; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; +import java.time.ZoneId; import java.util.stream.Collectors; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -34,6 +35,7 @@ import org.openhab.binding.groupepsa.internal.rest.api.GroupePSAConnectApi; import org.openhab.binding.groupepsa.internal.rest.exceptions.GroupePSACommunicationException; import org.openhab.core.auth.client.oauth2.OAuthFactory; import org.openhab.core.config.core.Configuration; +import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.StringType; import org.openhab.core.thing.Bridge; @@ -65,6 +67,7 @@ public class GroupePSAHandlerTest { private @NonNullByDefault({}) @Mock OAuthFactory oAuthFactory; private @NonNullByDefault({}) @Mock HttpClient httpClient; + private @NonNullByDefault({}) @Mock TimeZoneProvider timeZoneProvider; static String getResourceFileAsString(String fileName) throws GroupePSACommunicationException { try (InputStream is = GroupePSAConnectApi.class.getResourceAsStream(fileName)) { @@ -82,13 +85,12 @@ public class GroupePSAHandlerTest { } @BeforeEach - @SuppressWarnings("null") public void setUp() throws GroupePSACommunicationException { closeable = MockitoAnnotations.openMocks(this); // Create real objects bridgeHandler = spy(new GroupePSABridgeHandler(bridge, oAuthFactory, httpClient)); - thingHandler = spy(new GroupePSAHandler(thing)); + thingHandler = spy(new GroupePSAHandler(thing, timeZoneProvider)); api = spy(new GroupePSAConnectApi(httpClient, bridgeHandler, "clientId", "realm")); // Setup API mock @@ -122,6 +124,7 @@ public class GroupePSAHandlerTest { thingHandler.setCallback(thingCallback); doReturn(bridge).when(thingHandler).getBridge(); doNothing().when(thingHandler).buildDoorChannels(any()); + doReturn(ZoneId.systemDefault()).when(timeZoneProvider).getTimeZone(); } @AfterEach