]> git.basschouten.com Git - openhab-addons.git/blob
fc2199002ada9b8db68758168dfeffdf7c4b7e34
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.io.hueemulation.internal.automation;
14
15 import java.util.Map;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.automation.Action;
20 import org.openhab.core.automation.RuleRegistry;
21 import org.openhab.core.automation.handler.ActionHandler;
22 import org.openhab.core.automation.handler.BaseModuleHandler;
23 import org.openhab.core.config.core.Configuration;
24
25 /**
26  * This action module type allows to remove a rule from the rule registry.
27  * <p>
28  * This is very useful for rules that should execute only once etc.
29  * 
30  * @author David Graeff - Initial contribution
31  */
32 @NonNullByDefault
33 public class RemoveRuleActionHandler extends BaseModuleHandler<Action> implements ActionHandler {
34     public static final String MODULE_TYPE_ID = "rules.RemoveRuleAction";
35     public static final String CALLBACK_CONTEXT_NAME = "CALLBACK";
36     public static final String MODULE_CONTEXT_NAME = "MODULE";
37
38     public static final String CFG_REMOVE_UID = "removeuid";
39     private final String ruleUID;
40
41     private RuleRegistry ruleRegistry;
42
43     @SuppressWarnings({ "null", "unused" })
44     public RemoveRuleActionHandler(final Action module, RuleRegistry ruleRegistry) {
45         super(module);
46         this.ruleRegistry = ruleRegistry;
47         final Configuration config = module.getConfiguration();
48         if (config.getProperties().isEmpty()) {
49             throw new IllegalArgumentException("'Configuration' can not be empty.");
50         }
51
52         ruleUID = (String) config.get(CFG_REMOVE_UID);
53         if (ruleUID == null) {
54             throw new IllegalArgumentException("'ruleUIDs' property must not be null.");
55         }
56     }
57
58     @Override
59     public @Nullable Map<String, Object> execute(Map<String, Object> context) {
60         ruleRegistry.remove(ruleUID);
61         return null;
62     }
63 }