diff --git a/fhem/FHEM/70_Pushover.pm b/fhem/FHEM/70_Pushover.pm new file mode 100644 index 000000000..61c46337d --- /dev/null +++ b/fhem/FHEM/70_Pushover.pm @@ -0,0 +1,406 @@ +############################################## +# +# A module to send notifications to Pushover. +# +# written 2013 by Johannes B +# +############################################## +# +# Definition: +# define Pushover +# +# Example: +# define Pushover1 Pushover 12345 6789 +# +# +# You can send messages via the following command: +# set msg <msg> <device> <priority> <sound> [<retry> <expire>] +# +# Examples: +# set Pushover1 msg 'Titel' 'This is a text.' '' 0 '' +# set Pushover1 msg 'Emergency' 'Security issue in living room.' '' 2 'siren' 30 3600 +# +# Explantation: +# 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 HTTP::Request; +use LWP::UserAgent; +use utf8; + +my %sets = ( + "msg" => 1 +); + +sub Pushover_Initialize($$) +{ + my ($hash) = @_; + $hash->{DefFn} = "Pushover_Define"; + $hash->{SetFn} = "Pushover_Set"; + $hash->{AttrList} = "timestamp:0,1"; +} + +sub Pushover_Define($$) +{ + my ($hash, $def) = @_; + + my @args = split("[ \t]+", $def); + + if (int(@args) < 2) + { + return "Invalid number of arguments: define <name> Pushover <token> <user>"; + } + + 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 ($cmd eq 'msg') + { + return Pushover_Set_Message($hash, @args); + } +} + +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) + { + $longExpressionMatched = 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((($title ne "") && ($message ne "")) && ((($retry ne "") && ($expire ne "")) || ($priority < 2))) + { + 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=" . time(); + } + + return Pushover_HTTP_Call($hash, $body); + } + else + { + return "Syntax: set <Pushover_device> msg <title> <msg> <device> <priority> <sound> [<retry> <expire>]"; + } +} + +sub Pushover_HTTP_Call($$) +{ + my ($hash,$body) = @_; + + my $client = LWP::UserAgent->new(); + + my $req = HTTP::Request->new(POST => "https://api.pushover.net/1/messages.json"); + $req->header('Content-Type' => 'application/x-www-form-urlencoded'); + $req->content($body); + + my $response = $client->request($req); + + if($response) + { + if ($response->is_error) + { + return "Error: " . $response->status_line; + } + else + { + return "OK"; + } + } + else + { + return "Status: " . $response->status_line; + } +} + +1; + +=pod +=begin html + +<a name="Pushover"></a> +<h3>Pushover</h3> +<ul> + Pushover is a service to receive instant push notifications on your + phone or tablet from a variety of sources.<br> + You need an account to use this module.<br> + For further information about the service see <a href="https://pushover.net">pushover.net</a>.<br> + <br> + You have to install these modules:<br> + <ul> + <li>IO::Socket::SSL</li> + <li>Mozilla::CA</li> + <li>LWP::Protocol::https</li> + </ul> + <br> + Use 'cpan -i *module-name*' to install them.<br> + For instructions on QNAP see <a href="http://forum.fhem.de/index.php/topic,17297.msg119096.html#msg119096">this thread</a>.<br> + Discuss the module <a href="http://forum.fhem.de/index.php/topic,16215.0.html">here</a>.<br> + <br> + <br> + <a name="PushoverDefine"></a> + <b>Define</b> + <ul> + <code>define <name> Pushover <token> <user></code><br> + <br> + You have to create an account to get the user key.<br> + And you have to create an application to get the API token.<br> + <br> + Example: + <ul> + <code>define Pushover1 Pushover 01234 56789</code> + </ul> + </ul> + <br> + <a name="PushoverSet"></a> + <b>Set</b> + <ul> + <code>set <name> 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 'Emergency' 'Security issue in living room.' '' 2 'siren' 30 3600</code><br> + </ul> + <br> + Notes: + <ul> + <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. + </li> + <li>If priority is higher or equal 2, retry and expire must be defined. + </li> + <li>For further documentation of these parameters have a look at the <a href="https://pushover.net/api">Pushover API</a>. + </li> + </ul> + </ul> + <br> + <b>Get</b> <ul>N/A</ul><br> + <a name="PushoverAttr"></a> + <b>Attributes</b> + <ul> + <a name="timestamp"></a> + <li>timestamp<br> + Send the unix timestamp with each message. + </li><br> + </ul> + <br> + <a name="PushoverEvents"></a> + <b>Generated events:</b> + <ul> + N/A + </ul> +</ul> + +=end html +=begin html_DE + +<a name="Pushover"></a> +<h3>Pushover</h3> +<ul> + Pushover ist ein Dienst, um Benachrichtigungen von einer vielzahl + von Quellen auf Deinem Smartphone oder Tablet zu empfangen.<br> + Du brauchst einen Account um dieses Modul zu verwenden.<br> + Für weitere Informationen über den Dienst besuche <a href="https://pushover.net">pushover.net</a>.<br> + <br> + Du musst diese Module installieren:<br> + <ul> + <li>IO::Socket::SSL</li> + <li>Mozilla::CA</li> + <li>LWP::Protocol::https</li> + </ul> + <br> + Verwende 'cpan -i *module-name*' um sie zu installieren.<br> + Für Installationsschritte auf einem QNAP-Gerät besuche <a href="http://forum.fhem.de/index.php/topic,17297.msg119096.html#msg119096">diesen Thread</a>.<br> + Diskutiere das Modul <a href="http://forum.fhem.de/index.php/topic,16215.0.html">hier</a>.<br> + <br> + <br> + <a name="PushoverDefine"></a> + <b>Define</b> + <ul> + <code>define <name> Pushover <token> <user></code><br> + <br> + Du musst einen Account erstellen, um den User Key zu bekommen.<br> + Und du musst eine Anwendung erstellen, um einen API Token zu bekommen.<br> + <br> + Beispiel: + <ul> + <code>define Pushover1 Pushover 01234 56789</code> + </ul> + </ul> + <br> + <a name="PushoverSet"></a> + <b>Set</b> + <ul> + <code>set <name> msg <title> <msg> <device> <priority> <sound> [<retry> <expire>]</code> + <br> + <br> + Beispiele: + <ul> + <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>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. + </li> + <li>Wenn die Priorität höher oder gleich 2 ist müssen retry und expire definiert sein. + </li> + <li>Für weiterführende Dokumentation über diese Parameter lies Dir die <a href="https://pushover.net/api">Pushover API</a> durch. + </li> + </ul> + </ul> + <br> + <b>Get</b> <ul>N/A</ul><br> + <a name="PushoverAttr"></a> + <b>Attributes</b> + <ul> + <a name="timestamp"></a> + <li>timestamp<br> + Sende den Unix-Zeitstempel mit jeder Nachricht. + </li><br> + </ul> + <br> + <a name="PushoverEvents"></a> + <b>Generated events:</b> + <ul> + N/A + </ul> +</ul> + +=end html_DE +=cut diff --git a/fhem/contrib/70_Pushover.pm b/fhem/contrib/70_Pushover.pm deleted file mode 100644 index 3c94d1e7d..000000000 --- a/fhem/contrib/70_Pushover.pm +++ /dev/null @@ -1,237 +0,0 @@ -############################################## -# -# A module to send notifications to Pushover. -# -# written 2013 by Johannes B <johannes_b at icloud.com> -# -############################################## -# -# Definition: -# define <name> Pushover <token> <user> -# -# Example: -# define Pushover1 Pushover 12345 6789 -# -# -# You can send messages via the following command: -# set <Pushover_device> msg <title> <msg> <device> <priority> <sound> [<retry> <expire>] -# -# Examples: -# set Pushover1 msg 'Titel' 'This is a text.' '' 0 '' -# set Pushover1 msg 'Emergency' 'Security issue in living room.' '' 2 'siren' 30 3600 -# -# Explantation: -# 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 HTTP::Request; -use LWP::UserAgent; -use IO::Socket::SSL; -use utf8; - -sub Pushover_Initialize($$) -{ - my ($hash) = @_; - $hash->{DefFn} = "Pushover_Define"; - $hash->{SetFn} = "Pushover_Set"; - $hash->{AttrList} = "timestamp:0,1"; -} - -sub Pushover_Define($$) -{ - my ($hash, $def) = @_; - - my @args = split("[ \t]+", $def); - - if (int(@args) < 2) - { - return "Invalid number of arguments: define <name> Pushover <token> <user>"; - } - - 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($cmd eq 'msg') - { - return Pushover_Set_Message($hash, @args); - } -} - -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) - { - $longExpressionMatched = 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((($title ne "") && ($message ne "")) && ((($retry ne "") && ($expire ne "")) || ($priority < 2))) - { - 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=" . time(); - } - - return Pushover_HTTP_Call($hash, $body); - } - else - { - return "Syntax: set <Pushover_device> msg <title> <msg> <device> <priority> <sound> [<retry> <expire>]"; - } -} - -sub Pushover_HTTP_Call($$) -{ - my ($hash,$body) = @_; - - my $client = LWP::UserAgent->new(); - - my $req = HTTP::Request->new(POST => "https://api.pushover.net/1/messages.json"); - $req->header('Content-Type' => 'application/x-www-form-urlencoded'); - $req->content($body); - - my $response = $client->request($req); - - if($response) - { - if ($response->is_error) - { - return "Error: " . $response->status_line; - } - else - { - return "OK"; - } - } - else - { - return "Status: " . $response->status_line; - } -} - -1;