2 * Copyright (c) 2010-2023 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.modbus.helioseasycontrols.internal;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.automation.annotation.ActionInput;
23 import org.openhab.core.automation.annotation.ActionOutput;
24 import org.openhab.core.automation.annotation.RuleAction;
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 class provides the actions available for the Helios device
34 * @author Bernhard Bauer - Initial contribution
36 @ThingActionsScope(name = "modbus.helioseasycontrols")
38 public class HeliosEasyControlsActions implements ThingActions {
40 private @Nullable HeliosEasyControlsHandler handler;
42 private final Logger logger = LoggerFactory.getLogger(HeliosEasyControlsActions.class);
45 public void setThingHandler(@Nullable ThingHandler handler) {
46 this.handler = (HeliosEasyControlsHandler) handler;
50 public @Nullable ThingHandler getThingHandler() {
54 private void triggerSwitch(String variableName) {
56 if (handler != null) {
57 handler.writeValue(variableName, "1");
59 } catch (HeliosException e) {
60 logger.warn("Error executing action triggering switch for variable {}: {}", variableName, e.getMessage());
61 } catch (InterruptedException e) {
63 "{} encountered Exception when trying to lock Semaphore for writing variable {} to the device: {}",
64 HeliosEasyControlsActions.class.getSimpleName(), variableName, e.getMessage());
68 @RuleAction(label = "@text/action.resetFilterChangeTimer.label", description = "@text/action.resetFilterChangeTimer.description")
69 public void resetFilterChangeTimer() {
70 triggerSwitch(HeliosEasyControlsBindingConstants.FILTER_CHANGE_RESET);
73 public static void resetFilterChangeTimer(ThingActions actions) {
74 ((HeliosEasyControlsActions) actions).resetFilterChangeTimer();
77 @RuleAction(label = "@text/action.resetErrors.label", description = "@text/action.resetErrors.description")
78 public void resetErrors() {
79 triggerSwitch(HeliosEasyControlsBindingConstants.RESET_FLAG);
82 public static void resetErrors(ThingActions actions) {
83 ((HeliosEasyControlsActions) actions).resetErrors();
86 @RuleAction(label = "@text/action.resetToFactoryDefaults.label", description = "@text/action.resetToFactoryDefaults.description")
87 public void resetToFactoryDefaults() {
88 triggerSwitch(HeliosEasyControlsBindingConstants.FACTORY_RESET);
91 public static void resetToFactoryDefaults(ThingActions actions) {
92 ((HeliosEasyControlsActions) actions).resetToFactoryDefaults();
95 @RuleAction(label = "@text/action.resetSwitchingTimes.label", description = "@text/action.resetSwitchingTimes.description")
96 public void resetSwitchingTimes() {
97 triggerSwitch(HeliosEasyControlsBindingConstants.FACTORY_SETTING_WZU);
100 public static void resetSwitchingTimes(ThingActions actions) {
101 ((HeliosEasyControlsActions) actions).resetSwitchingTimes();
104 @RuleAction(label = "@text/action.setSysDateTime.label", description = "@text/action.setSysDateTime.description")
105 public void setSysDateTime() {
106 HeliosEasyControlsHandler handler = this.handler;
107 if (handler != null) {
109 handler.setSysDateTime();
110 } catch (InterruptedException e) {
112 "{} encountered Exception when trying to lock Semaphore for setting system date and time on the device: {}",
113 HeliosEasyControlsActions.class.getSimpleName(), e.getMessage());
118 public static void setSysDateTime(ThingActions actions) {
119 ((HeliosEasyControlsActions) actions).setSysDateTime();
122 private void setBypass(boolean from, int day, int month) {
123 HeliosEasyControlsHandler handler = this.handler;
124 if (handler != null) {
126 handler.setBypass(from, day, month);
127 } catch (InterruptedException e) {
129 "{} encountered Exception when trying to lock Semaphore for setting bypass date on the device: {}",
130 HeliosEasyControlsActions.class.getSimpleName(), e.getMessage());
135 @RuleAction(label = "@text/action.setBypassFrom.label", description = "@text/action.setBypassFrom.description")
136 public void setBypassFrom(
137 @ActionInput(name = "day", label = "@text/action.setBypassFrom.inputParams.day.label", description = "@text/action.setBypassFrom.inputParams.day.description") int day,
138 @ActionInput(name = "month", label = "@text/action.setBypassFrom.inputParams.month.label", description = "@text/action.setBypassFrom.inputParams.month.description") int month) {
139 setBypass(true, day, month);
142 public static void setBypassFrom(ThingActions actions, int day, int month) {
143 ((HeliosEasyControlsActions) actions).setBypassFrom(day, month);
146 @RuleAction(label = "@text/action.setBypassTo.label", description = "@text/action.setBypassTo.description")
147 public void setBypassTo(
148 @ActionInput(name = "day", label = "@text/action.setBypassTo.inputParams.day.label", description = "@text/action.setBypassTo.inputParams.day.description") int day,
149 @ActionInput(name = "month", label = "@text/action.setBypassTo.inputParams.day.label", description = "@text/action.setBypassTo.inputParams.day.description") int month) {
150 setBypass(false, day, month);
153 public static void setBypassTo(ThingActions actions, int day, int month) {
154 ((HeliosEasyControlsActions) actions).setBypassTo(day, month);
157 @RuleAction(label = "@text/action.getErrorMessages.label", description = "@text/action.getErrorMessages.description")
158 public @ActionOutput(name = "errorMessages", type = "java.util.List<String>") List<String> getErrorMessages() {
159 return (handler != null) ? handler.getErrorMessages() : new ArrayList<String>();
162 public static List<String> getErrorMessages(ThingActions actions) {
163 return ((HeliosEasyControlsActions) actions).getErrorMessages();
166 @RuleAction(label = "@text/action.getWarningMessages.label", description = "@text/action.getWarningMessages.description")
167 public @ActionOutput(name = "warningMessages", type = "java.util.List<String>") List<String> getWarningMessages() {
168 return (handler != null) ? handler.getWarningMessages() : new ArrayList<String>();
171 public static List<String> getWarningMessages(ThingActions actions) {
172 return ((HeliosEasyControlsActions) actions).getWarningMessages();
175 @RuleAction(label = "@text/action.getInfoMessages.label", description = "@text/action.getInfoMessages.description")
176 public @ActionOutput(name = "infoMessages", type = "java.util.List<String>") List<String> getInfoMessages() {
177 return (handler != null) ? handler.getInfoMessages() : new ArrayList<String>();
180 public static List<String> getInfoMessages(ThingActions actions) {
181 return ((HeliosEasyControlsActions) actions).getInfoMessages();
184 @RuleAction(label = "@text/action.getStatusMessages.label", description = "@text/action.getStatusMessages.description")
185 public @ActionOutput(name = "statusMessages", type = "java.util.List<String>") List<String> getStatusMessages() {
186 return (handler != null) ? handler.getStatusMessages() : new ArrayList<String>();
189 public static List<String> getStatusMessages(ThingActions actions) {
190 return ((HeliosEasyControlsActions) actions).getStatusMessages();
193 @RuleAction(label = "@text/action.getMessages.label", description = "@text/action.getMessages.description")
194 public @ActionOutput(name = "errorMessages", type = "java.util.List<String>") @ActionOutput(name = "warningMessages", type = "java.util.List<String>") @ActionOutput(name = "infoMessages", type = "java.util.List<String>") @ActionOutput(name = "statusMessages", type = "java.util.List<String>") Map<String, Object> getMessages() {
195 Map<String, Object> messages = new HashMap<>();
196 HeliosEasyControlsHandler handler = this.handler;
197 if (handler != null) {
198 messages.put("errorMessages", handler.getErrorMessages());
199 messages.put("warningMessages", handler.getWarningMessages());
200 messages.put("infoMessages", handler.getInfoMessages());
201 messages.put("statusMessages", handler.getStatusMessages());
206 public static Map<String, Object> getMessages(ThingActions actions) {
207 return ((HeliosEasyControlsActions) actions).getMessages();