2 * Copyright (c) 2010-2024 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.io.openhabcloud.internal.actions;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Locale;
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;
33 * This class provides a {@link ModuleTypeProvider} implementation to provide actions to send notifications via
36 * @author Christoph Weitkamp - Initial contribution
37 * @author Dan Cunningham - Extended notification enhancements
40 @Component(service = ModuleTypeProvider.class)
41 public class NotificationActionTypeProvider implements ModuleTypeProvider {
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);
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);
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);
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,
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);
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);
78 private static final ModuleType SEND_LOG_NOTIFICATION_ACTION = new ActionType(
79 SendLogNotificationActionHandler.TYPE_ID, getNotificationConfig(ConfigType.NOT_EXTENDED, false, null),
81 "Sends a log notification to the openHAB Cloud instance. Notifications are NOT sent to any registered devices.",
82 null, Visibility.VISIBLE, null, null);
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);
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,
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);
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);
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);
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);
121 @SuppressWarnings("unchecked")
123 public @Nullable ModuleType getModuleType(String UID, @Nullable Locale locale) {
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;
155 public Collection<ModuleType> getAll() {
160 public Collection<ModuleType> getModuleTypes(@Nullable Locale locale) {
164 private static List<ConfigDescriptionParameter> getNotificationConfig(ConfigType type, boolean userRequired,
165 @Nullable Locale locale) {
166 List<ConfigDescriptionParameter> params = new ArrayList<>();
169 params.add(ConfigDescriptionParameterBuilder.create(SendNotificationActionHandler.PARAM_USER, Type.TEXT)
170 .withRequired(true).withLabel("User Id").withDescription("The cloud user id of the recipient.")
175 params.add(getMessageConfigParameter(locale));
176 params.add(getIconConfigParameter(locale));
177 params.add(getSeverityConfigParameter(locale));
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));
192 params.add(getTagConfigParameter(locale));
195 params.add(getReferenceIdConfigParameter(locale));
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();
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();
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();
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();
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();
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();
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();
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();
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\".")
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\".")
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\".")
263 public void addProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
264 // does nothing because this provider does not change
268 public void removeProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
269 // does nothing because this provider does not change
272 private enum ConfigType {