]> git.basschouten.com Git - openhab-addons.git/blob
78580a81e1d29faabb04eceb4bfec3582d0d8979
[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",
50             "Sends a notification to a specific cloud user. Optionally add an icon or the severity.", 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, severity, title, click action, media attachment and action buttons",
56             "Sends a notification to a specific cloud user. Optionally add an icon, severity, title, 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), "broadcast a notification with icon and severity",
67             "Sends a notification to all devices of all users. Optionally add an icon or the severity.", null,
68             Visibility.VISIBLE, null, null);
69
70     private static final ModuleType SEND_EXTENDED2_BROADCAST_NOTIFICATION_ACTION = new ActionType(
71             SendBroadcastNotificationActionHandler.EXTENDED2_TYPE_ID,
72             getNotificationConfig(ConfigType.EXTENDED2, false, null),
73             "broadcast a notification with with icon, severity, title, on click action, media attachment and action buttons",
74             "Sends a notification to all devices of all users. Optionally add an icon, severity, title, click action, media to attach, and up to 3 action buttons with a format of \"Title=Action\".",
75             null, Visibility.VISIBLE, null, null);
76
77     private static final ModuleType SEND_LOG_NOTIFICATION_ACTION = new ActionType(
78             SendLogNotificationActionHandler.TYPE_ID, getNotificationConfig(ConfigType.NOT_EXTENDED, false, null),
79             "send a log message",
80             "Sends a log notification to the openHAB Cloud instance. Notifications are NOT sent to any registered devices.",
81             null, Visibility.VISIBLE, null, null);
82
83     private static final ModuleType SEND_EXTENDED_LOG_NOTIFICATION_ACTION = new ActionType(
84             SendLogNotificationActionHandler.EXTENDED_TYPE_ID, getNotificationConfig(ConfigType.EXTENDED, false, null),
85             "send a log message with icon and severity",
86             "Sends a log notification to the openHAB Cloud instance. Optionally add an icon or the severity. Notifications are NOT sent to any registered devices.",
87             null, Visibility.VISIBLE, null, null);
88
89     private static final List<ModuleType> MODULE_TYPES = List.of(SEND_NOTIFICATION_ACTION,
90             SEND_EXTENDED_NOTIFICATION_ACTION, SEND_EXTENDED2_NOTIFICATION_ACTION, SEND_BROADCAST_NOTIFICATION_ACTION,
91             SEND_EXTENDED_BROADCAST_NOTIFICATION_ACTION, SEND_EXTENDED2_BROADCAST_NOTIFICATION_ACTION,
92             SEND_LOG_NOTIFICATION_ACTION, SEND_EXTENDED_LOG_NOTIFICATION_ACTION);
93
94     @SuppressWarnings("unchecked")
95     @Override
96     public @Nullable ModuleType getModuleType(String UID, @Nullable Locale locale) {
97         switch (UID) {
98             case SendNotificationActionHandler.TYPE_ID:
99                 return SEND_NOTIFICATION_ACTION;
100             case SendNotificationActionHandler.EXTENDED_TYPE_ID:
101                 return SEND_EXTENDED_NOTIFICATION_ACTION;
102             case SendNotificationActionHandler.EXTENDED2_TYPE_ID:
103                 return SEND_EXTENDED2_NOTIFICATION_ACTION;
104             case SendBroadcastNotificationActionHandler.TYPE_ID:
105                 return SEND_BROADCAST_NOTIFICATION_ACTION;
106             case SendBroadcastNotificationActionHandler.EXTENDED_TYPE_ID:
107                 return SEND_EXTENDED_BROADCAST_NOTIFICATION_ACTION;
108             case SendBroadcastNotificationActionHandler.EXTENDED2_TYPE_ID:
109                 return SEND_EXTENDED2_BROADCAST_NOTIFICATION_ACTION;
110             case SendLogNotificationActionHandler.TYPE_ID:
111                 return SEND_LOG_NOTIFICATION_ACTION;
112             case SendLogNotificationActionHandler.EXTENDED_TYPE_ID:
113                 return SEND_EXTENDED_LOG_NOTIFICATION_ACTION;
114             default:
115                 return null;
116         }
117     }
118
119     @Override
120     public Collection<ModuleType> getAll() {
121         return MODULE_TYPES;
122     }
123
124     @Override
125     public Collection<ModuleType> getModuleTypes(@Nullable Locale locale) {
126         return MODULE_TYPES;
127     }
128
129     private static List<ConfigDescriptionParameter> getNotificationConfig(ConfigType type, boolean userRequired,
130             @Nullable Locale locale) {
131         List<ConfigDescriptionParameter> params = new ArrayList<>();
132
133         if (userRequired) {
134             params.add(ConfigDescriptionParameterBuilder.create(SendNotificationActionHandler.PARAM_USER, Type.TEXT)
135                     .withRequired(true).withLabel("User Id").withDescription("The cloud user id of the recipient.")
136                     .build());
137         }
138
139         if (type == ConfigType.EXTENDED || type == ConfigType.EXTENDED2) {
140             params.add(getMessageConfigParameter(locale));
141             params.add(getIconConfigParameter(locale));
142             params.add(getSeverityConfigParameter(locale));
143         }
144         if (type == ConfigType.EXTENDED2) {
145             params.add(getTitleConfigParameter(locale));
146             params.add(getonClickActionConfigParameter(locale));
147             params.add(getMediaAttachmentUrlConfigParameter(locale));
148             params.add(getActionButton1ConfigParameter(locale));
149             params.add(getActionButton2ConfigParameter(locale));
150             params.add(getActionButton3ConfigParameter(locale));
151         }
152
153         return params;
154     }
155
156     private static ConfigDescriptionParameter getMessageConfigParameter(@Nullable Locale locale) {
157         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_MESSAGE, Type.TEXT)
158                 .withRequired(true).withLabel("Message").withDescription("The body of the notification.").build();
159     }
160
161     private static ConfigDescriptionParameter getIconConfigParameter(@Nullable Locale locale) {
162         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_ICON, Type.TEXT)
163                 .withLabel("Icon").withDescription("The icon of the notification.").build();
164     }
165
166     private static ConfigDescriptionParameter getSeverityConfigParameter(@Nullable Locale locale) {
167         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_SEVERITY, Type.TEXT)
168                 .withLabel("Severity").withDescription("The severity of the notification.").build();
169     }
170
171     private static ConfigDescriptionParameter getTitleConfigParameter(@Nullable Locale locale) {
172         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_TITLE, Type.TEXT)
173                 .withLabel("Title").withDescription("The title of the notification.").build();
174     }
175
176     private static ConfigDescriptionParameter getonClickActionConfigParameter(@Nullable Locale locale) {
177         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_ON_CLICK_ACTION, Type.TEXT)
178                 .withLabel("On Click Action").withDescription("The action to perform when clicked.").build();
179     }
180
181     private static ConfigDescriptionParameter getMediaAttachmentUrlConfigParameter(@Nullable Locale locale) {
182         return ConfigDescriptionParameterBuilder
183                 .create(BaseNotificationActionHandler.PARAM_MEDIA_ATTACHMENT_URL, Type.TEXT)
184                 .withLabel("Media Attachment URL").withDescription("The media to attach to a notification.").build();
185     }
186
187     private static ConfigDescriptionParameter getActionButton1ConfigParameter(@Nullable Locale locale) {
188         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_ACTION_BUTTON_1, Type.TEXT)
189                 .withLabel("Action Button 1").withDescription("An action button in the format \"Title=Action\".")
190                 .build();
191     }
192
193     private static ConfigDescriptionParameter getActionButton2ConfigParameter(@Nullable Locale locale) {
194         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_ACTION_BUTTON_2, Type.TEXT)
195                 .withLabel("Action Button 2").withDescription("An action button in the format \"Title=Action\".")
196                 .build();
197     }
198
199     private static ConfigDescriptionParameter getActionButton3ConfigParameter(@Nullable Locale locale) {
200         return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_ACTION_BUTTON_3, Type.TEXT)
201                 .withLabel("Action Button 3").withDescription("An action button in the format \"Title=Action\".")
202                 .build();
203     }
204
205     @Override
206     public void addProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
207         // does nothing because this provider does not change
208     }
209
210     @Override
211     public void removeProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
212         // does nothing because this provider does not change
213     }
214
215     private enum ConfigType {
216         NOT_EXTENDED,
217         EXTENDED,
218         EXTENDED2;
219     }
220 }