2 * Copyright (c) 2010-2023 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
39 @Component(service = ModuleTypeProvider.class)
40 public class NotificationActionTypeProvider implements ModuleTypeProvider {
42 private static final ModuleType SEND_NOTIFICATION_ACTION = new ActionType(SendNotificationActionHandler.TYPE_ID,
43 getSendNotificationConfig(false, null), "send a notification",
44 "Sends a notification to a specific cloud user.", null, Visibility.VISIBLE, null, null);
45 private static final ModuleType SEND_EXTENDED_NOTIFICATION_ACTION = new ActionType(
46 SendNotificationActionHandler.EXTENDED_TYPE_ID, getSendNotificationConfig(true, null),
47 "send a notification with icon and severity",
48 "Sends a notification to a specific cloud user. Optionally add an icon or the severity.", null,
49 Visibility.VISIBLE, null, null);
50 private static final ModuleType SEND_BROADCAST_NOTIFICATION_ACTION = new ActionType(
51 SendBroadcastNotificationActionHandler.TYPE_ID, getNotificationConfig(false, null),
52 "broadcast a notification", "Sends a notification to all devices of all users.", null, Visibility.VISIBLE,
54 private static final ModuleType SEND_EXRENDED_BROADCAST_NOTIFICATION_ACTION = new ActionType(
55 SendBroadcastNotificationActionHandler.EXTENDED_TYPE_ID, getNotificationConfig(true, null),
56 "broadcast a notification with icon and severity",
57 "Sends a notification to all devices of all users. Optionally add an icon or the severity.", null,
58 Visibility.VISIBLE, null, null);
59 private static final ModuleType SEND_LOG_NOTIFICATION_ACTION = new ActionType(
60 SendLogNotificationActionHandler.TYPE_ID, getNotificationConfig(false, null), "send a log message",
61 "Sends a log notification to the openHAB Cloud instance. Notifications are NOT sent to any registered devices.",
62 null, Visibility.VISIBLE, null, null);
63 private static final ModuleType SEND_EXTENDED_LOG_NOTIFICATION_ACTION = new ActionType(
64 SendLogNotificationActionHandler.EXTENDED_TYPE_ID, getNotificationConfig(true, null),
65 "send a log message with icon and severity",
66 "Sends a log notification to the openHAB Cloud instance. Optionally add an icon or the severity. Notifications are NOT sent to any registered devices.",
67 null, Visibility.VISIBLE, null, null);
68 private static final List<ModuleType> MODULE_TYPES = List.of(SEND_NOTIFICATION_ACTION,
69 SEND_EXTENDED_NOTIFICATION_ACTION, SEND_BROADCAST_NOTIFICATION_ACTION,
70 SEND_EXRENDED_BROADCAST_NOTIFICATION_ACTION, SEND_LOG_NOTIFICATION_ACTION,
71 SEND_EXTENDED_LOG_NOTIFICATION_ACTION);
73 @SuppressWarnings("unchecked")
75 public @Nullable ModuleType getModuleType(String UID, @Nullable Locale locale) {
77 case SendNotificationActionHandler.TYPE_ID:
78 return SEND_NOTIFICATION_ACTION;
79 case SendNotificationActionHandler.EXTENDED_TYPE_ID:
80 return SEND_EXTENDED_NOTIFICATION_ACTION;
81 case SendBroadcastNotificationActionHandler.TYPE_ID:
82 return SEND_BROADCAST_NOTIFICATION_ACTION;
83 case SendBroadcastNotificationActionHandler.EXTENDED_TYPE_ID:
84 return SEND_EXRENDED_BROADCAST_NOTIFICATION_ACTION;
85 case SendLogNotificationActionHandler.TYPE_ID:
86 return SEND_LOG_NOTIFICATION_ACTION;
87 case SendLogNotificationActionHandler.EXTENDED_TYPE_ID:
88 return SEND_EXTENDED_LOG_NOTIFICATION_ACTION;
95 public Collection<ModuleType> getAll() {
100 public Collection<ModuleType> getModuleTypes(@Nullable Locale locale) {
104 private static List<ConfigDescriptionParameter> getSendNotificationConfig(boolean isExtended,
105 @Nullable Locale locale) {
106 List<ConfigDescriptionParameter> params = new ArrayList<>();
107 params.add(ConfigDescriptionParameterBuilder.create(SendNotificationActionHandler.PARAM_USER, Type.TEXT)
108 .withRequired(true).withLabel("User Id").withDescription("The cloud user id of the recipient.")
110 params.addAll(getNotificationConfig(isExtended, locale));
114 private static List<ConfigDescriptionParameter> getNotificationConfig(boolean isExtended, @Nullable Locale locale) {
115 List<ConfigDescriptionParameter> params = new ArrayList<>();
116 params.add(getMessageConfigParameter(locale));
118 params.add(getIconConfigParameter(locale));
119 params.add(getSeverityConfigParameter(locale));
124 private static ConfigDescriptionParameter getMessageConfigParameter(@Nullable Locale locale) {
125 return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_MESSAGE, Type.TEXT)
126 .withRequired(true).withLabel("Message").withDescription("The body of the notification.").build();
129 private static ConfigDescriptionParameter getIconConfigParameter(@Nullable Locale locale) {
130 return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_ICON, Type.TEXT)
131 .withLabel("Icon").withDescription("The icon of the notification.").build();
134 private static ConfigDescriptionParameter getSeverityConfigParameter(@Nullable Locale locale) {
135 return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_SEVERITY, Type.TEXT)
136 .withLabel("Severity").withDescription("The severity of the notification.").build();
140 public void addProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
141 // does nothing because this provider does not change
145 public void removeProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
146 // does nothing because this provider does not change