2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.omnilink.internal.action;
15 import java.time.ZoneId;
16 import java.time.ZonedDateTime;
17 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.omnilink.internal.handler.OmnilinkBridgeHandler;
22 import org.openhab.core.automation.annotation.ActionInput;
23 import org.openhab.core.automation.annotation.RuleAction;
24 import org.openhab.core.i18n.TimeZoneProvider;
25 import org.openhab.core.thing.binding.ThingActions;
26 import org.openhab.core.thing.binding.ThingActionsScope;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * This is the action handler service for the synchronizeControllerTime action.
34 * @author Ethan Dye - Initial contribution
36 @ThingActionsScope(name = "omnilink")
38 public class OmnilinkActions implements ThingActions {
39 private final Logger logger = LoggerFactory.getLogger(OmnilinkActions.class);
40 public static Optional<TimeZoneProvider> timeZoneProvider = Optional.empty();
41 private @Nullable OmnilinkBridgeHandler handler;
44 public void setThingHandler(@Nullable ThingHandler handler) {
45 if (handler instanceof OmnilinkBridgeHandler bridgeHandler) {
46 this.handler = bridgeHandler;
51 public @Nullable ThingHandler getThingHandler() {
55 @RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
56 public void synchronizeControllerTime(
57 @ActionInput(name = "zone", label = "@text/actionInputZoneLabel", description = "@text/actionInputZoneDesc") @Nullable String zone) {
58 OmnilinkBridgeHandler actionsHandler = handler;
59 if (actionsHandler == null) {
60 logger.debug("Action service ThingHandler is null!");
63 if (ZoneId.getAvailableZoneIds().contains(zone)) {
64 zdt = ZonedDateTime.now(ZoneId.of(zone));
66 logger.debug("Time zone provided invalid, using system default!");
67 if (timeZoneProvider.isPresent()) {
68 zdt = ZonedDateTime.now(timeZoneProvider.get().getTimeZone());
70 zdt = ZonedDateTime.now(ZoneId.systemDefault());
73 actionsHandler.synchronizeControllerTime(zdt);
77 public static void synchronizeControllerTime(ThingActions actions, @Nullable String zone) {
78 ((OmnilinkActions) actions).synchronizeControllerTime(zone);
81 public static void setTimeZoneProvider(TimeZoneProvider tzp) {
82 timeZoneProvider = Optional.of(tzp);