From ef92a3e60abe73b7a45031d4abdc3cfbf5700832 Mon Sep 17 00:00:00 2001 From: jbosecker Date: Wed, 16 Apr 2014 10:26:36 +0000 Subject: [PATCH] Added possibility to use default values in pushover module. Thanks to Benni for contribution git-svn-id: svn://svn.code.sf.net/p/fhem/code/trunk@5535 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/70_Pushover.pm | 233 ++++++++++++++++++++++++++------------- 1 file changed, 156 insertions(+), 77 deletions(-) diff --git a/fhem/FHEM/70_Pushover.pm b/fhem/FHEM/70_Pushover.pm index 968534473..1972a885c 100644 --- a/fhem/FHEM/70_Pushover.pm +++ b/fhem/FHEM/70_Pushover.pm @@ -1,10 +1,12 @@ -############################################## +############################################################################### # # A module to send notifications to Pushover. # -# written 2013 by Johannes B +# written 2013 by Johannes B +# modified 24.02.2014 by Benjamin Battran +# -> Added title, device, priority and sound attributes (see documentation below) # -############################################## +############################################################################### # # Definition: # define Pushover @@ -14,13 +16,19 @@ # # # You can send messages via the following command: -# set msg <msg> <device> <priority> <sound> [<retry> <expire>] +# set <Pushover_device> msg ['title'] '<msg>' ['<device>' <priority> '<sound>' [<retry> <expire>]] # # Examples: -# set Pushover1 msg 'Titel' 'This is a text.' '' 0 '' +# set Pushover1 msg 'This is a text.' +# set Pushover1 msg 'Title' 'This is a text.' +# set Pushover1 msg 'Title' 'This is a text.' '' 0 '' # set Pushover1 msg 'Emergency' 'Security issue in living room.' '' 2 'siren' 30 3600 # # Explantation: +# +# For the first and the second example the corresponding device attributes for the +# missing arguments must be set with valid values (see attributes section) +# # If device is empty, the message will be sent to all devices. # If sound is empty, the default setting in the app will be used. # If priority is higher or equal 2, retry and expire must be defined. @@ -39,15 +47,21 @@ my %sets = ( "msg" => 1 ); +#------------------------------------------------------------------------------ sub Pushover_Initialize($$) +#------------------------------------------------------------------------------ { my ($hash) = @_; $hash->{DefFn} = "Pushover_Define"; $hash->{SetFn} = "Pushover_Set"; - $hash->{AttrList} = "timestamp:0,1"; + $hash->{AttrList} = "timestamp:0,1 title sound device priority:0,1,-1"; + #a priority value of 2 is not predifined as for this also a value for retry and expire must be set + #which will most likely not be used with default values. } +#------------------------------------------------------------------------------ sub Pushover_Define($$) +#------------------------------------------------------------------------------ { my ($hash, $def) = @_; @@ -74,7 +88,9 @@ sub Pushover_Define($$) } } +#------------------------------------------------------------------------------ sub Pushover_Set($@) +#------------------------------------------------------------------------------ { my ($hash, $name, $cmd, @args) = @_; @@ -89,84 +105,94 @@ sub Pushover_Set($@) } } +#------------------------------------------------------------------------------ sub Pushover_Set_Message +#------------------------------------------------------------------------------ { my $hash = shift; my $attr = join(" ", @_); - - my $shortExpressionMatched = 0; - my $longExpressionMatched = 0; - - if($attr =~ /(".*"|'.*')\s*(".*"|'.*')\s*(".*"|'.*')\s*(-?\d+)\s*(".*"|'.*')\s*(\d+)\s*(\d+)\s*$/s) + + #Set defaults + my $title=AttrVal($hash->{NAME}, "title", ""); + my $message=""; + my $device=AttrVal($hash->{NAME}, "device", ""); + my $priority=AttrVal($hash->{NAME}, "priority", 0); + my $sound=AttrVal($hash->{NAME}, "sound", ""); + my $retry=""; + my $expire=""; + + + #Split parameters + my $argc=0; + if($attr =~ /(".*"|'.*')\s*(".*"|'.*')\s*(".*"|'.*')\s*(-?\d+)\s*(".*"|'.*')\s*(\d+)\s*(\d+)\s*$/s) { - $longExpressionMatched = 1; + $argc=7; + } elsif ($attr =~ /(".*"|'.*')\s*(".*"|'.*')\s*(".*"|'.*')\s*(-?\d+)\s*(".*"|'.*')\s*$/s) + { + $argc=5; + } elsif ($attr =~ /(".*"|'.*')\s*(".*"|'.*')\s*$/s) + { + $argc=2; + } elsif ($attr =~ /(".*"|'.*')\s*$/s) + { + $argc=1 } - elsif($attr =~ /(".*"|'.*')\s*(".*"|'.*')\s*(".*"|'.*')\s*(-?\d+)\s*(".*"|'.*')\s*$/s) - { - $shortExpressionMatched = 1; - } - - my $title = ""; - my $message = ""; - my $device = ""; - my $priority = ""; - my $sound = ""; - my $retry = ""; - my $expire = ""; - - if(($shortExpressionMatched == 1) || ($longExpressionMatched == 1)) - { - $title = $1; - $message = $2; - $device = $3; - $priority = $4; - $sound = $5; - - if($longExpressionMatched == 1) - { - $retry = $6; - $expire = $7; - } - - if($title =~ /^['"](.*)['"]$/s) - { - $title = $1; - } - - if($message =~ /^['"](.*)['"]$/s) - { - $message = $1; - } - - if($device =~ /^['"](.*)['"]$/s) - { - $device = $1; - } - - if($priority =~ /^['"](.*)['"]$/) - { - $priority = $1; - } - - if($sound =~ /^['"](.*)['"]$/s) - { - $sound = $1; - } - - if($retry =~ /^['"](.*)['"]$/s) - { - $retry = $1; - } - - if($expire =~ /^['"](.*)['"]$/s) - { - $expire = $1; + + if($argc > 1) { + $title=$1; + $message=$2; + + if($argc >2) { + $device=$3; + $priority=$4; + $sound=$5; + + if($argc > 5) { + $retry=$6; + $expire=$7; + } } } - + elsif ($argc==1) { + $message=$1; + } + + #Remove quotation marks + if($title =~ /^['"](.*)['"]$/s) + { + $title = $1; + } + if($message =~ /^['"](.*)['"]$/s) + { + $message = $1; + } + if($device =~ /^['"](.*)['"]$/s) + { + $device = $1; + } + if($priority =~ /^['"](.*)['"]$/s) + { + $priority = $1; + } + if($sound =~ /^['"](.*)['"]$/s) + { + $sound = $1; + } + if($retry =~ /^['"](.*)['"]$/s) + { + $retry = $1; + } + if($expire =~ /^['"](.*)['"]$/s) + { + $expire = $1; + } + + #Check if all mandatory arguments are filled + #"title" and "message" can not be empty and if "priority" is set to "2" "retry" and "expire" must also be set if((($title ne "") && ($message ne "")) && ((($retry ne "") && ($expire ne "")) || ($priority < 2))) { + #Build the "body" for the URL-Call of Pushover-Service (see Pushover-API-Documentation) my $body = "token=" . $hash->{Token} . "&" . "user=" . $hash->{User} . "&" . "title=" . $title . "&" . @@ -206,6 +232,7 @@ sub Pushover_Set_Message my $result = Pushover_HTTP_Call($hash, $body); + #Save result and data of the last call to the readings. readingsBeginUpdate($hash); readingsBulkUpdate($hash, "last-message", $title . ": " . $message); readingsBulkUpdate($hash, "last-result", $result); @@ -215,11 +242,21 @@ sub Pushover_Set_Message } else { - return "Syntax: set <Pushover_device> msg <title> <msg> <device> <priority> <sound> [<retry> <expire>]"; + #There was a problem with the arguments, so tell the user the correct usage of the 'set msg' command + if ((1 == $argc) && ($title eq "")) + { + return "Please define the default title in the pushover device arguments."; + } + else + { + return "Syntax: <Pushover_device> msg [title] <msg> [<device> <priority> <sound> [<retry> <expire>]]"; + } } } +#------------------------------------------------------------------------------ sub Pushover_HTTP_Call($$) +#------------------------------------------------------------------------------ { my ($hash,$body) = @_; @@ -250,6 +287,8 @@ sub Pushover_HTTP_Call($$) 1; +############################################################################### + =pod =begin html @@ -281,17 +320,21 @@ sub Pushover_HTTP_Call($$) <a name="PushoverSet"></a> <b>Set</b> <ul> - <code>set <name> msg <title> <msg> <device> <priority> <sound> [<retry> <expire>]</code> + <code>set <Pushover_device> msg [title] <msg> [<device> <priority> <sound> [<retry> <expire>]]</code> <br> <br> Examples: <ul> - <code>set Pushover1 msg 'Titel' 'This is a text.' '' 0 ''</code><br> + <code>set Pushover1 msg 'This is a text.'</code><br> + <code>set Pushover1 msg 'Title' 'This is a text.'</code><br> + <code>set Pushover1 msg 'Title' 'This is a text.' '' 0 ''</code><br> <code>set Pushover1 msg 'Emergency' 'Security issue in living room.' '' 2 'siren' 30 3600</code><br> </ul> <br> Notes: <ul> + <li>For the first and the second example the corresponding default attributes for the missing arguments must be defined for the device (see attributes section) + </li> <li>If device is empty, the message will be sent to all devices. </li> <li>If sound is empty, the default setting in the app will be used. @@ -311,6 +354,22 @@ sub Pushover_HTTP_Call($$) <li>timestamp<br> Send the unix timestamp with each message. </li><br> + <a name="title"></a> + <li>title<br> + Will be used as title if title is not specified as an argument. + </li><br> + <a name="device"></a> + <li>device<br> + Will be used for the device name if device is not specified as an argument. If left blank, the message will be sent to all devices. + </li><br> + <a name="priority"></a> + <li>priority<br> + Will be used as priority value if priority is not specified as an argument. Valid values are -1 = silent / 0 = normal priority / 1 = high priority + </li><br> + <a name="sound"></a> + <li>sound<br> + Will be used as the default sound if sound argument is missing. If left blank the adjusted sound of the app will be used. + </li><br> </ul> <br> <a name="PushoverEvents"></a> @@ -351,17 +410,21 @@ sub Pushover_HTTP_Call($$) <a name="PushoverSet"></a> <b>Set</b> <ul> - <code>set <name> msg <title> <msg> <device> <priority> <sound> [<retry> <expire>]</code> + <code>set <Pushover_device> msg [title] <msg> [<device> <priority> <sound> [<retry> <expire>]]</code> <br> <br> Beispiele: <ul> + <code>set Pushover1 msg 'Dies ist ein Text.'</code><br> + <code>set Pushover1 msg 'Titel' 'Dies ist ein Text.'</code><br> <code>set Pushover1 msg 'Titel' 'Dies ist ein Text.' '' 0 ''</code><br> <code>set Pushover1 msg 'Notfall' 'Sicherheitsproblem im Wohnzimmer.' '' 2 'siren' 30 3600</code><br> </ul> <br> Anmerkungen: <ul> + <li>Bei der Verwendung der ersten beiden Beispiele müssen die entsprechenden Attribute als Ersatz für die fehlenden Parameter belegt sein (s. Attribute) + </li> <li>Wenn device leer ist, wird die Nachricht an alle Geräte geschickt. </li> <li>Wenn sound leer ist, dann wird die Standardeinstellung in der App verwendet. @@ -381,6 +444,22 @@ sub Pushover_HTTP_Call($$) <li>timestamp<br> Sende den Unix-Zeitstempel mit jeder Nachricht. </li><br> + <a name="title"></a> + <li>title<br> + Wird beim Senden als Titel verwendet, sofern dieser nicht als Aufrufargument angegeben wurde. + </li><br> + <a name="device"></a> + <li>device<br> + Wird beim Senden als Gerätename verwendet, sofern dieser nicht als Aufrufargument angegeben wurde. Kann auch generell entfallen, bzw. leer sein, dann wird an alle Geräte gesendet. + </li><br> + <a name="priority"></a> + <li>priority<br> + Wird beim Senden als Priorität verwendet, sofern diese nicht als Aufrufargument angegeben wurde. Zulässige Werte sind -1 = leise / 0 = normale Priorität / 1 = hohe Priorität + </li><br> + <a name="sound"></a> + <li>sound<br> + Wird beim Senden als Titel verwendet, sofern dieser nicht als Aufrufargument angegeben wurde. Kann auch generell entfallen, dann wird der eingestellte Ton der App verwendet. + </li><br> </ul> <br> <a name="PushoverEvents"></a>