]> git.basschouten.com Git - openhab-addons.git/blob
d97e76b0e6dcd31d4a7103308716f71546f5608b
[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  */
38 @NonNullByDefault
39 @Component(service = ModuleTypeProvider.class)
40 public class NotificationActionTypeProvider implements ModuleTypeProvider {
41
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,
53             null, null);
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);
72
73     @SuppressWarnings("unchecked")
74     @Override
75     public @Nullable ModuleType getModuleType(String UID, @Nullable Locale locale) {
76         switch (UID) {
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;
89             default:
90                 return null;
91         }
92     }
93
94     @Override
95     public Collection<ModuleType> getAll() {
96         return MODULE_TYPES;
97     }
98
99     @Override
100     public Collection<ModuleType> getModuleTypes(@Nullable Locale locale) {
101         return MODULE_TYPES;
102     }
103
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.")
109                 .build());
110         params.addAll(getNotificationConfig(isExtended, locale));
111         return params;
112     }
113
114     private static List<ConfigDescriptionParameter> getNotificationConfig(boolean isExtended, @Nullable Locale locale) {
115         List<ConfigDescriptionParameter> params = new ArrayList<>();
116         params.add(getMessageConfigParameter(locale));
117         if (isExtended) {
118             params.add(getIconConfigParameter(locale));
119             params.add(getSeverityConfigParameter(locale));
120         }
121         return params;
122     }
123
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();
127     }
128
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();
132     }
133
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();
137     }
138
139     @Override
140     public void addProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
141         // does nothing because this provider does not change
142     }
143
144     @Override
145     public void removeProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
146         // does nothing because this provider does not change
147     }
148 }