]> git.basschouten.com Git - openhab-addons.git/commitdiff
[pushover] Minor improvements, SAT findings (#10840)
authorChristoph Weitkamp <github@christophweitkamp.de>
Thu, 10 Jun 2021 07:21:51 +0000 (09:21 +0200)
committerGitHub <noreply@github.com>
Thu, 10 Jun 2021 07:21:51 +0000 (09:21 +0200)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
bundles/org.openhab.binding.pushover/README.md
bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/actions/PushoverActions.java
bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/config/PushoverAccountConfiguration.java
bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/config/PushoverConfigOptionProvider.java
bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/connection/PushoverAPIConnection.java
bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/connection/PushoverMessageBuilder.java
bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/handler/PushoverAccountHandler.java
bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/config/config.xml
bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/i18n/pushover_de.properties

index 314c846c386a49302208f57da80bb073132cb4ee..7ae9fd4543654f59a350c217a25ed161214c1dcd 100644 (file)
@@ -12,16 +12,16 @@ You are able to create multiple instances of this Thing to broadcast to differen
 
 ## Thing Configuration
 
-| Configuration Parameter | Type    | Description                                                                                                                                          |
-|-------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------|
-| `apikey`                | text    | Your API token / key (APP_TOKEN) to access the Pushover Message API. **mandatory**                                                                   |
-| `user`                  | text    | Your user key or group key (USER_KEY) to which you want to push notifications. **mandatory**                                                         |
-| `title`                 | text    | The default title of a message (default: `openHAB`).                                                                                                 |
-| `format`                | text    | The default format (`none`, `html` or `monospace`) of a message (default: `none`).                                                                   |
-| `sound`                 | text    | The default notification sound on target device (default: `default`) (see [supported notification sounds](https://pushover.net/api#sounds)).         |
-| `retry`                 | integer | The retry parameter specifies how often (in seconds) the Pushover servers will send the same notification to the user (default: `300`). **advanced** |
-| `expire`                | integer | The expire parameter specifies how long (in seconds) your notification will continue to be retried (default: `3600`). **advanced**                   |
-| `timeout`               | integer | The timeout parameter specifies maximum number of seconds a request to Pushover can take. **advanced**                                               |
+| Configuration Parameter | Type    | Description                                                                                                                                                                                                                                                                                                   |
+|-------------------------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| `apikey`                | text    | Your API token / key (APP_TOKEN) to access the Pushover Message API. **mandatory**                                                                                                                                                                                                                            |
+| `user`                  | text    | Your user key or group key (USER_KEY) to which you want to push notifications. **mandatory**                                                                                                                                                                                                                  |
+| `title`                 | text    | The default title of a message (default: `openHAB`).                                                                                                                                                                                                                                                          |
+| `format`                | text    | The default format (`none`, `html` or `monospace`) of a message (default: `none`).                                                                                                                                                                                                                            |
+| `sound`                 | text    | The notification sound on target device (default: `default`) (see [supported notification sounds](https://pushover.net/api#sounds)). This list will be populated dynamically during runtime with 21 different sounds plus user-defined [custom sounds](https://blog.pushover.net/posts/2021/3/custom-sounds). |
+| `retry`                 | integer | The retry parameter specifies how often (in seconds) the Pushover servers will send the same notification to the user (default: `300`). **advanced**                                                                                                                                                          |
+| `expire`                | integer | The expire parameter specifies how long (in seconds) your notification will continue to be retried (default: `3600`). **advanced**                                                                                                                                                                            |
+| `timeout`               | integer | The timeout parameter specifies maximum number of seconds a request to Pushover can take. **advanced**                                                                                                                                                                                                        |
 
 The `retry` and `expire` parameters are only used for emergency-priority notifications.
 
index ebef8cdc058d5c3572d76c4c5669c76f67512538..b09431d459782066554490710f001b66eb4bb79e 100644 (file)
@@ -59,7 +59,8 @@ public class PushoverActions implements ThingActions {
                 message, title, sound, url, urlTitle, attachment, contentType, priority, device);
 
         PushoverMessageBuilder builder = getDefaultPushoverMessageBuilder(message);
-        if (sound != null) {
+        // add sound, if defined
+        if (sound != null && !DEFAULT_SOUND.equals(sound)) {
             builder.withSound(sound);
         }
         if (url != null) {
index 4a39c0022f9ed9f1314b217b55a96013f62e1ae8..1b7a7211e9adb94ba2c39cd85f57199e954b3f26 100644 (file)
@@ -14,8 +14,11 @@ package org.openhab.binding.pushover.internal.config;
 
 import static org.openhab.binding.pushover.internal.PushoverBindingConstants.*;
 
+import java.util.List;
+
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
+import org.openhab.binding.pushover.internal.dto.Sound;
 
 /**
  * The {@link PushoverAccountConfiguration} class contains fields mapping thing configuration parameters.
@@ -24,6 +27,17 @@ import org.eclipse.jdt.annotation.Nullable;
  */
 @NonNullByDefault
 public class PushoverAccountConfiguration {
+    public static final List<Sound> DEFAULT_SOUNDS = List.of(new Sound("alien", "Alien Alarm (long)"),
+            new Sound("bike", "Bike"), new Sound("bugle", "Bugle"), new Sound("cashregister", "Cash Register"),
+            new Sound("classical", "Classical"), new Sound("climb", "Climb (long)"), new Sound("cosmic", "Cosmic"),
+            new Sound("falling", "Falling"), new Sound("gamelan", "Gamelan"), new Sound("incoming", "Incoming"),
+            new Sound("intermission", "Intermission"), new Sound("magic", "Magic"),
+            new Sound("mechanical", "Mechanical"), new Sound("none", "None (silent)"),
+            new Sound("persistent", "Persistent (long)"), new Sound("pianobar", "Piano Bar"),
+            new Sound("pushover", "Pushover (default)"), new Sound("echo", "Pushover Echo (long)"),
+            new Sound("siren", "Siren"), new Sound("spacealarm", "Space Alarm"), new Sound("tugboat", "Tug Boat"),
+            new Sound("updown", "Up Down (long)"), new Sound("vibrate", "Vibrate Only"));
+
     public @Nullable String apikey;
     public @Nullable String user;
     public String title = DEFAULT_TITLE;
index b9b0a77469b7846fd35c0c57e58bd16e978ddf32..c3bce422be517458189d8ed6ba74315173577a35 100644 (file)
@@ -45,9 +45,10 @@ public class PushoverConfigOptionProvider implements ConfigOptionProvider, Thing
     @Override
     public @Nullable Collection<ParameterOption> getParameterOptions(URI uri, String param, @Nullable String context,
             @Nullable Locale locale) {
-        if (accountHandler != null && PUSHOVER_ACCOUNT.getAsString().equals(uri.getSchemeSpecificPart())
+        PushoverAccountHandler localAccountHandler = accountHandler;
+        if (localAccountHandler != null && PUSHOVER_ACCOUNT.getAsString().equals(uri.getSchemeSpecificPart())
                 && CONFIG_SOUND.equals(param)) {
-            List<Sound> sounds = accountHandler.getSounds();
+            List<Sound> sounds = localAccountHandler.getSounds();
             if (!sounds.isEmpty()) {
                 return sounds.stream().map(Sound::getAsParameterOption)
                         .sorted(Comparator.comparing(ParameterOption::getLabel))
index 8f84241d44ba4658a655e895c12a484db8dd087e..f7c5f2cbcbc419da53a359d1a07637a7bb830cae 100644 (file)
@@ -14,7 +14,6 @@ package org.openhab.binding.pushover.internal.connection;
 
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -103,9 +102,8 @@ public class PushoverAPIConnection {
         final JsonObject sounds = json == null || !json.has("sounds") ? null : json.get("sounds").getAsJsonObject();
 
         return sounds == null ? List.of()
-                : Collections.unmodifiableList(sounds.entrySet().stream()
-                        .map(entry -> new Sound(entry.getKey(), entry.getValue().getAsString()))
-                        .collect(Collectors.toList()));
+                : sounds.entrySet().stream().map(entry -> new Sound(entry.getKey(), entry.getValue().getAsString()))
+                        .collect(Collectors.toUnmodifiableList());
     }
 
     private String buildURL(String url, Map<String, String> requestParams) {
index 7418a0c48389fee4fb546427d35e97938d208b8a..29424cc40d107d609d4b4981e168d5b57b8d3612 100644 (file)
@@ -163,7 +163,7 @@ public class PushoverMessageBuilder {
         return this;
     }
 
-    public ContentProvider build() {
+    public ContentProvider build() throws PushoverCommunicationException {
         if (message != null) {
             if (message.length() > MAX_MESSAGE_LENGTH) {
                 throw new IllegalArgumentException(String.format(
@@ -248,7 +248,9 @@ public class PushoverMessageBuilder {
                 body.addFilePart(MESSAGE_KEY_ATTACHMENT, file.getName(),
                         new PathContentProvider(contentType, file.toPath()), null);
             } catch (IOException e) {
-                throw new IllegalArgumentException(String.format("Skip sending the message: %s", e.getMessage()));
+                logger.debug("IOException occurred - skip sending message: {}", e.getLocalizedMessage(), e);
+                throw new PushoverCommunicationException(
+                        String.format("Skip sending the message: %s", e.getLocalizedMessage()), e);
             }
         }
 
index 9840bc23ae8c66a1841604d6eb75ead5d28f1c18..b0064aa200f13435efe64af834ae2eb9675076a6 100644 (file)
@@ -50,7 +50,7 @@ public class PushoverAccountHandler extends BaseThingHandler {
 
     private final HttpClient httpClient;
 
-    private @NonNullByDefault({}) PushoverAccountConfiguration config;
+    private PushoverAccountConfiguration config = new PushoverAccountConfiguration();
     private @Nullable PushoverAPIConnection connection;
 
     public PushoverAccountHandler(Thing thing, HttpClient httpClient) {
@@ -100,7 +100,14 @@ public class PushoverAccountHandler extends BaseThingHandler {
      * @return a list of {@link Sound}s
      */
     public List<Sound> getSounds() {
-        return connection != null ? connection.getSounds() : List.of();
+        try {
+            return connection != null ? connection.getSounds() : PushoverAccountConfiguration.DEFAULT_SOUNDS;
+        } catch (PushoverCommunicationException e) {
+            // do nothing, causing exception is already logged
+        } catch (PushoverConfigurationException e) {
+            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
+        }
+        return PushoverAccountConfiguration.DEFAULT_SOUNDS;
     }
 
     /**
@@ -125,7 +132,7 @@ public class PushoverAccountHandler extends BaseThingHandler {
             default:
                 break;
         }
-        // add sound if defined
+        // add sound, if defined
         if (!DEFAULT_SOUND.equals(config.sound)) {
             builder.withSound(config.sound);
         }
index 09a61f1622773897e42f151f7ff0a97566d65f1d..9f4be6e413423cb629ffac776783de094fde2357 100644 (file)
@@ -32,7 +32,7 @@
                </parameter>
                <parameter name="sound" type="text">
                        <label>Notification Sound</label>
-                       <description>The default notification sound on target device.</description>
+                       <description>The notification sound on target device.</description>
                        <default>default</default>
                </parameter>
                <parameter name="retry" type="integer" min="30" unit="s">
index 9f094e31e8f77315a140eeea86778af0c3f5d05a..2ef1579529c770aa22fdb7176e872da223ee96fe 100644 (file)
@@ -15,11 +15,13 @@ thing-type.config.pushover.pushover-account.title.description = Standardtitel de
 thing-type.config.pushover.pushover-account.format.label = Format
 thing-type.config.pushover.pushover-account.format.description = Standardformat der Nachricht.
 thing-type.config.pushover.pushover-account.sound.label = Benachrichtigungston
-thing-type.config.pushover.pushover-account.sound.description = Standardbenachrichtigungston auf dem Endgerät.
+thing-type.config.pushover.pushover-account.sound.description = Benachrichtigungston auf dem Endgerät.
 thing-type.config.pushover.pushover-account.retry.label = Wiederholungen
 thing-type.config.pushover.pushover-account.retry.description = Dieser Parameter gibt an, in welchen Abständen eine Prioritätsnachricht wiederholt an den Benutzer gesendet werden soll.
 thing-type.config.pushover.pushover-account.expire.label = Verfall
 thing-type.config.pushover.pushover-account.expire.description = Dieser Parameter gibt an, wie lange eine Prioritätsnachricht wiederholt wird.
+thing-type.config.pushover.pushover-account.timeout.label = Timeout
+thing-type.config.pushover.pushover-account.timeout.description = Dieser Parameter gibt an, wie lange eine Anfrage an die Pushover Message API maximal dauern darf.
 
 # user defined messages
 offline.conf-error-missing-apikey = Der Parameter 'apikey' muss konfiguriert werden.