############################################################################### # # A module to send notifications to Pushover. # # 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 # # Example: # define Pushover1 Pushover 12345 6789 # # # You can send messages via the following command: # set msg ['title'] '' ['' '' [ ]] # # Examples: # 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. # # # For further documentation of these parameters: # https://pushover.net/api package main; use HttpUtils; use utf8; my %sets = ( "msg" => 1 ); #------------------------------------------------------------------------------ sub Pushover_Initialize($$) #------------------------------------------------------------------------------ { my ($hash) = @_; $hash->{DefFn} = "Pushover_Define"; $hash->{SetFn} = "Pushover_Set"; $hash->{AttrList} = "disable:0,1 timestamp:0,1 title sound device priority:0,1,-1 ssl:0,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) = @_; my @args = split("[ \t]+", $def); if (int(@args) < 2) { return "Invalid number of arguments: define Pushover "; } my ($name, $type, $token, $user) = @args; $hash->{STATE} = 'Initialized'; if(defined($token) && defined($user)) { $hash->{Token} = $token; $hash->{User} = $user; return undef; } else { return "Token and/or user missing."; } } #------------------------------------------------------------------------------ sub Pushover_Set($@) #------------------------------------------------------------------------------ { my ($hash, $name, $cmd, @args) = @_; if (!defined($sets{$cmd})) { return "Unknown argument " . $cmd . ", choose one of " . join(" ", sort keys %sets); } if (AttrVal($name, "disable", 0 ) == 1) { return "Device is disabled"; } if ($cmd eq 'msg') { return Pushover_Set_Message($hash, @args); } } #------------------------------------------------------------------------------ sub Pushover_HTTP_Call #------------------------------------------------------------------------------ { my ($hash,$body,$ssl) = @_; my $url = "http"; if (1 == $ssl) { $url = $url . "s"; } $url = $url . "://api.pushover.net/1/messages.json"; $response = GetFileFromURL($url, 10, $body, 0, 5); if ($response =~ m/"status":(.*),/) { if ($1 eq "1") { return undef; } elsif ($response =~ m/"errors":\[(.*)\]/) { return "Error: " . $1; } else { return "Error"; } } else { return "Error: No known response\nResponse was: " . $response; } } #------------------------------------------------------------------------------ sub Pushover_Set_Message #------------------------------------------------------------------------------ { my $hash = shift; my $attr = join(" ", @_); #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=""; my $ssl=AttrVal($hash->{NAME}, "ssl", 1); #Split parameters my $argc=0; if($attr =~ /(".*"|'.*')\s*(".*"|'.*')\s*(".*"|'.*')\s*(-?\d+)\s*(".*"|'.*')\s*(\d+)\s*(\d+)\s*$/s) { $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 } 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 . "&" . "message=" . $message; if ($device ne "") { $body = $body . "&" . "device=" . $device; } if ($priority ne "") { $body = $body . "&" . "priority=" . $priority; } if ($sound ne "") { $body = $body . "&" . "sound=" . $sound; } if ($retry ne "") { $body = $body . "&" . "retry=" . $retry; } if ($expire ne "") { $body = $body . "&" . "expire=" . $expire; } my $timestamp = AttrVal($hash->{NAME}, "timestamp", 0); if (1 == $timestamp) { $body = $body . "&" . "timestamp=" . int(time()); } my $result = Pushover_HTTP_Call($hash, $body, $ssl); #Save result and data of the last call to the readings. readingsBeginUpdate($hash); readingsBulkUpdate($hash, "last-message", $title . ": " . $message); readingsBulkUpdate($hash, "last-result", $result); readingsEndUpdate($hash, 1); return $result; } else { #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: msg [title] [ [ ]]"; } } } 1; ############################################################################### =pod =begin html

Pushover

    Pushover is a service to receive instant push notifications on your phone or tablet from a variety of sources.
    You need an account to use this module.
    For further information about the service see pushover.net.

    Discuss the module here.


    Define
      define <name> Pushover <token> <user>

      You have to create an account to get the user key.
      And you have to create an application to get the API token.

      Example:
        define Pushover1 Pushover 01234 56789

    Set
      set <Pushover_device> msg [title] <msg> [<device> <priority> <sound> [<retry> <expire>]]

      Examples:
        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

      Notes:
      • For the first and the second example the corresponding default attributes for the missing arguments must be defined for the device (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.
      • For further documentation of these parameters have a look at the Pushover API.

    Get
      N/A

    Attributes
    • timestamp
      Send the unix timestamp with each message.

    • title
      Will be used as title if title is not specified as an argument.

    • device
      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.

    • priority
      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

    • sound
      Will be used as the default sound if sound argument is missing. If left blank the adjusted sound of the app will be used.

    • ssl
      Send the requests over HTTP or HTTPS. Valid values are 0 = HTTP / 1 = HTTPS. Default is 1.


    Generated events:
      N/A
=end html =begin html_DE

Pushover

    Pushover ist ein Dienst, um Benachrichtigungen von einer vielzahl von Quellen auf Deinem Smartphone oder Tablet zu empfangen.
    Du brauchst einen Account um dieses Modul zu verwenden.
    Für weitere Informationen über den Dienst besuche pushover.net.

    Diskutiere das Modul hier.


    Define
      define <name> Pushover <token> <user>

      Du musst einen Account erstellen, um den User Key zu bekommen.
      Und du musst eine Anwendung erstellen, um einen API Token zu bekommen.

      Beispiel:
        define Pushover1 Pushover 01234 56789

    Set
      set <Pushover_device> msg [title] <msg> [<device> <priority> <sound> [<retry> <expire>]]

      Beispiele:
        set Pushover1 msg 'Dies ist ein Text.'
        set Pushover1 msg 'Titel' 'Dies ist ein Text.'
        set Pushover1 msg 'Titel' 'Dies ist ein Text.' '' 0 ''
        set Pushover1 msg 'Notfall' 'Sicherheitsproblem im Wohnzimmer.' '' 2 'siren' 30 3600

      Anmerkungen:
      • Bei der Verwendung der ersten beiden Beispiele müssen die entsprechenden Attribute als Ersatz für die fehlenden Parameter belegt sein (s. Attribute)
      • Wenn device leer ist, wird die Nachricht an alle Geräte geschickt.
      • Wenn sound leer ist, dann wird die Standardeinstellung in der App verwendet.
      • Wenn die Priorität höher oder gleich 2 ist müssen retry und expire definiert sein.
      • Für weiterführende Dokumentation über diese Parameter lies Dir die Pushover API durch.

    Get
      N/A

    Attributes
    • timestamp
      Sende den Unix-Zeitstempel mit jeder Nachricht.

    • title
      Wird beim Senden als Titel verwendet, sofern dieser nicht als Aufrufargument angegeben wurde.

    • device
      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.

    • priority
      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

    • sound
      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.

    • ssl
      Sende die Requests über HTTP oder HTTPS. Zulässige Werte sind 0 = HTTP / 1 = HTTPS. Standard ist 1.


    Generated events:
      N/A
=end html_DE =cut