]> git.basschouten.com Git - openhab-addons.git/blob
311f05cacecbb99ec7ab62d3862c8b84045ab411
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.openhabcloud.internal.actions;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Locale;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.automation.Visibility;
23 import org.openhab.core.automation.type.ActionType;
24 import org.openhab.core.automation.type.ModuleType;
25 import org.openhab.core.automation.type.ModuleTypeProvider;
26 import org.openhab.core.common.registry.ProviderChangeListener;
27 import org.openhab.core.config.core.ConfigDescriptionParameter;
28 import org.openhab.core.config.core.ConfigDescriptionParameter.Type;
29 import org.openhab.core.config.core.ConfigDescriptionParameterBuilder;
30 import org.osgi.service.component.annotations.Component;
31
32 /**
33  * This class provides a {@link ModuleTypeProvider} implementation to provide actions to send notifications via
34  * openHAB Cloud.
35  *
36  * @author Christoph Weitkamp - Initial contribution
37  * @author Dan Cunningham - Extended notification enhancements
38  */
39 @NonNullByDefault
40 @Component(service = ModuleTypeProvider.class)
41 public class NotificationActionTypeProvider implements ModuleTypeProvider {
42
43     private static final ModuleType SEND_NOTIFICATION_ACTION = new ActionType(SendNotificationActionHandler.TYPE_ID,
44             getNotificationConfig(ConfigType.NOT_EXTENDED, true, null), "send a notification",
45             "Sends a notification to a specific cloud user.", null, Visibility.VISIBLE, null, null);
46
47     private static final ModuleType SEND_EXTENDED_NOTIFICATION_ACTION = new ActionType(
48             SendNotificationActionHandler.EXTENDED_TYPE_ID, getNotificationConfig(ConfigType.EXTENDED, true, null),
49             "send a notification with icon and severity (tag)",
50             "Sends a notification to a specific cloud user. Optionally add an icon or the severity (tag).", null,
51             Visibility.VISIBLE, null, null);
52
53     private static final ModuleType SEND_EXTENDED2_NOTIFICATION_ACTION = new ActionType(
54             SendNotificationActionHandler.EXTENDED2_TYPE_ID, getNotificationConfig(ConfigType.EXTENDED2, true, null),
55             "send a notification with icon, tag, title, reference id, click action, media attachment and action buttons",
56             "Sends a notification to a specific cloud user. Optionally add an icon, tag, title, reference id, on click action, media to attach, and up to 3 action buttons with a format of \"Title=Action\".",
57             null, Visibility.VISIBLE, null, null);
58
59     private static final ModuleType SEND_BROADCAST_NOTIFICATION_ACTION = new ActionType(
60             SendBroadcastNotificationActionHandler.TYPE_ID, getNotificationConfig(ConfigType.NOT_EXTENDED, true, null),
61             "broadcast a notification", "Sends a notification to all devices of all users.", null, Visibility.VISIBLE,
62             null, null);
63
64     private static final ModuleType SEND_EXTENDED_BROADCAST_NOTIFICATION_ACTION = new ActionType(
65             SendBroadcastNotificationActionHandler.EXTENDED_TYPE_ID,
66             getNotificationConfig(ConfigType.EXTENDED, false, null),
67             "broadcast a notification with icon and severity (tag)",
68             "Sends a notification to all devices of all users. Optionally add an icon or the severity (tag).", null,
69             Visibility.VISIBLE, null, null);
70
71     private static final ModuleType SEND_EXTENDED2_BROADCAST_NOTIFICATION_ACTION = new ActionType(
72             SendBroadcastNotificationActionHandler.EXTENDED2_TYPE_ID,
73             getNotificationConfig(ConfigType.EXTENDED2, false, null),
74             "broadcast a notification with with icon, tag, title, reference id, on click action, media attachment and action buttons",
75             "Sends a notification to all devices of all users. Optionally add an icon, tag, title, reference id, click action, media to attach, and up to 3 action buttons with a format of \"Title=Action\".",
76             null, Visibility.VISIBLE, null, null);
77
78     private static final ModuleType SEND_LOG_NOTIFICATION_ACTION = new ActionType(
79             SendLogNotificationActionHandler.TYPE_ID, getNotificationConfig(ConfigType.NOT_EXTENDED, false, null),
80             "send a log message",
81             "Sends a log notification to the openHAB Cloud instance. Notifications are NOT sent to any registered devices.",
82             null, Visibility.VISIBLE, null, null);
83
84     private static final ModuleType SEND_EXTENDED_LOG_NOTIFICATION_ACTION = new ActionType(
85             SendLogNotificationActionHandler.EXTENDED_TYPE_ID, getNotificationConfig(ConfigType.EXTENDED, false, null),
86             "send a log message with icon and severity (tag)",
87             "Sends a log notification to the openHAB Cloud instance. Optionally add an icon or the severity (tag). Notifications are NOT sent to any registered devices.",
88             null, Visibility.VISIBLE, null, null);
89
90     private static final ModuleType HIDE_NOTIFICATION_BY_REFERENCE_ID_ACTION = new ActionType(
91             HideNotificationByReferenceIdActionHandler.TYPE_ID,
92             getNotificationConfig(ConfigType.HIDE_BY_REF, true, null),
93             "hide notifications by reference id for all devices of a specific user",
94             "Hide notifications by reference id for all devices of a specific user.", null, Visibility.VISIBLE, null,
95             null);
96
97     private static final ModuleType HIDE_BROADCAST_NOTIFICATION_BY_REFERENCE_ID_ACTION = new ActionType(
98             HideBroadcastNotificationByReferenceIdActionHandler.TYPE_ID,
99             getNotificationConfig(ConfigType.HIDE_BY_REF, false, null),
100             "hide notifications by reference id for all users and devices",
101             "Hide notifications by reference id for all users and devices.", null, Visibility.VISIBLE, null, null);
102
103     private static final ModuleType HIDE_NOTIFICATION_BY_TAG_ACTION = new ActionType(
104             HideNotificationByTagActionHandler.TYPE_ID, getNotificationConfig(ConfigType.HIDE_BY_TAG, true, null),
105             "hide notifications by tag for all devices of a specific user",
106             "Hide notifications by tag id for all devices of a specific user.", null, Visibility.VISIBLE, null, null);
107
108     private static final ModuleType HIDE_BROADCAST_NOTIFICATION_BY_TAG_ACTION = new ActionType(
109             HideBroadcastNotificationByTagActionHandler.TYPE_ID,
110             getNotificationConfig(ConfigType.HIDE_BY_TAG, false, null),
111             "hide notifications by tag id for all users and devices",
112             "Hide notifications by tag id for all users and devices.", null, Visibility.VISIBLE, null, null);
113
114     private static final List<ModuleType> MODULE_TYPES = List.of(SEND_NOTIFICATION_ACTION,
115             SEND_EXTENDED_NOTIFICATION_ACTION, SEND_EXTENDED2_NOTIFICATION_ACTION, SEND_BROADCAST_NOTIFICATION_ACTION,
116             SEND_EXTENDED_BROADCAST_NOTIFICATION_ACTION, SEND_EXTENDED2_BROADCAST_NOTIFICATION_ACTION,
117             SEND_LOG_NOTIFICATION_ACTION, SEND_EXTENDED_LOG_NOTIFICATION_ACTION,
118             HIDE_BROADCAST_NOTIFICATION_BY_REFERENCE_ID_ACTION, HIDE_BROADCAST_NOTIFICATION_BY_TAG_ACTION,
119             HIDE_NOTIFICATION_BY_REFERENCE_ID_ACTION, HIDE_NOTIFICATION_BY_TAG_ACTION);
120
121     @SuppressWarnings("unchecked")
122     @Override
123     public @Nullable ModuleType getModuleType(String UID, @Nullable Locale locale) {
124         switch (UID) {
125             case SendNotificationActionHandler.TYPE_ID:
126                 return SEND_NOTIFICATION_ACTION;
127             case SendNotificationActionHandler.EXTENDED_TYPE_ID:
128                 return SEND_EXTENDED_NOTIFICATION_ACTION;
129             case SendNotificationActionHandler.EXTENDED2_TYPE_ID:
130                 return SEND_EXTENDED2_NOTIFICATION_ACTION;
131             case SendBroadcastNotificationActionHandler.TYPE_ID:
132                 return SEND_BROADCAST_NOTIFICATION_ACTION;
133             case SendBroadcastNotificationActionHandler.EXTENDED_TYPE_ID:
134                 return SEND_EXTENDED_BROADCAST_NOTIFICATION_ACTION;
135             case SendBroadcastNotificationActionHandler.EXTENDED2_TYPE_ID:
136                 return SEND_EXTENDED2_BROADCAST_NOTIFICATION_ACTION;
137             case SendLogNotificationActionHandler.TYPE_ID:
138                 return SEND_LOG_NOTIFICATION_ACTION;
139             case SendLogNotificationActionHandler.EXTENDED_TYPE_ID:
140                 return SEND_EXTENDED_LOG_NOTIFICATION_ACTION;
141             case HideNotificationByTagActionHandler.TYPE_ID:
142                 return HIDE_NOTIFICATION_BY_TAG_ACTION;
143             case HideBroadcastNotificationByTagActionHandler.TYPE_ID:
144                 return HIDE_BROADCAST_NOTIFICATION_BY_TAG_ACTION;
145             case HideNotificationByReferenceIdActionHandler.TYPE_ID:
146                 return HIDE_NOTIFICATION_BY_REFERENCE_ID_ACTION;
147             case HideBroadcastNotificationByReferenceIdActionHandler.TYPE_ID:
148                 return HIDE_BROADCAST_NOTIFICATION_BY_REFERENCE_ID_ACTION;
149             default:
150                 return null;
151         }
152     }
153
154     @Override
155     public Collection<ModuleType> getAll() {
156         return MODULE_TYPES;
157     }
158
159     @Override
160     public Collection<ModuleType> getModuleTypes(@Nullable Locale locale) {
161         return MODULE_TYPES;
162     }
163
164     private static List<ConfigDescriptionParameter> getNotificationConfig(ConfigType type, boolean userRequired,
165             @Nullable Locale locale) {
166         List<ConfigDescriptionParameter> params = new ArrayList<>();
167
168         if (userRequired) {
169             params.add(ConfigDescriptionParameterBuilder.create(SendNotificationActionHandler.PARAM_USER, Type.TEXT)
170                     .withRequired(true).withLabel("User Id").withDescription("The cloud user id of the recipient.")
171                     .build());
172         }
173         switch (type) {
174             case EXTENDED:
175                 params.add(getMessageConfigParameter(locale));
176                 params.add(getIconConfigParameter(locale));
177                 params.add(getSeverityConfigParameter(locale));
178                 break;
179             case EXTENDED2:
180                 params.add(getMessageConfigParameter(locale));
181                 params.add(getIconConfigParameter(locale));
182                 params.add(getTagConfigParameter(locale));
183                 params.add(getTitleConfigParameter(locale));
184                 params.add(getReferenceIdConfigParameter(locale));
185                 params.add(getonClickActionConfigParameter(locale));
186                 params.add(getMediaAttachmentUrlConfigParameter(locale));
187                 params.add(getActionButton1ConfigParameter(locale));
188                 params.add(getActionButton2ConfigParameter(locale));
189                 params.add(getActionButton3ConfigParameter(locale));
190                 break;
191             case HIDE_BY_TAG:
192                 params.add(getTagConfigParameter(locale));
193                 break;
194             case HIDE_BY_REF:
195                 params.add(getReferenceIdConfigParameter(locale));
196                 break;
197             default:
198                 break;
199         }
200         return params;
201     }
202
203     private static ConfigDescriptionParameter getMessageConfigParameter(@Nullable Locale locale) {
204         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_MESSAGE, Type.TEXT)
205                 .withRequired(true).withLabel("Message").withDescription("The body of the notification.").build();
206     }
207
208     private static ConfigDescriptionParameter getIconConfigParameter(@Nullable Locale locale) {
209         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_ICON, Type.TEXT)
210                 .withLabel("Icon").withDescription("The icon of the notification.").build();
211     }
212
213     private static ConfigDescriptionParameter getSeverityConfigParameter(@Nullable Locale locale) {
214         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_SEVERITY, Type.TEXT)
215                 .withLabel("Severity (Tag)").withDescription("The severity/tag of the notification.").build();
216     }
217
218     private static ConfigDescriptionParameter getTagConfigParameter(@Nullable Locale locale) {
219         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_TAG, Type.TEXT)
220                 .withLabel("Tag").withDescription("The tag of the notification.").build();
221     }
222
223     private static ConfigDescriptionParameter getTitleConfigParameter(@Nullable Locale locale) {
224         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_TITLE, Type.TEXT)
225                 .withLabel("Title").withDescription("The title of the notification.").build();
226     }
227
228     private static ConfigDescriptionParameter getReferenceIdConfigParameter(@Nullable Locale locale) {
229         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_REFERENCE_ID, Type.TEXT)
230                 .withLabel("Reference Id").withDescription("A reference Id for the notification.").build();
231     }
232
233     private static ConfigDescriptionParameter getonClickActionConfigParameter(@Nullable Locale locale) {
234         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_ON_CLICK_ACTION, Type.TEXT)
235                 .withLabel("On Click Action").withDescription("The action to perform when clicked.").build();
236     }
237
238     private static ConfigDescriptionParameter getMediaAttachmentUrlConfigParameter(@Nullable Locale locale) {
239         return ConfigDescriptionParameterBuilder
240                 .create(BaseNotificationActionHandler.PARAM_MEDIA_ATTACHMENT_URL, Type.TEXT)
241                 .withLabel("Media Attachment URL").withDescription("The media to attach to a notification.").build();
242     }
243
244     private static ConfigDescriptionParameter getActionButton1ConfigParameter(@Nullable Locale locale) {
245         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_ACTION_BUTTON_1, Type.TEXT)
246                 .withLabel("Action Button 1").withDescription("An action button in the format \"Title=Action\".")
247                 .build();
248     }
249
250     private static ConfigDescriptionParameter getActionButton2ConfigParameter(@Nullable Locale locale) {
251         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_ACTION_BUTTON_2, Type.TEXT)
252                 .withLabel("Action Button 2").withDescription("An action button in the format \"Title=Action\".")
253                 .build();
254     }
255
256     private static ConfigDescriptionParameter getActionButton3ConfigParameter(@Nullable Locale locale) {
257         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_ACTION_BUTTON_3, Type.TEXT)
258                 .withLabel("Action Button 3").withDescription("An action button in the format \"Title=Action\".")
259                 .build();
260     }
261
262     @Override
263     public void addProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
264         // does nothing because this provider does not change
265     }
266
267     @Override
268     public void removeProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
269         // does nothing because this provider does not change
270     }
271
272     private enum ConfigType {
273         NOT_EXTENDED,
274         EXTENDED,
275         EXTENDED2,
276         HIDE_BY_TAG,
277         HIDE_BY_REF;
278     }
279 }