From 1d41b23deb72c155f8dfede645207c4e37838ad4 Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Mon, 1 Jul 2024 21:42:15 -0700 Subject: [PATCH 1/8] [openhabcloud] Support hiding notifications See #16934 Signed-off-by: Dan Cunningham --- bundles/org.openhab.io.openhabcloud/README.md | 21 ++++- .../io/openhabcloud/NotificationAction.java | 82 ++++++++++++++--- .../io/openhabcloud/internal/CloudClient.java | 92 +++++++++++++++++-- .../openhabcloud/internal/CloudService.java | 68 ++++++++++++-- .../BaseHideNotificationActionHandler.java | 48 ++++++++++ .../BaseNotificationActionHandler.java | 3 + ...otificationByReferenceIdActionHandler.java | 42 +++++++++ ...stNotificationBySeverityActionHandler.java | 42 +++++++++ ...otificationByReferenceIdActionHandler.java | 53 +++++++++++ ...deNotificationBySeverityActionHandler.java | 53 +++++++++++ .../NotificationActionTypeProvider.java | 62 +++++++++++-- .../NotificationModuleHandlerFactory.java | 13 ++- ...endBroadcastNotificationActionHandler.java | 4 +- .../SendNotificationActionHandler.java | 4 +- 14 files changed, 543 insertions(+), 44 deletions(-) create mode 100644 bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseHideNotificationActionHandler.java create mode 100644 bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationByReferenceIdActionHandler.java create mode 100644 bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationBySeverityActionHandler.java create mode 100644 bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByReferenceIdActionHandler.java create mode 100644 bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationBySeverityActionHandler.java diff --git a/bundles/org.openhab.io.openhabcloud/README.md b/bundles/org.openhab.io.openhabcloud/README.md index 655691fa89dc5..f4686b4939060 100644 --- a/bundles/org.openhab.io.openhabcloud/README.md +++ b/bundles/org.openhab.io.openhabcloud/README.md @@ -112,11 +112,13 @@ The parameters for these actions have the following meaning: `null` may be used to skip the `icon` or `severity` parameter. -### Title, Media Attachments & Actions +### Title, Reference ID, Media Attachments & Actions -The `sendNotification` and `sendBroadcastNotification` actions additionally support setting a title, media attachments and actions. +The `sendNotification` and `sendBroadcastNotification` actions additionally support setting a title, reference id, media attachments and actions. The title is displayed as the notification title on the device and defaults to "openHAB" for the Android and iOS apps. +The reference id is a user supplied identifier that when set will replace messages with the same id on the user's device (so only the last version exists). +The reference id can also be used later with `hideNotificationByReferenceId` and `hideBroadcastNotificationByReferenceId` methods to remove notifications with this id. Media attachments are displayed together with the notification on the device and can be used to display images, e.g. a camera snapshot. Actions allow the user to interact with the notification, e.g. to open a specific page in the app or to send a command to an Item. @@ -130,9 +132,11 @@ To specify media attachments and actions, there is another variant of the `sendN - `sendNotification(emailAddress, message, icon, severity, title, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)` - `sendBroadcastNotification(message, icon, severity, title, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)` + The additional parameter for these variants have the following meaning: - `title`: The title of the notification. Defaults to "openHAB" inside the Android and iOS apps. +- `referenceId`: A user supplied id to both replace existing messages, and later remove messages with this id. - `onClickAction`: The action to be performed when the user clicks on the notification. Specified using the [action syntax](#action-syntax). - `mediaAttachmentUrl`: The URL of the media attachment to be displayed with the notification. This URL must be reachable by the push notification client. - `actionButton1`: The action to be performed when the user clicks on the first action button. Specified as `Title=$action`, where `$action` follows the [action syntax](#action-syntax). @@ -141,6 +145,15 @@ The additional parameter for these variants have the following meaning: These parameters may be skipped by setting them to `null`. +### Hide Notification Actions + +There are also actions to hide existing notifications, either by `referenceId` or `severity` + +- `hideNotificationByReferenceId(emailAddress, referenceId)` +- `hideBroadcastNotificationByReferenceId(referenceId)` +- `hideNotificationBySeverity(emailAddress, severity)` +- `hideBroadcastNotificationBySeverity(severity)` + #### Action Syntax The action syntax is a string containing the action type and the action payload separated by a colon. @@ -151,6 +164,7 @@ There are two types of actions available: - `ui`: Controls the UI in two possible ways: - `ui:$path` where `$path` is either `/basicui/app?...` for navigating sitemaps (using the native renderer) or `/some/absolute/path` for navigating (using the web view). - `ui:$commandItemSyntax` where `$commandItemSyntax` is the same syntax as used for the [UI Command Item]({{base}}/mainui/about.html#ui-command-item). +- `http:` or `https:` : Opens the fully qualified URL in an embedded browser on the device Examples: @@ -160,6 +174,7 @@ Examples: - `ui:/some/absolute/path`: Navigates to the absolut path `/some/absolute/path`. - `ui:navigate:/page/my_floorplan_page`: Navigates Main UI to the page with the ID `my_floorplan_page`. - `ui:popup:oh-clock-card`: Opens a popup with `oh-clock-card`. +- `https://openhab.org`: Opens an embedded browser to the site `https://openhab.org` ### Examples @@ -267,7 +282,7 @@ when Item Apartment_MotionSensor changed to ON then sendBroadcastNotification("Motion detected in the apartment!", "motion", "MEDIUM", - "Motion Detected", null, "https://apartment.my/camera-snapshot.jpg", + "Motion Detected", null, null, "https://apartment.my/camera-snapshot.jpg", "Turn on the light=command:Apartment_Light:ON", null, null) end ``` diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java index 2d4591dabe70a..068cf28a51a08 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java @@ -58,7 +58,7 @@ public static void sendNotification(String userId, String message) { @ActionDoc(text = "Sends a push notification to mobile devices of user with userId") public static void sendNotification(String userId, String message, @Nullable String icon, @Nullable String severity) { - sendNotification(userId, message, icon, severity, null, null, null, null, null, null); + sendNotification(userId, message, icon, severity, null, null, null, null, null, null, null); } /** @@ -69,6 +69,7 @@ public static void sendNotification(String userId, String message, @Nullable Str * @param icon name for the notification * @param severity category for the notification * @param title for the notification + * @param referenceId an identifier used to collapse and hide notifications * @param onClickAction the action to perform when clicked * @param mediaAttachmentUrl the media to attach to a notification * @param actionButton1 an action button in the format "Title=Action" @@ -78,12 +79,13 @@ public static void sendNotification(String userId, String message, @Nullable Str */ @ActionDoc(text = "Sends a push notification to mobile devices of user with userId") public static void sendNotification(String userId, String message, @Nullable String icon, @Nullable String severity, - @Nullable String title, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, - @Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) { + @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, + @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, + @Nullable String actionButton3) { logger.debug("sending notification '{}' to user {}", message, userId); if (cloudService != null) { - cloudService.sendNotification(userId, message, icon, severity, title, onClickAction, mediaAttachmentUrl, - actionButton1, actionButton2, actionButton3); + cloudService.sendNotification(userId, message, icon, severity, title, referenceId, onClickAction, + mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); } } @@ -137,9 +139,9 @@ public static void sendBroadcastNotification(String message) { * @param severity category for the notification * */ - @ActionDoc(text = "Sends a push notification to mobile devices of user with userId") + @ActionDoc(text = "Sends a broadcast notification to all mobile devices of all account users") public static void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String severity) { - sendBroadcastNotification(message, icon, severity, null, null, null, null, null, null); + sendBroadcastNotification(message, icon, severity, null, null, null, null, null, null, null); } /** @@ -150,6 +152,7 @@ public static void sendBroadcastNotification(String message, @Nullable String ic * @param icon name for the notification * @param severity category for the notification * @param title for the notification + * @param referenceId an identifier used to collapse and hide notifications * @param onClickAction the action to perform when clicked * @param mediaAttachmentUrl the media to attach to a notification * @param actionButton1 an action button in the format "Title=Action" @@ -157,14 +160,69 @@ public static void sendBroadcastNotification(String message, @Nullable String ic * @param actionButton3 an action button in the format "Title=Action" * */ - @ActionDoc(text = "Sends a push notification to mobile devices of user with userId") + @ActionDoc(text = "Sends a broadcast notification to all mobile devices of all account user") public static void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String severity, - @Nullable String title, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, - @Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) { + @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, + @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, + @Nullable String actionButton3) { logger.debug("sending broadcast notification '{}' to all users", message); if (cloudService != null) { - cloudService.sendBroadcastNotification(message, icon, severity, title, onClickAction, mediaAttachmentUrl, - actionButton1, actionButton2, actionButton3); + cloudService.sendBroadcastNotification(message, icon, severity, title, referenceId, onClickAction, + mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); + } + } + + /** + * Hides notifications that contains a matching reference id to all mobile devices of a single user. + * + * @param userId the cloud user id of the recipient + * @param referenceId the user reference id + * + */ + @ActionDoc(text = "Hides notifications that contain the reference id to mobile devices of user with userId") + public static void hideNotificationByReferenceId(String userId, String referenceId) { + if (cloudService != null) { + cloudService.hideNotificationByReferenceId(userId, referenceId); + } + } + + /** + * Hides notifications that contains a matching reference id to all mobile devices of all users of the account + * + * @param referenceId the user reference id + * + */ + @ActionDoc(text = "Hides notifications that contain the reference id to all mobile devices of all account users") + public static void hideBroadcastNotificationByReferenceId(String referenceId) { + if (cloudService != null) { + cloudService.hideBroadcastNotificationByReferenceId(referenceId); + } + } + + /** + * Hides notifications that are associated with a severity group to all mobile devices of a single user. + * + * @param userId the cloud user id of the recipient + * @param severity the severity group + * + */ + @ActionDoc(text = "Hides notifications that are associated with a severity group to mobile devices of user with userId") + public static void hideNotificationBySeverity(String userId, String severity) { + if (cloudService != null) { + cloudService.hideNotificationBySeverity(userId, severity); + } + } + + /** + * Hides notifications that are associated with a severity group to all mobile devices of all users of the account + * + * @param severity the severity group + * + */ + @ActionDoc(text = "Hides notifications that are associated with a severity group to all mobile devices of all account users") + public static void hideBroadcastNotificationBySeverity(String severity) { + if (cloudService != null) { + cloudService.hideBroadcastNotificationBySeverity(severity); } } } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java index a99b7cd79544b..fe578e1eed1e1 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java @@ -590,6 +590,7 @@ private void handleCommandEvent(JSONObject data) { * @param icon name of the icon for this notification * @param severity severity name for this notification * @param title for the notification + * @param referenceId an identifier used to collapse and hide notifications * @param onClickAction the action to perform when clicked * @param mediaAttachmentUrl the media to attach to a notification * @param actionButton1 an action button in the format "Title=Action" @@ -597,9 +598,10 @@ private void handleCommandEvent(JSONObject data) { * @param actionButton3 an action button in the format "Title=Action" */ public void sendNotification(String userId, String message, @Nullable String icon, @Nullable String severity, - @Nullable String title, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, - @Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) { - sendNotificationInternal(userId, message, icon, severity, title, onClickAction, mediaAttachmentUrl, + @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, + @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, + @Nullable String actionButton3) { + sendNotificationInternal(userId, message, icon, severity, title, referenceId, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); } @@ -610,6 +612,7 @@ public void sendNotification(String userId, String message, @Nullable String ico * @param icon name of the icon for this notification * @param severity severity name for this notification * @param title for this notification + * @param referenceId an identifier used to collapse and hide notifications * @param onClickAction the action to perform when clicked * @param mediaAttachmentUrl the media to attach to a notification * @param actionButton1 an action button in the format "Title=Action" @@ -617,25 +620,31 @@ public void sendNotification(String userId, String message, @Nullable String ico * @param actionButton3 an action button in the format "Title=Action" */ public void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String severity, - @Nullable String title, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, - @Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) { - sendNotificationInternal(null, message, icon, severity, title, onClickAction, mediaAttachmentUrl, actionButton1, - actionButton2, actionButton3); + @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, + @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, + @Nullable String actionButton3) { + sendNotificationInternal(null, message, icon, severity, title, referenceId, onClickAction, mediaAttachmentUrl, + actionButton1, actionButton2, actionButton3); } private void sendNotificationInternal(@Nullable String userId, String message, @Nullable String icon, - @Nullable String severity, @Nullable String title, @Nullable String onClickAction, - @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, - @Nullable String actionButton3) { + @Nullable String severity, @Nullable String title, @Nullable String referenceId, + @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, + @Nullable String actionButton2, @Nullable String actionButton3) { if (isConnected()) { JSONObject notificationMessage = new JSONObject(); try { if (userId != null) { notificationMessage.put("userId", userId); } + + notificationMessage.put("type", "notification"); notificationMessage.put("message", message); notificationMessage.put("icon", icon); notificationMessage.put("severity", severity); + if (referenceId != null) { + notificationMessage.put("reference-id", title); + } if (title != null) { notificationMessage.put("title", title); } @@ -681,6 +690,69 @@ public void sendLogNotification(String message, @Nullable String icon, @Nullable } } + /** + * This method hides a notification by its reference id to a single user + * + * @param userId openHAB Cloud user id + * @param referenceId the reference id + */ + public void hideNotificationByReferenceId(String userId, String referenceId) { + hideNotificationInternal(userId, referenceId, null); + } + + /** + * This method hides a notification by its reference id to for all users + * + * @param referenceId the reference id + */ + public void hideBroadcastNotificationByReferenceId(String referenceId) { + hideNotificationInternal(null, referenceId, null); + } + + /** + * This method hides a notification by its severity group to for all users + * + * @param userId openHAB Cloud user id + * @param severity severity name for this notification + */ + public void hideNotificationBySeverity(String userId, String severity) { + hideNotificationInternal(userId, null, severity); + } + + /** + * This method hides a notification by its severity group to for all users + * + * @param severity severity name for this notification + */ + public void hideBroadcastNotificationBySeverity(String severity) { + hideNotificationInternal(null, null, severity); + } + + private void hideNotificationInternal(@Nullable String userId, @Nullable String referenceId, + @Nullable String severity) { + if (isConnected()) { + JSONObject notificationMessage = new JSONObject(); + try { + notificationMessage.put("type", "hideNotification"); + + if (userId != null) { + notificationMessage.put("userId", userId); + } + if (referenceId != null) { + notificationMessage.put("referenceId", referenceId); + } + if (severity != null) { + notificationMessage.put("severity", severity); + } + socket.emit(userId == null ? "broadcastnotification" : "notification", notificationMessage); + } catch (JSONException e) { + logger.debug("{}", e.getMessage()); + } + } else { + logger.debug("No connection, notification is not sent"); + } + } + /** * Send item update to openHAB Cloud * diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java index 4d82b1dfa9ab1..e9f0c09db1eab 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java @@ -116,6 +116,7 @@ public CloudService(final @Reference HttpClientFactory httpClientFactory, * @param icon the {@link String} containing a name of the icon to be used with this notification * @param severity the {@link String} containing severity (good, info, warning, error) of notification * @param title the {@link String} containing the title to be used with this notification + * @param referenceId the {@link String} identifier used to collapse and hide notifications * @param onClickAction the {@link String} containing the action to perform when clicked * @param mediaAttachmentUrl the {@link String} containing the media to attach to a notification * @param actionButton1 the {@link String} containing the action button in the format "Title=Action" @@ -123,11 +124,12 @@ public CloudService(final @Reference HttpClientFactory httpClientFactory, * @param actionButton3 the {@link String} containing the action button in the format "Title=Action" */ public void sendNotification(String userId, String message, @Nullable String icon, @Nullable String severity, - @Nullable String title, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, - @Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) { + @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, + @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, + @Nullable String actionButton3) { logger.debug("Sending message '{}' to user id {}", message, userId); - cloudClient.sendNotification(userId, message, icon, severity, title, onClickAction, mediaAttachmentUrl, - actionButton1, actionButton2, actionButton3); + cloudClient.sendNotification(userId, message, icon, severity, title, referenceId, onClickAction, + mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); } /** @@ -151,6 +153,7 @@ public void sendLogNotification(String message, @Nullable String icon, @Nullable * @param icon the {@link String} containing a name of the icon to be used with this notification * @param severity the {@link String} containing severity (good, info, warning, error) of notification * @param title the {@link String} containing the title to be used with this notification + * @param referenceId the {@link String} identifier used to collapse and hide notifications * @param onClickAction the {@link String} containing the action to perform when clicked * @param mediaAttachmentUrl the {@link String} containing the media to attach to a notification * @param actionButton1 the {@link String} containing the action button in the format "Title=Action" @@ -158,11 +161,60 @@ public void sendLogNotification(String message, @Nullable String icon, @Nullable * @param actionButton3 the {@link String} containing the action button in the format "Title=Action" */ public void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String severity, - @Nullable String title, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, - @Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) { + @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, + @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, + @Nullable String actionButton3) { logger.debug("Sending broadcast message '{}' to all users", message); - cloudClient.sendBroadcastNotification(message, icon, severity, title, onClickAction, mediaAttachmentUrl, - actionButton1, actionButton2, actionButton3); + cloudClient.sendBroadcastNotification(message, icon, severity, title, referenceId, onClickAction, + mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); + } + + /** + * This method hides a notification by its severity group through the openHAB Cloud service + * + * @param userId the {@link String} containing the openHAB Cloud user id to hide messages for + * @param severity the {@link String} containing severity group of notification + * + */ + public void hideNotificationBySeverity(String userId, String severity) { + logger.debug("hiding with severity '{}' to user id {}", severity, userId); + cloudClient.hideNotificationBySeverity(userId, severity); + } + + /** + * This method hides a notification by its severity group through the openHAB Cloud service to all + * mobile devices of all users of the account + * + * @param severity the {@link String} containing severity (good, info, warning, error) of notification + * + */ + public void hideBroadcastNotificationBySeverity(String severity) { + logger.debug("hiding broadcast with severity '{}'", severity); + cloudClient.hideBroadcastNotificationBySeverity(severity); + } + + /** + * This method hides a notification by its reference id through the openHAB Cloud service + * + * @param userId the {@link String} containing the openHAB Cloud user id to hide messages for + * @param severity the {@link String} containing severity group of notification + * + */ + public void hideNotificationByReferenceId(String userId, String referenceId) { + logger.debug("hiding with referenceId '{}' to user id {}", referenceId, userId); + cloudClient.hideNotificationByReferenceId(userId, referenceId); + } + + /** + * This method hides a notification by its reference id through the openHAB Cloud service to all + * mobile devices of all users of the account + * + * @param severity the {@link String} containing severity (good, info, warning, error) of notification + * + */ + public void hideBroadcastNotificationByReferenceId(String referenceId) { + logger.debug("hiding broadcast with referenceId '{}'", referenceId); + cloudClient.hideBroadcastNotificationByReferenceId(referenceId); } private String substringBefore(String str, String separator) { diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseHideNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseHideNotificationActionHandler.java new file mode 100644 index 0000000000000..31be4375f021d --- /dev/null +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseHideNotificationActionHandler.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2010-2024 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.io.openhabcloud.internal.actions; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.automation.Action; +import org.openhab.core.automation.handler.BaseActionModuleHandler; +import org.openhab.core.automation.handler.ModuleHandler; +import org.openhab.core.config.core.ConfigParser; +import org.openhab.io.openhabcloud.internal.CloudService; + +/** + * This is a base {@link ModuleHandler} implementation for {@link Action}s to hide notifications via openHAB Cloud. + * + * @author Dan Cunningham - Initial contribution + */ +@NonNullByDefault +public abstract class BaseHideNotificationActionHandler extends BaseActionModuleHandler { + + public static final String PARAM_SEVERITY = "severity"; + public static final String PARAM_REFERENCE_ID = "referenceId"; + + protected final CloudService cloudService; + protected final @Nullable String severity; + protected final @Nullable String referenceId; + + public BaseHideNotificationActionHandler(Action module, CloudService cloudService) { + super(module); + this.cloudService = cloudService; + this.severity = stringConfig(PARAM_SEVERITY); + this.referenceId = stringConfig(PARAM_REFERENCE_ID); + } + + private @Nullable String stringConfig(String key) { + return ConfigParser.valueAs(module.getConfiguration().get(key), String.class); + } +} diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java index e866e1f9264d3..de83286fd9caf 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java @@ -33,6 +33,7 @@ public abstract class BaseNotificationActionHandler extends BaseActionModuleHand public static final String PARAM_ICON = "icon"; public static final String PARAM_SEVERITY = "severity"; public static final String PARAM_TITLE = "title"; + public static final String PARAM_REFERENCE_ID = "referenceId"; public static final String PARAM_ON_CLICK_ACTION = "onClickAction"; public static final String PARAM_MEDIA_ATTACHMENT_URL = "mediaAttachmentUrl"; public static final String PARAM_ACTION_BUTTON_1 = "actionButton1"; @@ -45,6 +46,7 @@ public abstract class BaseNotificationActionHandler extends BaseActionModuleHand protected final @Nullable String icon; protected final @Nullable String severity; protected final @Nullable String title; + protected final @Nullable String referenceId; protected final @Nullable String onClickAction; protected final @Nullable String mediaAttachmentUrl; protected final @Nullable String actionButton1; @@ -65,6 +67,7 @@ public BaseNotificationActionHandler(Action module, CloudService cloudService) { this.icon = stringConfig(PARAM_ICON); this.severity = stringConfig(PARAM_SEVERITY); this.title = stringConfig(PARAM_TITLE); + this.referenceId = stringConfig(PARAM_REFERENCE_ID); this.onClickAction = stringConfig(PARAM_ON_CLICK_ACTION); this.mediaAttachmentUrl = stringConfig(PARAM_MEDIA_ATTACHMENT_URL); this.actionButton1 = stringConfig(PARAM_ACTION_BUTTON_1); diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationByReferenceIdActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationByReferenceIdActionHandler.java new file mode 100644 index 0000000000000..8fb645aefa2e4 --- /dev/null +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationByReferenceIdActionHandler.java @@ -0,0 +1,42 @@ +/** + * Copyright (c) 2010-2024 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.io.openhabcloud.internal.actions; + +import java.util.Map; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.automation.Action; +import org.openhab.core.automation.handler.ModuleHandler; +import org.openhab.io.openhabcloud.internal.CloudService; + +/** + * This is a {@link ModuleHandler} implementation for {@link Action}s to hide a notification to a specific cloud user. + * + * @author Dan Cunningham - Initial contribution + */ +@NonNullByDefault +public class HideBroadcastNotificationByReferenceIdActionHandler extends BaseHideNotificationActionHandler { + + public static final String TYPE_ID = "notification.HideBroadcastNotificationByReferenceId"; + + public HideBroadcastNotificationByReferenceIdActionHandler(Action module, CloudService cloudService) { + super(module, cloudService); + } + + @Override + public @Nullable Map execute(Map context) { + cloudService.hideBroadcastNotificationByReferenceId(referenceId); + return null; + } +} diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationBySeverityActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationBySeverityActionHandler.java new file mode 100644 index 0000000000000..e419246c19fbd --- /dev/null +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationBySeverityActionHandler.java @@ -0,0 +1,42 @@ +/** + * Copyright (c) 2010-2024 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.io.openhabcloud.internal.actions; + +import java.util.Map; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.automation.Action; +import org.openhab.core.automation.handler.ModuleHandler; +import org.openhab.io.openhabcloud.internal.CloudService; + +/** + * This is a {@link ModuleHandler} implementation for {@link Action}s to hide a notification to a specific cloud user. + * + * @author Dan Cunningham - Initial contribution + */ +@NonNullByDefault +public class HideBroadcastNotificationBySeverityActionHandler extends BaseHideNotificationActionHandler { + + public static final String TYPE_ID = "notification.HideBroadcastNotificationBySeverity"; + + public HideBroadcastNotificationBySeverityActionHandler(Action module, CloudService cloudService) { + super(module, cloudService); + } + + @Override + public @Nullable Map execute(Map context) { + cloudService.hideBroadcastNotificationBySeverity(severity); + return null; + } +} diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByReferenceIdActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByReferenceIdActionHandler.java new file mode 100644 index 0000000000000..270edb52a8f63 --- /dev/null +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByReferenceIdActionHandler.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2010-2024 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.io.openhabcloud.internal.actions; + +import java.util.Map; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.automation.Action; +import org.openhab.core.automation.handler.ModuleHandler; +import org.openhab.io.openhabcloud.internal.CloudService; + +/** + * This is a {@link ModuleHandler} implementation for {@link Action}s to hide a notification to a specific cloud user. + * + * @author Dan Cunningham - - Initial contribution + */ +@NonNullByDefault +public class HideNotificationByReferenceIdActionHandler extends BaseHideNotificationActionHandler { + + public static final String TYPE_ID = "notification.HideNotificationByReferenceId"; + + public static final String PARAM_USER = "userId"; + + private final String userId; + + public HideNotificationByReferenceIdActionHandler(Action module, CloudService cloudService) { + super(module, cloudService); + + Object userIdParam = module.getConfiguration().get(PARAM_USER); + if (userIdParam instanceof String) { + this.userId = userIdParam.toString(); + } else { + throw new IllegalArgumentException(String.format("Param '%s' should be of type String.", PARAM_USER)); + } + } + + @Override + public @Nullable Map execute(Map context) { + cloudService.hideNotificationByReferenceId(userId, referenceId); + return null; + } +} diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationBySeverityActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationBySeverityActionHandler.java new file mode 100644 index 0000000000000..e57e79bf1f742 --- /dev/null +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationBySeverityActionHandler.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2010-2024 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.io.openhabcloud.internal.actions; + +import java.util.Map; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.automation.Action; +import org.openhab.core.automation.handler.ModuleHandler; +import org.openhab.io.openhabcloud.internal.CloudService; + +/** + * This is a {@link ModuleHandler} implementation for {@link Action}s to hide a notification to a specific cloud user. + * + * @author Dan Cunningham - Initial contribution + */ +@NonNullByDefault +public class HideNotificationBySeverityActionHandler extends BaseHideNotificationActionHandler { + + public static final String TYPE_ID = "notification.HideNotificationBySeverity"; + + public static final String PARAM_USER = "userId"; + + private final String userId; + + public HideNotificationBySeverityActionHandler(Action module, CloudService cloudService) { + super(module, cloudService); + + Object userIdParam = module.getConfiguration().get(PARAM_USER); + if (userIdParam instanceof String) { + this.userId = userIdParam.toString(); + } else { + throw new IllegalArgumentException(String.format("Param '%s' should be of type String.", PARAM_USER)); + } + } + + @Override + public @Nullable Map execute(Map context) { + cloudService.hideNotificationBySeverity(userId, severity); + return null; + } +} diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationActionTypeProvider.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationActionTypeProvider.java index 78580a81e1d29..7d94cec67851f 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationActionTypeProvider.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationActionTypeProvider.java @@ -52,8 +52,8 @@ SendNotificationActionHandler.EXTENDED_TYPE_ID, getNotificationConfig(ConfigType private static final ModuleType SEND_EXTENDED2_NOTIFICATION_ACTION = new ActionType( SendNotificationActionHandler.EXTENDED2_TYPE_ID, getNotificationConfig(ConfigType.EXTENDED2, true, null), - "send a notification with icon, severity, title, click action, media attachment and action buttons", - "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\".", + "send a notification with icon, severity, title, reference id, click action, media attachment and action buttons", + "Sends a notification to a specific cloud user. Optionally add an icon, severity, title, reference id, on click action, media to attach, and up to 3 action buttons with a format of \"Title=Action\".", null, Visibility.VISIBLE, null, null); private static final ModuleType SEND_BROADCAST_NOTIFICATION_ACTION = new ActionType( @@ -70,8 +70,8 @@ SendBroadcastNotificationActionHandler.TYPE_ID, getNotificationConfig(ConfigType private static final ModuleType SEND_EXTENDED2_BROADCAST_NOTIFICATION_ACTION = new ActionType( SendBroadcastNotificationActionHandler.EXTENDED2_TYPE_ID, getNotificationConfig(ConfigType.EXTENDED2, false, null), - "broadcast a notification with with icon, severity, title, on click action, media attachment and action buttons", - "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\".", + "broadcast a notification with with icon, severity, title, reference id, on click action, media attachment and action buttons", + "Sends a notification to all devices of all users. Optionally add an icon, severity, title, reference id, click action, media to attach, and up to 3 action buttons with a format of \"Title=Action\".", null, Visibility.VISIBLE, null, null); private static final ModuleType SEND_LOG_NOTIFICATION_ACTION = new ActionType( @@ -86,10 +86,37 @@ SendLogNotificationActionHandler.EXTENDED_TYPE_ID, getNotificationConfig(ConfigT "Sends a log notification to the openHAB Cloud instance. Optionally add an icon or the severity. Notifications are NOT sent to any registered devices.", null, Visibility.VISIBLE, null, null); + private static final ModuleType HIDE_NOTIFICATION_BY_REFERENCE_ID_ACTION = new ActionType( + HideNotificationByReferenceIdActionHandler.TYPE_ID, + getNotificationConfig(ConfigType.HIDE_BY_REF, true, null), + "hide notifications by reference id for all devices of a specific user", + "Hide notifications by reference id for all devices of a specific user.", null, Visibility.VISIBLE, null, + null); + + private static final ModuleType HIDE_BROADCAST_NOTIFICATION_BY_REFERENCE_ID_ACTION = new ActionType( + HideBroadcastNotificationByReferenceIdActionHandler.TYPE_ID, + getNotificationConfig(ConfigType.HIDE_BY_REF, false, null), + "hide notifications by reference id for all users and devices", + "Hide notifications by reference id for all users and devices.", null, Visibility.VISIBLE, null, null); + + private static final ModuleType HIDE_NOTIFICATION_BY_SEVERITY_ACTION = new ActionType( + HideNotificationBySeverityActionHandler.TYPE_ID, getNotificationConfig(ConfigType.HIDE_BY_SEV, true, null), + "hide notifications by severity for all devices of a specific user", + "Hide notifications by severity id for all devices of a specific user.", null, Visibility.VISIBLE, null, + null); + + private static final ModuleType HIDE_BROADCAST_NOTIFICATION_BY_SEVERITY_ACTION = new ActionType( + HideBroadcastNotificationBySeverityActionHandler.TYPE_ID, + getNotificationConfig(ConfigType.HIDE_BY_SEV, false, null), + "hide notifications by severity id for all users and devices", + "Hide notifications by severity id for all users and devices.", null, Visibility.VISIBLE, null, null); + private static final List MODULE_TYPES = List.of(SEND_NOTIFICATION_ACTION, SEND_EXTENDED_NOTIFICATION_ACTION, SEND_EXTENDED2_NOTIFICATION_ACTION, SEND_BROADCAST_NOTIFICATION_ACTION, SEND_EXTENDED_BROADCAST_NOTIFICATION_ACTION, SEND_EXTENDED2_BROADCAST_NOTIFICATION_ACTION, - SEND_LOG_NOTIFICATION_ACTION, SEND_EXTENDED_LOG_NOTIFICATION_ACTION); + SEND_LOG_NOTIFICATION_ACTION, SEND_EXTENDED_LOG_NOTIFICATION_ACTION, + HIDE_BROADCAST_NOTIFICATION_BY_REFERENCE_ID_ACTION, HIDE_BROADCAST_NOTIFICATION_BY_SEVERITY_ACTION, + HIDE_NOTIFICATION_BY_REFERENCE_ID_ACTION, HIDE_NOTIFICATION_BY_SEVERITY_ACTION); @SuppressWarnings("unchecked") @Override @@ -111,6 +138,14 @@ SendLogNotificationActionHandler.EXTENDED_TYPE_ID, getNotificationConfig(ConfigT return SEND_LOG_NOTIFICATION_ACTION; case SendLogNotificationActionHandler.EXTENDED_TYPE_ID: return SEND_EXTENDED_LOG_NOTIFICATION_ACTION; + case HideNotificationBySeverityActionHandler.TYPE_ID: + return HIDE_NOTIFICATION_BY_SEVERITY_ACTION; + case HideBroadcastNotificationBySeverityActionHandler.TYPE_ID: + return HIDE_BROADCAST_NOTIFICATION_BY_SEVERITY_ACTION; + case HideNotificationByReferenceIdActionHandler.TYPE_ID: + return HIDE_NOTIFICATION_BY_REFERENCE_ID_ACTION; + case HideBroadcastNotificationByReferenceIdActionHandler.TYPE_ID: + return HIDE_BROADCAST_NOTIFICATION_BY_REFERENCE_ID_ACTION; default: return null; } @@ -143,6 +178,7 @@ private static List getNotificationConfig(ConfigType } if (type == ConfigType.EXTENDED2) { params.add(getTitleConfigParameter(locale)); + params.add(getReferenceIdConfigParameter(locale)); params.add(getonClickActionConfigParameter(locale)); params.add(getMediaAttachmentUrlConfigParameter(locale)); params.add(getActionButton1ConfigParameter(locale)); @@ -150,6 +186,13 @@ private static List getNotificationConfig(ConfigType params.add(getActionButton3ConfigParameter(locale)); } + if (type == ConfigType.HIDE_BY_SEV) { + params.add(getSeverityConfigParameter(locale)); + } + + if (type == ConfigType.HIDE_BY_REF) { + params.add(getReferenceIdConfigParameter(locale)); + } return params; } @@ -173,6 +216,11 @@ private static ConfigDescriptionParameter getTitleConfigParameter(@Nullable Loca .withLabel("Title").withDescription("The title of the notification.").build(); } + private static ConfigDescriptionParameter getReferenceIdConfigParameter(@Nullable Locale locale) { + return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_REFERENCE_ID, Type.TEXT) + .withLabel("Reference Id").withDescription("A reference Id for the notification.").build(); + } + private static ConfigDescriptionParameter getonClickActionConfigParameter(@Nullable Locale locale) { return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_ON_CLICK_ACTION, Type.TEXT) .withLabel("On Click Action").withDescription("The action to perform when clicked.").build(); @@ -215,6 +263,8 @@ public void removeProviderChangeListener(ProviderChangeListener list private enum ConfigType { NOT_EXTENDED, EXTENDED, - EXTENDED2; + EXTENDED2, + HIDE_BY_SEV, + HIDE_BY_REF; } } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationModuleHandlerFactory.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationModuleHandlerFactory.java index 812149c814350..091b0c70c7334 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationModuleHandlerFactory.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationModuleHandlerFactory.java @@ -43,7 +43,10 @@ public class NotificationModuleHandlerFactory extends BaseModuleHandlerFactory { SendNotificationActionHandler.EXTENDED_TYPE_ID, SendNotificationActionHandler.EXTENDED2_TYPE_ID, SendBroadcastNotificationActionHandler.TYPE_ID, SendBroadcastNotificationActionHandler.EXTENDED_TYPE_ID, SendBroadcastNotificationActionHandler.EXTENDED2_TYPE_ID, SendLogNotificationActionHandler.TYPE_ID, - SendLogNotificationActionHandler.EXTENDED_TYPE_ID); + SendLogNotificationActionHandler.EXTENDED_TYPE_ID, + HideBroadcastNotificationByReferenceIdActionHandler.TYPE_ID, + HideBroadcastNotificationBySeverityActionHandler.TYPE_ID, + HideNotificationByReferenceIdActionHandler.TYPE_ID, HideNotificationBySeverityActionHandler.TYPE_ID); private final CloudService cloudService; @Activate @@ -77,6 +80,14 @@ public Collection getTypes() { case SendLogNotificationActionHandler.TYPE_ID: case SendLogNotificationActionHandler.EXTENDED_TYPE_ID: return new SendLogNotificationActionHandler(action, cloudService); + case HideBroadcastNotificationByReferenceIdActionHandler.TYPE_ID: + return new HideBroadcastNotificationByReferenceIdActionHandler(action, cloudService); + case HideNotificationByReferenceIdActionHandler.TYPE_ID: + return new HideNotificationByReferenceIdActionHandler(action, cloudService); + case HideBroadcastNotificationBySeverityActionHandler.TYPE_ID: + return new HideBroadcastNotificationBySeverityActionHandler(action, cloudService); + case HideNotificationBySeverityActionHandler.TYPE_ID: + return new HideNotificationBySeverityActionHandler(action, cloudService); default: break; } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendBroadcastNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendBroadcastNotificationActionHandler.java index 8bb28c3b88a76..3e8b8f1571447 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendBroadcastNotificationActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendBroadcastNotificationActionHandler.java @@ -40,8 +40,8 @@ public SendBroadcastNotificationActionHandler(Action module, CloudService cloudS @Override public @Nullable Map execute(Map context) { - cloudService.sendBroadcastNotification(message, icon, severity, title, onClickAction, mediaAttachmentUrl, - actionButton1, actionButton2, actionButton3); + cloudService.sendBroadcastNotification(message, icon, severity, title, referenceId, onClickAction, + mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); return null; } } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java index 5ce6ac44b8aca..a95ca71ca9dcc 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java @@ -50,8 +50,8 @@ public SendNotificationActionHandler(Action module, CloudService cloudService) { @Override public @Nullable Map execute(Map context) { - cloudService.sendNotification(userId, message, icon, severity, title, onClickAction, mediaAttachmentUrl, - actionButton1, actionButton2, actionButton3); + cloudService.sendNotification(userId, message, icon, severity, title, referenceId, onClickAction, + mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); return null; } } From 95bd73fe3cbbdead1b9012adb2878a3d3ec3a7f4 Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Tue, 2 Jul 2024 12:02:59 -0700 Subject: [PATCH 2/8] Adds support for using tag over severity on extended actions Signed-off-by: Dan Cunningham --- bundles/org.openhab.io.openhabcloud/README.md | 18 +-- .../io/openhabcloud/NotificationAction.java | 33 +++--- .../io/openhabcloud/internal/CloudClient.java | 54 +++++---- .../openhabcloud/internal/CloudService.java | 34 +++--- .../BaseHideNotificationActionHandler.java | 6 +- .../BaseNotificationActionHandler.java | 3 + ...adcastNotificationByTagActionHandler.java} | 8 +- ...> HideNotificationByTagActionHandler.java} | 8 +- .../NotificationActionTypeProvider.java | 109 ++++++++++-------- .../NotificationModuleHandlerFactory.java | 12 +- ...endBroadcastNotificationActionHandler.java | 4 +- .../SendNotificationActionHandler.java | 4 +- 12 files changed, 154 insertions(+), 139 deletions(-) rename bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/{HideBroadcastNotificationBySeverityActionHandler.java => HideBroadcastNotificationByTagActionHandler.java} (78%) rename bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/{HideNotificationBySeverityActionHandler.java => HideNotificationByTagActionHandler.java} (84%) diff --git a/bundles/org.openhab.io.openhabcloud/README.md b/bundles/org.openhab.io.openhabcloud/README.md index f4686b4939060..11b4c66a1dceb 100644 --- a/bundles/org.openhab.io.openhabcloud/README.md +++ b/bundles/org.openhab.io.openhabcloud/README.md @@ -129,12 +129,12 @@ There are four different actions available: To specify media attachments and actions, there is another variant of the `sendNotification` and `sendBroadcastNotification` actions: -- `sendNotification(emailAddress, message, icon, severity, title, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)` -- `sendBroadcastNotification(message, icon, severity, title, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)` +- `sendNotification(emailAddress, message, icon, tag, title, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)` +- `sendBroadcastNotification(message, icon, tag, title, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)` The additional parameter for these variants have the following meaning: - +- `tag` : This renames the `severity` parameter, both are functionally identical. - `title`: The title of the notification. Defaults to "openHAB" inside the Android and iOS apps. - `referenceId`: A user supplied id to both replace existing messages, and later remove messages with this id. - `onClickAction`: The action to be performed when the user clicks on the notification. Specified using the [action syntax](#action-syntax). @@ -147,12 +147,12 @@ These parameters may be skipped by setting them to `null`. ### Hide Notification Actions -There are also actions to hide existing notifications, either by `referenceId` or `severity` +There are also actions to hide existing notifications, either by `referenceId` or `tag` (formally severity) - `hideNotificationByReferenceId(emailAddress, referenceId)` - `hideBroadcastNotificationByReferenceId(referenceId)` -- `hideNotificationBySeverity(emailAddress, severity)` -- `hideBroadcastNotificationBySeverity(severity)` +- `hideNotificationByTag(emailAddress, tag)` +- `hideBroadcastNotificationByTag(tag)` #### Action Syntax @@ -282,7 +282,7 @@ when Item Apartment_MotionSensor changed to ON then sendBroadcastNotification("Motion detected in the apartment!", "motion", "MEDIUM", - "Motion Detected", null, null, "https://apartment.my/camera-snapshot.jpg", + "Motion Detected", "Motion Tag", "motion-id-1234", "https://apartment.my/camera-snapshot.jpg", "Turn on the light=command:Apartment_Light:ON", null, null) end ``` @@ -295,7 +295,7 @@ end rules.when().item('Apartment_MotionSensor').changed().to('ON').then(() => { actions.notificationBuilder('Motion detected in the apartment!') .withIcon('motion') - .withSeverity('MEDIUM') + .withTag('motion-tag') .withTitle('Motion Detected') .withMediaAttachment('https://apartment.my/camera-snapshot.jpg') .addActionButton('Turn on the light=command:Apartment_Light:ON') @@ -313,7 +313,7 @@ rule "Motion Detected Notification" do run do notify "Motion detected in the apartment!", icon: "motion", - severity: "MEDIUM", + tag: "motion-tag", title: "Motion Detected", attachment: "https://apartment.my/camera-snapshot.jpg", buttons: { "Turn on the light" => "command:Apartment_Light:ON" } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java index 068cf28a51a08..16bb694801160 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java @@ -52,13 +52,12 @@ public static void sendNotification(String userId, String message) { * @param userId the cloud user id of the recipient * @param message the body of the notification * @param icon name for the notification - * @param severity category for the notification + * @param tag for the notification (formally severity) * */ @ActionDoc(text = "Sends a push notification to mobile devices of user with userId") - public static void sendNotification(String userId, String message, @Nullable String icon, - @Nullable String severity) { - sendNotification(userId, message, icon, severity, null, null, null, null, null, null, null); + public static void sendNotification(String userId, String message, @Nullable String icon, @Nullable String tag) { + sendNotification(userId, message, icon, tag, null, null, null, null, null, null, null); } /** @@ -136,12 +135,12 @@ public static void sendBroadcastNotification(String message) { * * @param message the body of the notification * @param icon name for the notification - * @param severity category for the notification + * @param tag for the notification (formally severity) * */ @ActionDoc(text = "Sends a broadcast notification to all mobile devices of all account users") - public static void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String severity) { - sendBroadcastNotification(message, icon, severity, null, null, null, null, null, null, null); + public static void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String tag) { + sendBroadcastNotification(message, icon, tag, null, null, null, null, null, null, null); } /** @@ -200,29 +199,29 @@ public static void hideBroadcastNotificationByReferenceId(String referenceId) { } /** - * Hides notifications that are associated with a severity group to all mobile devices of a single user. + * Hides notifications that are associated with a tag to all mobile devices of a single user. * * @param userId the cloud user id of the recipient - * @param severity the severity group + * @param tag the tag associated with notifications * */ - @ActionDoc(text = "Hides notifications that are associated with a severity group to mobile devices of user with userId") - public static void hideNotificationBySeverity(String userId, String severity) { + @ActionDoc(text = "Hides notifications that are associated with a tag to mobile devices of user with userId") + public static void hideNotificationByTag(String userId, String tag) { if (cloudService != null) { - cloudService.hideNotificationBySeverity(userId, severity); + cloudService.hideNotificationByTag(userId, tag); } } /** - * Hides notifications that are associated with a severity group to all mobile devices of all users of the account + * Hides notifications that are associated with a tag to all mobile devices of all users of the account * - * @param severity the severity group + * @param tag the tag associated with notifications * */ - @ActionDoc(text = "Hides notifications that are associated with a severity group to all mobile devices of all account users") - public static void hideBroadcastNotificationBySeverity(String severity) { + @ActionDoc(text = "Hides notifications that are associated with a tag to all mobile devices of all account users") + public static void hideBroadcastNotificationByTag(String tag) { if (cloudService != null) { - cloudService.hideBroadcastNotificationBySeverity(severity); + cloudService.hideBroadcastNotificationByTag(tag); } } } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java index fe578e1eed1e1..932a6e053b9ad 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java @@ -588,7 +588,7 @@ private void handleCommandEvent(JSONObject data) { * @param userId openHAB Cloud user id * @param message notification message text * @param icon name of the icon for this notification - * @param severity severity name for this notification + * @param tag name for this notification (formally severity) * @param title for the notification * @param referenceId an identifier used to collapse and hide notifications * @param onClickAction the action to perform when clicked @@ -597,11 +597,11 @@ private void handleCommandEvent(JSONObject data) { * @param actionButton2 an action button in the format "Title=Action" * @param actionButton3 an action button in the format "Title=Action" */ - public void sendNotification(String userId, String message, @Nullable String icon, @Nullable String severity, + public void sendNotification(String userId, String message, @Nullable String icon, @Nullable String tag, @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) { - sendNotificationInternal(userId, message, icon, severity, title, referenceId, onClickAction, mediaAttachmentUrl, + sendNotificationInternal(userId, message, icon, tag, title, referenceId, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); } @@ -610,7 +610,7 @@ public void sendNotification(String userId, String message, @Nullable String ico * * @param message notification message text * @param icon name of the icon for this notification - * @param severity severity name for this notification + * @param tag name for this notification (formally severity) * @param title for this notification * @param referenceId an identifier used to collapse and hide notifications * @param onClickAction the action to perform when clicked @@ -619,18 +619,18 @@ public void sendNotification(String userId, String message, @Nullable String ico * @param actionButton2 an action button in the format "Title=Action" * @param actionButton3 an action button in the format "Title=Action" */ - public void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String severity, + public void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String tag, @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) { - sendNotificationInternal(null, message, icon, severity, title, referenceId, onClickAction, mediaAttachmentUrl, + sendNotificationInternal(null, message, icon, tag, title, referenceId, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); } private void sendNotificationInternal(@Nullable String userId, String message, @Nullable String icon, - @Nullable String severity, @Nullable String title, @Nullable String referenceId, - @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, - @Nullable String actionButton2, @Nullable String actionButton3) { + @Nullable String tag, @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, + @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, + @Nullable String actionButton3) { if (isConnected()) { JSONObject notificationMessage = new JSONObject(); try { @@ -640,10 +640,15 @@ private void sendNotificationInternal(@Nullable String userId, String message, @ notificationMessage.put("type", "notification"); notificationMessage.put("message", message); - notificationMessage.put("icon", icon); - notificationMessage.put("severity", severity); + + if (icon != null) { + notificationMessage.put("icon", icon); + } + if (tag != null) { + notificationMessage.put("tag", tag); + } if (referenceId != null) { - notificationMessage.put("reference-id", title); + notificationMessage.put("reference-id", referenceId); } if (title != null) { notificationMessage.put("title", title); @@ -710,26 +715,25 @@ public void hideBroadcastNotificationByReferenceId(String referenceId) { } /** - * This method hides a notification by its severity group to for all users + * This method hides a notification by its tag for all users * * @param userId openHAB Cloud user id - * @param severity severity name for this notification + * @param tag severity name for this notification */ - public void hideNotificationBySeverity(String userId, String severity) { - hideNotificationInternal(userId, null, severity); + public void hideNotificationByTag(String userId, String tag) { + hideNotificationInternal(userId, null, tag); } /** - * This method hides a notification by its severity group to for all users + * This method hides a notification by its tag for all users * - * @param severity severity name for this notification + * @param tag name for this notification */ - public void hideBroadcastNotificationBySeverity(String severity) { - hideNotificationInternal(null, null, severity); + public void hideBroadcastNotificationByTag(String tag) { + hideNotificationInternal(null, null, tag); } - private void hideNotificationInternal(@Nullable String userId, @Nullable String referenceId, - @Nullable String severity) { + private void hideNotificationInternal(@Nullable String userId, @Nullable String referenceId, @Nullable String tag) { if (isConnected()) { JSONObject notificationMessage = new JSONObject(); try { @@ -739,10 +743,10 @@ private void hideNotificationInternal(@Nullable String userId, @Nullable String notificationMessage.put("userId", userId); } if (referenceId != null) { - notificationMessage.put("referenceId", referenceId); + notificationMessage.put("reference-id", referenceId); } - if (severity != null) { - notificationMessage.put("severity", severity); + if (tag != null) { + notificationMessage.put("tag", tag); } socket.emit(userId == null ? "broadcastnotification" : "notification", notificationMessage); } catch (JSONException e) { diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java index e9f0c09db1eab..fc90c384415db 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java @@ -114,7 +114,7 @@ public CloudService(final @Reference HttpClientFactory httpClientFactory, * @param userId the {@link String} containing the openHAB Cloud user id to send message to * @param message the {@link String} containing a message to send to specified user id * @param icon the {@link String} containing a name of the icon to be used with this notification - * @param severity the {@link String} containing severity (good, info, warning, error) of notification + * @param tag the {@link String} containing tag of notification (formally severity) * @param title the {@link String} containing the title to be used with this notification * @param referenceId the {@link String} identifier used to collapse and hide notifications * @param onClickAction the {@link String} containing the action to perform when clicked @@ -123,13 +123,13 @@ public CloudService(final @Reference HttpClientFactory httpClientFactory, * @param actionButton2 the {@link String} containing the action button in the format "Title=Action" * @param actionButton3 the {@link String} containing the action button in the format "Title=Action" */ - public void sendNotification(String userId, String message, @Nullable String icon, @Nullable String severity, + public void sendNotification(String userId, String message, @Nullable String icon, @Nullable String tag, @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) { logger.debug("Sending message '{}' to user id {}", message, userId); - cloudClient.sendNotification(userId, message, icon, severity, title, referenceId, onClickAction, - mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); + cloudClient.sendNotification(userId, message, icon, tag, title, referenceId, onClickAction, mediaAttachmentUrl, + actionButton1, actionButton2, actionButton3); } /** @@ -151,7 +151,7 @@ public void sendLogNotification(String message, @Nullable String icon, @Nullable * * @param message the {@link String} containing a message to send to specified user id * @param icon the {@link String} containing a name of the icon to be used with this notification - * @param severity the {@link String} containing severity (good, info, warning, error) of notification + * @param tag the {@link String} containing tag of notification (formally severity) * @param title the {@link String} containing the title to be used with this notification * @param referenceId the {@link String} identifier used to collapse and hide notifications * @param onClickAction the {@link String} containing the action to perform when clicked @@ -160,37 +160,37 @@ public void sendLogNotification(String message, @Nullable String icon, @Nullable * @param actionButton2 the {@link String} containing the action button in the format "Title=Action" * @param actionButton3 the {@link String} containing the action button in the format "Title=Action" */ - public void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String severity, + public void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String tag, @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) { logger.debug("Sending broadcast message '{}' to all users", message); - cloudClient.sendBroadcastNotification(message, icon, severity, title, referenceId, onClickAction, - mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); + cloudClient.sendBroadcastNotification(message, icon, tag, title, referenceId, onClickAction, mediaAttachmentUrl, + actionButton1, actionButton2, actionButton3); } /** - * This method hides a notification by its severity group through the openHAB Cloud service + * This method hides a notification by its tag through the openHAB Cloud service * * @param userId the {@link String} containing the openHAB Cloud user id to hide messages for - * @param severity the {@link String} containing severity group of notification + * @param tag the {@link String} containing severity group of notification * */ - public void hideNotificationBySeverity(String userId, String severity) { - logger.debug("hiding with severity '{}' to user id {}", severity, userId); - cloudClient.hideNotificationBySeverity(userId, severity); + public void hideNotificationByTag(String userId, String tag) { + logger.debug("hiding with tag '{}' to user id {}", tag, userId); + cloudClient.hideNotificationByTag(userId, tag); } /** - * This method hides a notification by its severity group through the openHAB Cloud service to all + * This method hides a notification by its tag through the openHAB Cloud service to all * mobile devices of all users of the account * * @param severity the {@link String} containing severity (good, info, warning, error) of notification * */ - public void hideBroadcastNotificationBySeverity(String severity) { - logger.debug("hiding broadcast with severity '{}'", severity); - cloudClient.hideBroadcastNotificationBySeverity(severity); + public void hideBroadcastNotificationByTag(String tag) { + logger.debug("hiding broadcast with tag '{}'", tag); + cloudClient.hideBroadcastNotificationByTag(tag); } /** diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseHideNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseHideNotificationActionHandler.java index 31be4375f021d..83f4c161cfef9 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseHideNotificationActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseHideNotificationActionHandler.java @@ -28,17 +28,17 @@ @NonNullByDefault public abstract class BaseHideNotificationActionHandler extends BaseActionModuleHandler { - public static final String PARAM_SEVERITY = "severity"; + public static final String PARAM_TAG = "tag"; public static final String PARAM_REFERENCE_ID = "referenceId"; protected final CloudService cloudService; - protected final @Nullable String severity; + protected final @Nullable String tag; protected final @Nullable String referenceId; public BaseHideNotificationActionHandler(Action module, CloudService cloudService) { super(module); this.cloudService = cloudService; - this.severity = stringConfig(PARAM_SEVERITY); + this.tag = stringConfig(PARAM_TAG); this.referenceId = stringConfig(PARAM_REFERENCE_ID); } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java index de83286fd9caf..e43ac927c9f32 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java @@ -32,6 +32,7 @@ public abstract class BaseNotificationActionHandler extends BaseActionModuleHand public static final String PARAM_MESSAGE = "message"; public static final String PARAM_ICON = "icon"; public static final String PARAM_SEVERITY = "severity"; + public static final String PARAM_TAG = "tag"; public static final String PARAM_TITLE = "title"; public static final String PARAM_REFERENCE_ID = "referenceId"; public static final String PARAM_ON_CLICK_ACTION = "onClickAction"; @@ -45,6 +46,7 @@ public abstract class BaseNotificationActionHandler extends BaseActionModuleHand protected final String message; protected final @Nullable String icon; protected final @Nullable String severity; + protected final @Nullable String tag; protected final @Nullable String title; protected final @Nullable String referenceId; protected final @Nullable String onClickAction; @@ -66,6 +68,7 @@ public BaseNotificationActionHandler(Action module, CloudService cloudService) { this.icon = stringConfig(PARAM_ICON); this.severity = stringConfig(PARAM_SEVERITY); + this.tag = stringConfig(PARAM_TAG); this.title = stringConfig(PARAM_TITLE); this.referenceId = stringConfig(PARAM_REFERENCE_ID); this.onClickAction = stringConfig(PARAM_ON_CLICK_ACTION); diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationBySeverityActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationByTagActionHandler.java similarity index 78% rename from bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationBySeverityActionHandler.java rename to bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationByTagActionHandler.java index e419246c19fbd..4bde5ac4fe405 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationBySeverityActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideBroadcastNotificationByTagActionHandler.java @@ -26,17 +26,17 @@ * @author Dan Cunningham - Initial contribution */ @NonNullByDefault -public class HideBroadcastNotificationBySeverityActionHandler extends BaseHideNotificationActionHandler { +public class HideBroadcastNotificationByTagActionHandler extends BaseHideNotificationActionHandler { - public static final String TYPE_ID = "notification.HideBroadcastNotificationBySeverity"; + public static final String TYPE_ID = "notification.HideBroadcastNotificationByTag"; - public HideBroadcastNotificationBySeverityActionHandler(Action module, CloudService cloudService) { + public HideBroadcastNotificationByTagActionHandler(Action module, CloudService cloudService) { super(module, cloudService); } @Override public @Nullable Map execute(Map context) { - cloudService.hideBroadcastNotificationBySeverity(severity); + cloudService.hideBroadcastNotificationByTag(tag); return null; } } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationBySeverityActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByTagActionHandler.java similarity index 84% rename from bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationBySeverityActionHandler.java rename to bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByTagActionHandler.java index e57e79bf1f742..1580fded0c632 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationBySeverityActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByTagActionHandler.java @@ -26,15 +26,15 @@ * @author Dan Cunningham - Initial contribution */ @NonNullByDefault -public class HideNotificationBySeverityActionHandler extends BaseHideNotificationActionHandler { +public class HideNotificationByTagActionHandler extends BaseHideNotificationActionHandler { - public static final String TYPE_ID = "notification.HideNotificationBySeverity"; + public static final String TYPE_ID = "notification.HideNotificationByTag"; public static final String PARAM_USER = "userId"; private final String userId; - public HideNotificationBySeverityActionHandler(Action module, CloudService cloudService) { + public HideNotificationByTagActionHandler(Action module, CloudService cloudService) { super(module, cloudService); Object userIdParam = module.getConfiguration().get(PARAM_USER); @@ -47,7 +47,7 @@ public HideNotificationBySeverityActionHandler(Action module, CloudService cloud @Override public @Nullable Map execute(Map context) { - cloudService.hideNotificationBySeverity(userId, severity); + cloudService.hideNotificationByTag(userId, tag); return null; } } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationActionTypeProvider.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationActionTypeProvider.java index 7d94cec67851f..311f05cacecbb 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationActionTypeProvider.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationActionTypeProvider.java @@ -46,14 +46,14 @@ public class NotificationActionTypeProvider implements ModuleTypeProvider { private static final ModuleType SEND_EXTENDED_NOTIFICATION_ACTION = new ActionType( SendNotificationActionHandler.EXTENDED_TYPE_ID, getNotificationConfig(ConfigType.EXTENDED, true, null), - "send a notification with icon and severity", - "Sends a notification to a specific cloud user. Optionally add an icon or the severity.", null, + "send a notification with icon and severity (tag)", + "Sends a notification to a specific cloud user. Optionally add an icon or the severity (tag).", null, Visibility.VISIBLE, null, null); private static final ModuleType SEND_EXTENDED2_NOTIFICATION_ACTION = new ActionType( SendNotificationActionHandler.EXTENDED2_TYPE_ID, getNotificationConfig(ConfigType.EXTENDED2, true, null), - "send a notification with icon, severity, title, reference id, click action, media attachment and action buttons", - "Sends a notification to a specific cloud user. Optionally add an icon, severity, title, reference id, on click action, media to attach, and up to 3 action buttons with a format of \"Title=Action\".", + "send a notification with icon, tag, title, reference id, click action, media attachment and action buttons", + "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\".", null, Visibility.VISIBLE, null, null); private static final ModuleType SEND_BROADCAST_NOTIFICATION_ACTION = new ActionType( @@ -63,15 +63,16 @@ SendBroadcastNotificationActionHandler.TYPE_ID, getNotificationConfig(ConfigType private static final ModuleType SEND_EXTENDED_BROADCAST_NOTIFICATION_ACTION = new ActionType( SendBroadcastNotificationActionHandler.EXTENDED_TYPE_ID, - getNotificationConfig(ConfigType.EXTENDED, false, null), "broadcast a notification with icon and severity", - "Sends a notification to all devices of all users. Optionally add an icon or the severity.", null, + getNotificationConfig(ConfigType.EXTENDED, false, null), + "broadcast a notification with icon and severity (tag)", + "Sends a notification to all devices of all users. Optionally add an icon or the severity (tag).", null, Visibility.VISIBLE, null, null); private static final ModuleType SEND_EXTENDED2_BROADCAST_NOTIFICATION_ACTION = new ActionType( SendBroadcastNotificationActionHandler.EXTENDED2_TYPE_ID, getNotificationConfig(ConfigType.EXTENDED2, false, null), - "broadcast a notification with with icon, severity, title, reference id, on click action, media attachment and action buttons", - "Sends a notification to all devices of all users. Optionally add an icon, severity, title, reference id, click action, media to attach, and up to 3 action buttons with a format of \"Title=Action\".", + "broadcast a notification with with icon, tag, title, reference id, on click action, media attachment and action buttons", + "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\".", null, Visibility.VISIBLE, null, null); private static final ModuleType SEND_LOG_NOTIFICATION_ACTION = new ActionType( @@ -82,8 +83,8 @@ SendLogNotificationActionHandler.TYPE_ID, getNotificationConfig(ConfigType.NOT_E private static final ModuleType SEND_EXTENDED_LOG_NOTIFICATION_ACTION = new ActionType( SendLogNotificationActionHandler.EXTENDED_TYPE_ID, getNotificationConfig(ConfigType.EXTENDED, false, null), - "send a log message with icon and severity", - "Sends a log notification to the openHAB Cloud instance. Optionally add an icon or the severity. Notifications are NOT sent to any registered devices.", + "send a log message with icon and severity (tag)", + "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.", null, Visibility.VISIBLE, null, null); private static final ModuleType HIDE_NOTIFICATION_BY_REFERENCE_ID_ACTION = new ActionType( @@ -99,24 +100,23 @@ SendLogNotificationActionHandler.EXTENDED_TYPE_ID, getNotificationConfig(ConfigT "hide notifications by reference id for all users and devices", "Hide notifications by reference id for all users and devices.", null, Visibility.VISIBLE, null, null); - private static final ModuleType HIDE_NOTIFICATION_BY_SEVERITY_ACTION = new ActionType( - HideNotificationBySeverityActionHandler.TYPE_ID, getNotificationConfig(ConfigType.HIDE_BY_SEV, true, null), - "hide notifications by severity for all devices of a specific user", - "Hide notifications by severity id for all devices of a specific user.", null, Visibility.VISIBLE, null, - null); + private static final ModuleType HIDE_NOTIFICATION_BY_TAG_ACTION = new ActionType( + HideNotificationByTagActionHandler.TYPE_ID, getNotificationConfig(ConfigType.HIDE_BY_TAG, true, null), + "hide notifications by tag for all devices of a specific user", + "Hide notifications by tag id for all devices of a specific user.", null, Visibility.VISIBLE, null, null); - private static final ModuleType HIDE_BROADCAST_NOTIFICATION_BY_SEVERITY_ACTION = new ActionType( - HideBroadcastNotificationBySeverityActionHandler.TYPE_ID, - getNotificationConfig(ConfigType.HIDE_BY_SEV, false, null), - "hide notifications by severity id for all users and devices", - "Hide notifications by severity id for all users and devices.", null, Visibility.VISIBLE, null, null); + private static final ModuleType HIDE_BROADCAST_NOTIFICATION_BY_TAG_ACTION = new ActionType( + HideBroadcastNotificationByTagActionHandler.TYPE_ID, + getNotificationConfig(ConfigType.HIDE_BY_TAG, false, null), + "hide notifications by tag id for all users and devices", + "Hide notifications by tag id for all users and devices.", null, Visibility.VISIBLE, null, null); private static final List MODULE_TYPES = List.of(SEND_NOTIFICATION_ACTION, SEND_EXTENDED_NOTIFICATION_ACTION, SEND_EXTENDED2_NOTIFICATION_ACTION, SEND_BROADCAST_NOTIFICATION_ACTION, SEND_EXTENDED_BROADCAST_NOTIFICATION_ACTION, SEND_EXTENDED2_BROADCAST_NOTIFICATION_ACTION, SEND_LOG_NOTIFICATION_ACTION, SEND_EXTENDED_LOG_NOTIFICATION_ACTION, - HIDE_BROADCAST_NOTIFICATION_BY_REFERENCE_ID_ACTION, HIDE_BROADCAST_NOTIFICATION_BY_SEVERITY_ACTION, - HIDE_NOTIFICATION_BY_REFERENCE_ID_ACTION, HIDE_NOTIFICATION_BY_SEVERITY_ACTION); + HIDE_BROADCAST_NOTIFICATION_BY_REFERENCE_ID_ACTION, HIDE_BROADCAST_NOTIFICATION_BY_TAG_ACTION, + HIDE_NOTIFICATION_BY_REFERENCE_ID_ACTION, HIDE_NOTIFICATION_BY_TAG_ACTION); @SuppressWarnings("unchecked") @Override @@ -138,10 +138,10 @@ HideNotificationBySeverityActionHandler.TYPE_ID, getNotificationConfig(ConfigTyp return SEND_LOG_NOTIFICATION_ACTION; case SendLogNotificationActionHandler.EXTENDED_TYPE_ID: return SEND_EXTENDED_LOG_NOTIFICATION_ACTION; - case HideNotificationBySeverityActionHandler.TYPE_ID: - return HIDE_NOTIFICATION_BY_SEVERITY_ACTION; - case HideBroadcastNotificationBySeverityActionHandler.TYPE_ID: - return HIDE_BROADCAST_NOTIFICATION_BY_SEVERITY_ACTION; + case HideNotificationByTagActionHandler.TYPE_ID: + return HIDE_NOTIFICATION_BY_TAG_ACTION; + case HideBroadcastNotificationByTagActionHandler.TYPE_ID: + return HIDE_BROADCAST_NOTIFICATION_BY_TAG_ACTION; case HideNotificationByReferenceIdActionHandler.TYPE_ID: return HIDE_NOTIFICATION_BY_REFERENCE_ID_ACTION; case HideBroadcastNotificationByReferenceIdActionHandler.TYPE_ID: @@ -170,28 +170,32 @@ private static List getNotificationConfig(ConfigType .withRequired(true).withLabel("User Id").withDescription("The cloud user id of the recipient.") .build()); } - - if (type == ConfigType.EXTENDED || type == ConfigType.EXTENDED2) { - params.add(getMessageConfigParameter(locale)); - params.add(getIconConfigParameter(locale)); - params.add(getSeverityConfigParameter(locale)); - } - if (type == ConfigType.EXTENDED2) { - params.add(getTitleConfigParameter(locale)); - params.add(getReferenceIdConfigParameter(locale)); - params.add(getonClickActionConfigParameter(locale)); - params.add(getMediaAttachmentUrlConfigParameter(locale)); - params.add(getActionButton1ConfigParameter(locale)); - params.add(getActionButton2ConfigParameter(locale)); - params.add(getActionButton3ConfigParameter(locale)); - } - - if (type == ConfigType.HIDE_BY_SEV) { - params.add(getSeverityConfigParameter(locale)); - } - - if (type == ConfigType.HIDE_BY_REF) { - params.add(getReferenceIdConfigParameter(locale)); + switch (type) { + case EXTENDED: + params.add(getMessageConfigParameter(locale)); + params.add(getIconConfigParameter(locale)); + params.add(getSeverityConfigParameter(locale)); + break; + case EXTENDED2: + params.add(getMessageConfigParameter(locale)); + params.add(getIconConfigParameter(locale)); + params.add(getTagConfigParameter(locale)); + params.add(getTitleConfigParameter(locale)); + params.add(getReferenceIdConfigParameter(locale)); + params.add(getonClickActionConfigParameter(locale)); + params.add(getMediaAttachmentUrlConfigParameter(locale)); + params.add(getActionButton1ConfigParameter(locale)); + params.add(getActionButton2ConfigParameter(locale)); + params.add(getActionButton3ConfigParameter(locale)); + break; + case HIDE_BY_TAG: + params.add(getTagConfigParameter(locale)); + break; + case HIDE_BY_REF: + params.add(getReferenceIdConfigParameter(locale)); + break; + default: + break; } return params; } @@ -208,7 +212,12 @@ private static ConfigDescriptionParameter getIconConfigParameter(@Nullable Local private static ConfigDescriptionParameter getSeverityConfigParameter(@Nullable Locale locale) { return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_SEVERITY, Type.TEXT) - .withLabel("Severity").withDescription("The severity of the notification.").build(); + .withLabel("Severity (Tag)").withDescription("The severity/tag of the notification.").build(); + } + + private static ConfigDescriptionParameter getTagConfigParameter(@Nullable Locale locale) { + return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_TAG, Type.TEXT) + .withLabel("Tag").withDescription("The tag of the notification.").build(); } private static ConfigDescriptionParameter getTitleConfigParameter(@Nullable Locale locale) { @@ -264,7 +273,7 @@ private enum ConfigType { NOT_EXTENDED, EXTENDED, EXTENDED2, - HIDE_BY_SEV, + HIDE_BY_TAG, HIDE_BY_REF; } } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationModuleHandlerFactory.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationModuleHandlerFactory.java index 091b0c70c7334..fde56633dfdc5 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationModuleHandlerFactory.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationModuleHandlerFactory.java @@ -45,8 +45,8 @@ public class NotificationModuleHandlerFactory extends BaseModuleHandlerFactory { SendBroadcastNotificationActionHandler.EXTENDED2_TYPE_ID, SendLogNotificationActionHandler.TYPE_ID, SendLogNotificationActionHandler.EXTENDED_TYPE_ID, HideBroadcastNotificationByReferenceIdActionHandler.TYPE_ID, - HideBroadcastNotificationBySeverityActionHandler.TYPE_ID, - HideNotificationByReferenceIdActionHandler.TYPE_ID, HideNotificationBySeverityActionHandler.TYPE_ID); + HideBroadcastNotificationByTagActionHandler.TYPE_ID, HideNotificationByReferenceIdActionHandler.TYPE_ID, + HideNotificationByTagActionHandler.TYPE_ID); private final CloudService cloudService; @Activate @@ -84,10 +84,10 @@ public Collection getTypes() { return new HideBroadcastNotificationByReferenceIdActionHandler(action, cloudService); case HideNotificationByReferenceIdActionHandler.TYPE_ID: return new HideNotificationByReferenceIdActionHandler(action, cloudService); - case HideBroadcastNotificationBySeverityActionHandler.TYPE_ID: - return new HideBroadcastNotificationBySeverityActionHandler(action, cloudService); - case HideNotificationBySeverityActionHandler.TYPE_ID: - return new HideNotificationBySeverityActionHandler(action, cloudService); + case HideBroadcastNotificationByTagActionHandler.TYPE_ID: + return new HideBroadcastNotificationByTagActionHandler(action, cloudService); + case HideNotificationByTagActionHandler.TYPE_ID: + return new HideNotificationByTagActionHandler(action, cloudService); default: break; } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendBroadcastNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendBroadcastNotificationActionHandler.java index 3e8b8f1571447..a4cdfc55309f2 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendBroadcastNotificationActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendBroadcastNotificationActionHandler.java @@ -40,8 +40,8 @@ public SendBroadcastNotificationActionHandler(Action module, CloudService cloudS @Override public @Nullable Map execute(Map context) { - cloudService.sendBroadcastNotification(message, icon, severity, title, referenceId, onClickAction, - mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); + cloudService.sendBroadcastNotification(message, icon, tag == null ? severity : tag, title, referenceId, + onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); return null; } } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java index a95ca71ca9dcc..34ebc2b891a22 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java @@ -50,8 +50,8 @@ public SendNotificationActionHandler(Action module, CloudService cloudService) { @Override public @Nullable Map execute(Map context) { - cloudService.sendNotification(userId, message, icon, severity, title, referenceId, onClickAction, - mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); + cloudService.sendNotification(userId, message, icon, tag == null ? severity : tag, title, referenceId, + onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); return null; } } From 70ba608302a3cb1ec9b016a9c844fa9f9350208c Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Tue, 2 Jul 2024 12:27:23 -0700 Subject: [PATCH 3/8] Update README Signed-off-by: Dan Cunningham --- bundles/org.openhab.io.openhabcloud/README.md | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/bundles/org.openhab.io.openhabcloud/README.md b/bundles/org.openhab.io.openhabcloud/README.md index 11b4c66a1dceb..9a6eb90e99796 100644 --- a/bundles/org.openhab.io.openhabcloud/README.md +++ b/bundles/org.openhab.io.openhabcloud/README.md @@ -108,19 +108,21 @@ The parameters for these actions have the following meaning: - `emailAddress`: String containing the email address the target user is registered with in the cloud instance. - `message`: String containing the notification message text. - `icon`: String containing the icon name (as described in [Items: Icons]({{base}}/configuration/items.html#icons)). -- `severity`: String containing a description of the severity of the incident. +- `severity`: String containing a description of the severity (tag) of the incident. `null` may be used to skip the `icon` or `severity` parameter. -### Title, Reference ID, Media Attachments & Actions +### Title, Tag, Reference Id, Media Attachments & Actions The `sendNotification` and `sendBroadcastNotification` actions additionally support setting a title, reference id, media attachments and actions. -The title is displayed as the notification title on the device and defaults to "openHAB" for the Android and iOS apps. -The reference id is a user supplied identifier that when set will replace messages with the same id on the user's device (so only the last version exists). -The reference id can also be used later with `hideNotificationByReferenceId` and `hideBroadcastNotificationByReferenceId` methods to remove notifications with this id. -Media attachments are displayed together with the notification on the device and can be used to display images, e.g. a camera snapshot. -Actions allow the user to interact with the notification, e.g. to open a specific page in the app or to send a command to an Item. +- The tag is used for tagging messages for grouping when displaying in the app and to hide/remove groups of messages from a user's device. +Tag was previously referred to as "severity". +- The title is displayed as the notification title on the device and defaults to "openHAB" for the Android and iOS apps. +- The reference id is a user supplied identifier that when set will replace messages with the same id on the user's device (so only the last version exists). +The reference id is a user supplied id that can be used to update or remove notifications with this same id. +- Media attachments are displayed together with the notification on the device and can be used to display images, e.g. a camera snapshot. +- Actions allow the user to interact with the notification, e.g. to open a specific page in the app or to send a command to an Item. There are four different actions available: @@ -134,9 +136,9 @@ To specify media attachments and actions, there is another variant of the `sendN The additional parameter for these variants have the following meaning: -- `tag` : This renames the `severity` parameter, both are functionally identical. +- `tag` : A user supplied tag to group messages for removing using the `hideNotificationByTag` action or grouping messages when displayed in the app. This renames the `severity` parameter, both are functionally identical. - `title`: The title of the notification. Defaults to "openHAB" inside the Android and iOS apps. -- `referenceId`: A user supplied id to both replace existing messages, and later remove messages with this id. +- `referenceId`: A user supplied id to both replace existing messages when pushed, and later remove messages with the `hideNotificationReferenceId` actions. - `onClickAction`: The action to be performed when the user clicks on the notification. Specified using the [action syntax](#action-syntax). - `mediaAttachmentUrl`: The URL of the media attachment to be displayed with the notification. This URL must be reachable by the push notification client. - `actionButton1`: The action to be performed when the user clicks on the first action button. Specified as `Title=$action`, where `$action` follows the [action syntax](#action-syntax). From 12a6bad0a1e8d20f6c06c7c3e1ed8d68b57ce3f0 Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Wed, 3 Jul 2024 09:18:20 -0700 Subject: [PATCH 4/8] PR Review Feedback Signed-off-by: Dan Cunningham --- bundles/org.openhab.io.openhabcloud/README.md | 9 ++++--- .../io/openhabcloud/NotificationAction.java | 26 +++++++++---------- .../io/openhabcloud/internal/CloudClient.java | 6 ++--- .../openhabcloud/internal/CloudService.java | 4 +-- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/bundles/org.openhab.io.openhabcloud/README.md b/bundles/org.openhab.io.openhabcloud/README.md index 9a6eb90e99796..019612ff35670 100644 --- a/bundles/org.openhab.io.openhabcloud/README.md +++ b/bundles/org.openhab.io.openhabcloud/README.md @@ -140,7 +140,7 @@ The additional parameter for these variants have the following meaning: - `title`: The title of the notification. Defaults to "openHAB" inside the Android and iOS apps. - `referenceId`: A user supplied id to both replace existing messages when pushed, and later remove messages with the `hideNotificationReferenceId` actions. - `onClickAction`: The action to be performed when the user clicks on the notification. Specified using the [action syntax](#action-syntax). -- `mediaAttachmentUrl`: The URL of the media attachment to be displayed with the notification. This URL must be reachable by the push notification client. +- `mediaAttachmentUrl`: The URL of the media attachment to be displayed with the notification. This can either be a fully qualified URL, prefixed with `http://` or `https://` and reachable by the client device, or an image item with the format `item:MyImageItem` - `actionButton1`: The action to be performed when the user clicks on the first action button. Specified as `Title=$action`, where `$action` follows the [action syntax](#action-syntax). - `actionButton2`: The action to be performed when the user clicks on the second action button. Specified as `Title=$action`, where `$action` follows the [action syntax](#action-syntax). - `actionButton3`: The action to be performed when the user clicks on the third action button. Specified as `Title=$action`, where `$action` follows the [action syntax](#action-syntax). @@ -149,7 +149,7 @@ These parameters may be skipped by setting them to `null`. ### Hide Notification Actions -There are also actions to hide existing notifications, either by `referenceId` or `tag` (formally severity) +There are also actions to hide existing notifications, either by `referenceId` or `tag` (formerly severity) - `hideNotificationByReferenceId(emailAddress, referenceId)` - `hideBroadcastNotificationByReferenceId(referenceId)` @@ -283,8 +283,8 @@ rule "Motion Detected Notification" when Item Apartment_MotionSensor changed to ON then - sendBroadcastNotification("Motion detected in the apartment!", "motion", "MEDIUM", - "Motion Detected", "Motion Tag", "motion-id-1234", "https://apartment.my/camera-snapshot.jpg", + sendBroadcastNotification("Motion detected in the apartment!", "motion", "Motion Tag", + "Motion Detected", "motion-id-1234", null, "https://apartment.my/camera-snapshot.jpg", "Turn on the light=command:Apartment_Light:ON", null, null) end ``` @@ -299,6 +299,7 @@ rules.when().item('Apartment_MotionSensor').changed().to('ON').then(() => { .withIcon('motion') .withTag('motion-tag') .withTitle('Motion Detected') + .withReferenceId('motion-id-1234') .withMediaAttachment('https://apartment.my/camera-snapshot.jpg') .addActionButton('Turn on the light=command:Apartment_Light:ON') .send(); diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java index 16bb694801160..ad1fa963e0fb8 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java @@ -52,7 +52,7 @@ public static void sendNotification(String userId, String message) { * @param userId the cloud user id of the recipient * @param message the body of the notification * @param icon name for the notification - * @param tag for the notification (formally severity) + * @param tag for the notification (formerly severity) * */ @ActionDoc(text = "Sends a push notification to mobile devices of user with userId") @@ -66,7 +66,7 @@ public static void sendNotification(String userId, String message, @Nullable Str * @param userId the cloud user id of the recipient * @param message the body of the notification * @param icon name for the notification - * @param severity category for the notification + * @param tag for the notification * @param title for the notification * @param referenceId an identifier used to collapse and hide notifications * @param onClickAction the action to perform when clicked @@ -77,13 +77,13 @@ public static void sendNotification(String userId, String message, @Nullable Str * */ @ActionDoc(text = "Sends a push notification to mobile devices of user with userId") - public static void sendNotification(String userId, String message, @Nullable String icon, @Nullable String severity, + public static void sendNotification(String userId, String message, @Nullable String icon, @Nullable String tag, @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) { logger.debug("sending notification '{}' to user {}", message, userId); if (cloudService != null) { - cloudService.sendNotification(userId, message, icon, severity, title, referenceId, onClickAction, + cloudService.sendNotification(userId, message, icon, tag, title, referenceId, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); } } @@ -106,14 +106,14 @@ public static void sendLogNotification(String message) { * * @param message the body of the notification * @param icon name for the notification - * @param severity category for the notification + * @param tag for the notification (formerly severity) * */ @ActionDoc(text = "Sends a log notification which is shown in notifications log to all account users") - public static void sendLogNotification(String message, @Nullable String icon, @Nullable String severity) { + public static void sendLogNotification(String message, @Nullable String icon, @Nullable String tag) { logger.debug("sending log notification '{}'", message); if (cloudService != null) { - cloudService.sendLogNotification(message, icon, severity); + cloudService.sendLogNotification(message, icon, tag); } } @@ -135,7 +135,7 @@ public static void sendBroadcastNotification(String message) { * * @param message the body of the notification * @param icon name for the notification - * @param tag for the notification (formally severity) + * @param tag for the notification (formerly severity) * */ @ActionDoc(text = "Sends a broadcast notification to all mobile devices of all account users") @@ -159,7 +159,7 @@ public static void sendBroadcastNotification(String message, @Nullable String ic * @param actionButton3 an action button in the format "Title=Action" * */ - @ActionDoc(text = "Sends a broadcast notification to all mobile devices of all account user") + @ActionDoc(text = "Sends a broadcast notification to all mobile devices of all account users") public static void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String severity, @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, @@ -178,7 +178,7 @@ public static void sendBroadcastNotification(String message, @Nullable String ic * @param referenceId the user reference id * */ - @ActionDoc(text = "Hides notifications that contain the reference id to mobile devices of user with userId") + @ActionDoc(text = "Hides notifications that contain the reference id on mobile devices of user with userId") public static void hideNotificationByReferenceId(String userId, String referenceId) { if (cloudService != null) { cloudService.hideNotificationByReferenceId(userId, referenceId); @@ -191,7 +191,7 @@ public static void hideNotificationByReferenceId(String userId, String reference * @param referenceId the user reference id * */ - @ActionDoc(text = "Hides notifications that contain the reference id to all mobile devices of all account users") + @ActionDoc(text = "Hides notifications that contain the reference id on all mobile devices of all account users") public static void hideBroadcastNotificationByReferenceId(String referenceId) { if (cloudService != null) { cloudService.hideBroadcastNotificationByReferenceId(referenceId); @@ -205,7 +205,7 @@ public static void hideBroadcastNotificationByReferenceId(String referenceId) { * @param tag the tag associated with notifications * */ - @ActionDoc(text = "Hides notifications that are associated with a tag to mobile devices of user with userId") + @ActionDoc(text = "Hides notifications that are associated with a tag on mobile devices of user with userId") public static void hideNotificationByTag(String userId, String tag) { if (cloudService != null) { cloudService.hideNotificationByTag(userId, tag); @@ -218,7 +218,7 @@ public static void hideNotificationByTag(String userId, String tag) { * @param tag the tag associated with notifications * */ - @ActionDoc(text = "Hides notifications that are associated with a tag to all mobile devices of all account users") + @ActionDoc(text = "Hides notifications that are associated with a tag on all mobile devices of all account users") public static void hideBroadcastNotificationByTag(String tag) { if (cloudService != null) { cloudService.hideBroadcastNotificationByTag(tag); diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java index 932a6e053b9ad..7627d445b3032 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java @@ -588,7 +588,7 @@ private void handleCommandEvent(JSONObject data) { * @param userId openHAB Cloud user id * @param message notification message text * @param icon name of the icon for this notification - * @param tag name for this notification (formally severity) + * @param tag name for this notification (formerly severity) * @param title for the notification * @param referenceId an identifier used to collapse and hide notifications * @param onClickAction the action to perform when clicked @@ -610,7 +610,7 @@ public void sendNotification(String userId, String message, @Nullable String ico * * @param message notification message text * @param icon name of the icon for this notification - * @param tag name for this notification (formally severity) + * @param tag name for this notification (formerly severity) * @param title for this notification * @param referenceId an identifier used to collapse and hide notifications * @param onClickAction the action to perform when clicked @@ -696,7 +696,7 @@ public void sendLogNotification(String message, @Nullable String icon, @Nullable } /** - * This method hides a notification by its reference id to a single user + * This method hides a notification by its reference id for a single user * * @param userId openHAB Cloud user id * @param referenceId the reference id diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java index fc90c384415db..ce9e084eacde9 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java @@ -114,7 +114,7 @@ public CloudService(final @Reference HttpClientFactory httpClientFactory, * @param userId the {@link String} containing the openHAB Cloud user id to send message to * @param message the {@link String} containing a message to send to specified user id * @param icon the {@link String} containing a name of the icon to be used with this notification - * @param tag the {@link String} containing tag of notification (formally severity) + * @param tag the {@link String} containing tag of notification (formerly severity) * @param title the {@link String} containing the title to be used with this notification * @param referenceId the {@link String} identifier used to collapse and hide notifications * @param onClickAction the {@link String} containing the action to perform when clicked @@ -151,7 +151,7 @@ public void sendLogNotification(String message, @Nullable String icon, @Nullable * * @param message the {@link String} containing a message to send to specified user id * @param icon the {@link String} containing a name of the icon to be used with this notification - * @param tag the {@link String} containing tag of notification (formally severity) + * @param tag the {@link String} containing tag of notification (formerly severity) * @param title the {@link String} containing the title to be used with this notification * @param referenceId the {@link String} identifier used to collapse and hide notifications * @param onClickAction the {@link String} containing the action to perform when clicked From 425bb0445ace8f5fcb0ea73e768ca0378235bf9c Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Wed, 3 Jul 2024 09:19:53 -0700 Subject: [PATCH 5/8] PR Review Signed-off-by: Dan Cunningham --- .../org/openhab/io/openhabcloud/NotificationAction.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java index ad1fa963e0fb8..17027c9e5190a 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java @@ -149,7 +149,7 @@ public static void sendBroadcastNotification(String message, @Nullable String ic * * @param message the body of the notification * @param icon name for the notification - * @param severity category for the notification + * @param tag for the notification * @param title for the notification * @param referenceId an identifier used to collapse and hide notifications * @param onClickAction the action to perform when clicked @@ -160,13 +160,13 @@ public static void sendBroadcastNotification(String message, @Nullable String ic * */ @ActionDoc(text = "Sends a broadcast notification to all mobile devices of all account users") - public static void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String severity, + public static void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String tag, @Nullable String title, @Nullable String referenceId, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) { logger.debug("sending broadcast notification '{}' to all users", message); if (cloudService != null) { - cloudService.sendBroadcastNotification(message, icon, severity, title, referenceId, onClickAction, + cloudService.sendBroadcastNotification(message, icon, tag, title, referenceId, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3); } } From fa4390d80603e795a561956b8f482dd45b8017b5 Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Wed, 3 Jul 2024 15:26:20 -0700 Subject: [PATCH 6/8] PR review changes Signed-off-by: Dan Cunningham --- bundles/org.openhab.io.openhabcloud/README.md | 30 +++++++++---------- .../io/openhabcloud/internal/CloudClient.java | 2 +- .../openhabcloud/internal/CloudService.java | 8 ++--- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/bundles/org.openhab.io.openhabcloud/README.md b/bundles/org.openhab.io.openhabcloud/README.md index 019612ff35670..c2225f99bb689 100644 --- a/bundles/org.openhab.io.openhabcloud/README.md +++ b/bundles/org.openhab.io.openhabcloud/README.md @@ -116,11 +116,9 @@ The parameters for these actions have the following meaning: The `sendNotification` and `sendBroadcastNotification` actions additionally support setting a title, reference id, media attachments and actions. -- The tag is used for tagging messages for grouping when displaying in the app and to hide/remove groups of messages from a user's device. -Tag was previously referred to as "severity". +- The tag is used for tagging messages for grouping when displaying in the app and to hide/remove groups of messages from a user's device. Tag was previously referred to as "severity". - The title is displayed as the notification title on the device and defaults to "openHAB" for the Android and iOS apps. -- The reference id is a user supplied identifier that when set will replace messages with the same id on the user's device (so only the last version exists). -The reference id is a user supplied id that can be used to update or remove notifications with this same id. +- The reference id is a user supplied identifier that when set will replace messages with the same id on the user's device (so only the last version exists). It can be used to update or remove notifications. - Media attachments are displayed together with the notification on the device and can be used to display images, e.g. a camera snapshot. - Actions allow the user to interact with the notification, e.g. to open a specific page in the app or to send a command to an Item. @@ -131,14 +129,14 @@ There are four different actions available: To specify media attachments and actions, there is another variant of the `sendNotification` and `sendBroadcastNotification` actions: -- `sendNotification(emailAddress, message, icon, tag, title, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)` -- `sendBroadcastNotification(message, icon, tag, title, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)` +- `sendNotification(emailAddress, message, icon, tag, title, referenceId, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)` +- `sendBroadcastNotification(message, icon, tag, title, referenceId, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)` The additional parameter for these variants have the following meaning: - `tag` : A user supplied tag to group messages for removing using the `hideNotificationByTag` action or grouping messages when displayed in the app. This renames the `severity` parameter, both are functionally identical. - `title`: The title of the notification. Defaults to "openHAB" inside the Android and iOS apps. -- `referenceId`: A user supplied id to both replace existing messages when pushed, and later remove messages with the `hideNotificationReferenceId` actions. +- `referenceId`: A user supplied id to both replace existing messages when pushed, and later remove messages with the `hideNotificationByReferenceId` actions. - `onClickAction`: The action to be performed when the user clicks on the notification. Specified using the [action syntax](#action-syntax). - `mediaAttachmentUrl`: The URL of the media attachment to be displayed with the notification. This can either be a fully qualified URL, prefixed with `http://` or `https://` and reachable by the client device, or an image item with the format `item:MyImageItem` - `actionButton1`: The action to be performed when the user clicks on the first action button. Specified as `Title=$action`, where `$action` follows the [action syntax](#action-syntax). @@ -147,15 +145,6 @@ The additional parameter for these variants have the following meaning: These parameters may be skipped by setting them to `null`. -### Hide Notification Actions - -There are also actions to hide existing notifications, either by `referenceId` or `tag` (formerly severity) - -- `hideNotificationByReferenceId(emailAddress, referenceId)` -- `hideBroadcastNotificationByReferenceId(referenceId)` -- `hideNotificationByTag(emailAddress, tag)` -- `hideBroadcastNotificationByTag(tag)` - #### Action Syntax The action syntax is a string containing the action type and the action payload separated by a colon. @@ -178,6 +167,15 @@ Examples: - `ui:popup:oh-clock-card`: Opens a popup with `oh-clock-card`. - `https://openhab.org`: Opens an embedded browser to the site `https://openhab.org` +### Hide Notification Actions + +There are also actions to hide existing notifications, either by `referenceId` or `tag` (formerly severity): + +- `hideNotificationByReferenceId(emailAddress, referenceId)` +- `hideBroadcastNotificationByReferenceId(referenceId)` +- `hideNotificationByTag(emailAddress, tag)` +- `hideBroadcastNotificationByTag(tag)` + ### Examples Notify the openHAB Cloud user with email address _me@email.com_ that the front door was opened: diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java index 7627d445b3032..5ddef54660b85 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudClient.java @@ -706,7 +706,7 @@ public void hideNotificationByReferenceId(String userId, String referenceId) { } /** - * This method hides a notification by its reference id to for all users + * This method hides a notification by its reference id for all users * * @param referenceId the reference id */ diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java index ce9e084eacde9..c2e0a202b4bbb 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java @@ -170,7 +170,7 @@ public void sendBroadcastNotification(String message, @Nullable String icon, @Nu } /** - * This method hides a notification by its tag through the openHAB Cloud service + * This method hides a notification by its tag through the openHAB Cloud service for a specific user * * @param userId the {@link String} containing the openHAB Cloud user id to hide messages for * @param tag the {@link String} containing severity group of notification @@ -182,7 +182,7 @@ public void hideNotificationByTag(String userId, String tag) { } /** - * This method hides a notification by its tag through the openHAB Cloud service to all + * This method hides a notification by its tag through the openHAB Cloud service for all * mobile devices of all users of the account * * @param severity the {@link String} containing severity (good, info, warning, error) of notification @@ -194,7 +194,7 @@ public void hideBroadcastNotificationByTag(String tag) { } /** - * This method hides a notification by its reference id through the openHAB Cloud service + * This method hides a notification by its reference id through the openHAB Cloud service for a specific user * * @param userId the {@link String} containing the openHAB Cloud user id to hide messages for * @param severity the {@link String} containing severity group of notification @@ -206,7 +206,7 @@ public void hideNotificationByReferenceId(String userId, String referenceId) { } /** - * This method hides a notification by its reference id through the openHAB Cloud service to all + * This method hides a notification by its reference id through the openHAB Cloud service for all * mobile devices of all users of the account * * @param severity the {@link String} containing severity (good, info, warning, error) of notification From fa61cfc35f3b032046fa574d4dcfd8368d452c18 Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Wed, 3 Jul 2024 15:43:56 -0700 Subject: [PATCH 7/8] PR refactoring Signed-off-by: Dan Cunningham --- .../actions/BaseHideNotificationActionHandler.java | 2 +- .../actions/BaseNotificationActionHandler.java | 13 +++++-------- .../HideNotificationByReferenceIdActionHandler.java | 11 ++++------- .../actions/HideNotificationByTagActionHandler.java | 9 +++------ .../actions/SendNotificationActionHandler.java | 9 +++------ 5 files changed, 16 insertions(+), 28 deletions(-) diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseHideNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseHideNotificationActionHandler.java index 83f4c161cfef9..c19c4f0c3f7f3 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseHideNotificationActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseHideNotificationActionHandler.java @@ -42,7 +42,7 @@ public BaseHideNotificationActionHandler(Action module, CloudService cloudServic this.referenceId = stringConfig(PARAM_REFERENCE_ID); } - private @Nullable String stringConfig(String key) { + protected @Nullable String stringConfig(String key) { return ConfigParser.valueAs(module.getConfiguration().get(key), String.class); } } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java index e43ac927c9f32..932a4aed87ae5 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java @@ -12,6 +12,8 @@ */ package org.openhab.io.openhabcloud.internal.actions; +import java.util.Optional; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.automation.Action; @@ -59,13 +61,8 @@ public BaseNotificationActionHandler(Action module, CloudService cloudService) { super(module); this.cloudService = cloudService; - Object messageParam = module.getConfiguration().get(PARAM_MESSAGE); - if (messageParam instanceof String) { - this.message = messageParam.toString(); - } else { - throw new IllegalArgumentException(String.format("Param '%s' should be of type String.", PARAM_MESSAGE)); - } - + this.message = Optional.ofNullable(stringConfig(PARAM_MESSAGE)).orElseThrow(() -> new IllegalArgumentException( + String.format("Param '%s' should be of type String.", PARAM_MESSAGE))); this.icon = stringConfig(PARAM_ICON); this.severity = stringConfig(PARAM_SEVERITY); this.tag = stringConfig(PARAM_TAG); @@ -78,7 +75,7 @@ public BaseNotificationActionHandler(Action module, CloudService cloudService) { this.actionButton3 = stringConfig(PARAM_ACTION_BUTTON_3); } - private @Nullable String stringConfig(String key) { + protected @Nullable String stringConfig(String key) { return ConfigParser.valueAs(module.getConfiguration().get(key), String.class); } } diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByReferenceIdActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByReferenceIdActionHandler.java index 270edb52a8f63..d6ae9696cea55 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByReferenceIdActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByReferenceIdActionHandler.java @@ -13,6 +13,7 @@ package org.openhab.io.openhabcloud.internal.actions; import java.util.Map; +import java.util.Optional; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -23,7 +24,7 @@ /** * This is a {@link ModuleHandler} implementation for {@link Action}s to hide a notification to a specific cloud user. * - * @author Dan Cunningham - - Initial contribution + * @author Dan Cunningham - Initial contribution */ @NonNullByDefault public class HideNotificationByReferenceIdActionHandler extends BaseHideNotificationActionHandler { @@ -37,12 +38,8 @@ public class HideNotificationByReferenceIdActionHandler extends BaseHideNotifica public HideNotificationByReferenceIdActionHandler(Action module, CloudService cloudService) { super(module, cloudService); - Object userIdParam = module.getConfiguration().get(PARAM_USER); - if (userIdParam instanceof String) { - this.userId = userIdParam.toString(); - } else { - throw new IllegalArgumentException(String.format("Param '%s' should be of type String.", PARAM_USER)); - } + this.userId = Optional.ofNullable(stringConfig(PARAM_USER)).orElseThrow( + () -> new IllegalArgumentException(String.format("Param '%s' should be of type String.", PARAM_USER))); } @Override diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByTagActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByTagActionHandler.java index 1580fded0c632..5905480811011 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByTagActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/HideNotificationByTagActionHandler.java @@ -13,6 +13,7 @@ package org.openhab.io.openhabcloud.internal.actions; import java.util.Map; +import java.util.Optional; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -37,12 +38,8 @@ public class HideNotificationByTagActionHandler extends BaseHideNotificationActi public HideNotificationByTagActionHandler(Action module, CloudService cloudService) { super(module, cloudService); - Object userIdParam = module.getConfiguration().get(PARAM_USER); - if (userIdParam instanceof String) { - this.userId = userIdParam.toString(); - } else { - throw new IllegalArgumentException(String.format("Param '%s' should be of type String.", PARAM_USER)); - } + this.userId = Optional.ofNullable(stringConfig(PARAM_USER)).orElseThrow( + () -> new IllegalArgumentException(String.format("Param '%s' should be of type String.", PARAM_USER))); } @Override diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java index 34ebc2b891a22..1048330a27910 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java @@ -13,6 +13,7 @@ package org.openhab.io.openhabcloud.internal.actions; import java.util.Map; +import java.util.Optional; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -40,12 +41,8 @@ public class SendNotificationActionHandler extends BaseNotificationActionHandler public SendNotificationActionHandler(Action module, CloudService cloudService) { super(module, cloudService); - Object userIdParam = module.getConfiguration().get(PARAM_USER); - if (userIdParam instanceof String) { - this.userId = userIdParam.toString(); - } else { - throw new IllegalArgumentException(String.format("Param '%s' should be of type String.", PARAM_USER)); - } + this.userId = Optional.ofNullable(stringConfig(PARAM_USER)).orElseThrow( + () -> new IllegalArgumentException(String.format("Param '%s' should be of type String.", PARAM_USER))); } @Override From adf6017c3d2178c227cf6de48a76ce3ea4fbd8c8 Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Wed, 3 Jul 2024 16:12:24 -0700 Subject: [PATCH 8/8] PR review Signed-off-by: Dan Cunningham --- bundles/org.openhab.io.openhabcloud/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/org.openhab.io.openhabcloud/README.md b/bundles/org.openhab.io.openhabcloud/README.md index c2225f99bb689..fdb3c64b61f0c 100644 --- a/bundles/org.openhab.io.openhabcloud/README.md +++ b/bundles/org.openhab.io.openhabcloud/README.md @@ -118,7 +118,7 @@ The `sendNotification` and `sendBroadcastNotification` actions additionally supp - The tag is used for tagging messages for grouping when displaying in the app and to hide/remove groups of messages from a user's device. Tag was previously referred to as "severity". - The title is displayed as the notification title on the device and defaults to "openHAB" for the Android and iOS apps. -- The reference id is a user supplied identifier that when set will replace messages with the same id on the user's device (so only the last version exists). It can be used to update or remove notifications. +- The reference id is a user supplied identifier that when set will replace messages with the same id on the user's device (so only the last version exists). It can be used to update or remove notifications. - Media attachments are displayed together with the notification on the device and can be used to display images, e.g. a camera snapshot. - Actions allow the user to interact with the notification, e.g. to open a specific page in the app or to send a command to an Item.