]> git.basschouten.com Git - openhab-addons.git/blob
ffa561ce94e6ef15880c8103703e9961e57bf4ac
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.modbus.helioseasycontrols.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.core.automation.annotation.ActionInput;
18 import org.openhab.core.automation.annotation.RuleAction;
19 import org.openhab.core.thing.binding.ThingActions;
20 import org.openhab.core.thing.binding.ThingActionsScope;
21 import org.openhab.core.thing.binding.ThingHandler;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * This class provides the actions available for the Helios device
27  *
28  * @author Bernhard Bauer - Initial contribution
29  */
30 @ThingActionsScope(name = "modbus.helioseasycontrols")
31 @NonNullByDefault
32 public class HeliosEasyControlsActions implements ThingActions {
33
34     private @Nullable HeliosEasyControlsHandler handler;
35
36     private final Logger logger = LoggerFactory.getLogger(HeliosEasyControlsActions.class);
37
38     @Override
39     public void setThingHandler(@Nullable ThingHandler handler) {
40         this.handler = (HeliosEasyControlsHandler) handler;
41     }
42
43     @Override
44     public @Nullable ThingHandler getThingHandler() {
45         return handler;
46     }
47
48     private void triggerSwitch(String variableName) {
49         try {
50             if (this.handler != null) {
51                 this.handler.writeValue(variableName, "1");
52             }
53         } catch (HeliosException e) {
54             logger.warn("Error executing action 'resetFilterChangeTimer': {}", e.getMessage());
55         }
56     }
57
58     @RuleAction(label = "Reset filter change timer", description = "Sets the filter change timer back to the configured interval")
59     public void resetFilterChangeTimer() {
60         this.triggerSwitch(HeliosEasyControlsBindingConstants.FILTER_CHANGE_RESET);
61     }
62
63     public static void resetFilterChangeTimer(@Nullable ThingActions actions) {
64         if (actions instanceof HeliosEasyControlsActions) {
65             ((HeliosEasyControlsActions) actions).resetFilterChangeTimer();
66         } else {
67             throw new IllegalArgumentException("Instance is not an HeliosEasyControlsActions class.");
68         }
69     }
70
71     @RuleAction(label = "Reset error messages", description = "Reset error/warning/info messages")
72     public void resetErrors() {
73         this.triggerSwitch(HeliosEasyControlsBindingConstants.RESET_FLAG);
74     }
75
76     public static void resetErrors(@Nullable ThingActions actions) {
77         if (actions instanceof HeliosEasyControlsActions) {
78             ((HeliosEasyControlsActions) actions).resetErrors();
79         } else {
80             throw new IllegalArgumentException("Instance is not an HeliosEasyControlsActions class.");
81         }
82     }
83
84     @RuleAction(label = "Reset to factory defaults", description = "Reset device to factory defaults")
85     public void resetToFactoryDefaults() {
86         this.triggerSwitch(HeliosEasyControlsBindingConstants.FACTORY_RESET);
87     }
88
89     public static void resetToFactoryDefaults(@Nullable ThingActions actions) {
90         if (actions instanceof HeliosEasyControlsActions) {
91             ((HeliosEasyControlsActions) actions).resetToFactoryDefaults();
92         } else {
93             throw new IllegalArgumentException("Instance is not an HeliosEasyControlsActions class.");
94         }
95     }
96
97     @RuleAction(label = "Reset individual switching times", description = "Reset individual switching times")
98     public void resetSwitchingTimes() {
99         this.triggerSwitch(HeliosEasyControlsBindingConstants.FACTORY_SETTING_WZU);
100     }
101
102     public static void resetSwitchingTimes(@Nullable ThingActions actions) {
103         if (actions instanceof HeliosEasyControlsActions) {
104             ((HeliosEasyControlsActions) actions).resetSwitchingTimes();
105         } else {
106             throw new IllegalArgumentException("Instance is not an HeliosEasyControlsActions class.");
107         }
108     }
109
110     @RuleAction(label = "Set system date and time", description = "Sets the device's system date and time based on OH's system date and time")
111     public void setSysDateTime() {
112         HeliosEasyControlsHandler handler = this.handler;
113         if (handler != null) {
114             handler.setSysDateTime();
115         }
116     }
117
118     public static void setSysDateTime(@Nullable ThingActions actions) {
119         if (actions instanceof HeliosEasyControlsActions) {
120             ((HeliosEasyControlsActions) actions).setSysDateTime();
121         } else {
122             throw new IllegalArgumentException("Instance is not an HeliosEasyControlsActions class.");
123         }
124     }
125
126     private void setBypass(boolean from, int day, int month) {
127         HeliosEasyControlsHandler handler = this.handler;
128         if (handler != null) {
129             handler.setBypass(from, day, month);
130         }
131     }
132
133     @RuleAction(label = "Set the bypass from day and month", description = "Sets the day and month from when the bypass should be active")
134     public void setBypassFrom(
135             @ActionInput(name = "day", label = "bypass from day", description = "The day from when the bypass should be active") int day,
136             @ActionInput(name = "month", label = "bypass from month", description = "The month from when the bypass should be active") int month) {
137         this.setBypass(true, day, month);
138     }
139
140     public static void setBypassFrom(@Nullable ThingActions actions, int day, int month) {
141         if (actions instanceof HeliosEasyControlsActions) {
142             ((HeliosEasyControlsActions) actions).setBypassFrom(day, month);
143         } else {
144             throw new IllegalArgumentException("Instance is not an HeliosEasyControlsActions class.");
145         }
146     }
147
148     @RuleAction(label = "Set the bypass to day and month", description = "Sets the day and month until when the bypass should be active")
149     public void setBypassTo(
150             @ActionInput(name = "day", label = "bypass to day", description = "The day until when the bypass should be active") int day,
151             @ActionInput(name = "month", label = "bypass to month", description = "The month until when the bypass should be active") int month) {
152         this.setBypass(false, day, month);
153     }
154
155     public static void setBypassTo(@Nullable ThingActions actions, int day, int month) {
156         if (actions instanceof HeliosEasyControlsActions) {
157             ((HeliosEasyControlsActions) actions).setBypassTo(day, month);
158         } else {
159             throw new IllegalArgumentException("Instance is not an HeliosEasyControlsActions class.");
160         }
161     }
162 }