diff --git a/README.md b/README.md index c474be4..5722983 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ $test_url = "https://example.com/plweb/test.php?"; Die Variable `hawebhook` enthält die URL des Home Assistant-Webhooks, so wie dieser von PlusLifeWeb aus (serverseitig) aufgerufen wird. +Wird die Variable leer gelassen (`""`), so wird die Home Assistant-Benachrichtigung deaktiviert. Die Datenbankkonfiguration ist in `dbconnstring` enthalten. Hier ggf. das Paßwort anpassen. @@ -93,3 +94,55 @@ Die `test_url` zeigt zum Script test.php der PlusLifeWeb-Installation. Diese URL wird, zusammen mit der ID des fertigen Tests, als Benachrichtigung an Home Assistant übertragen. +### Home Assistant + +Um die Home Assistant (HA)-Benachrichtigung zu nutzen, muß in HA eine Automatisierung +eingerichtet werden, die per Webhook-Aufruf aktiviert wird. + +Diese kann beispielsweise so aussehen: + +```yaml +alias: Webhook PlusLifeWeb +triggers: + - trigger: webhook + allowed_methods: + - POST + local_only: true + webhook_id: plweb +actions: + - if: + - condition: template + value_template: "{% if trigger.json.url is defined %}true{% endif %}" + then: + - action: notify.familie + metadata: {} + data: + message: |- + {{ trigger.json.message }} + Klicken, um den Test anzusehen. + data: + clickAction: "{{ trigger.json.url }}" + group: plweb + title: PlusLifeWeb + else: + - action: notify.familie + metadata: {} + data: + message: "{{ trigger.json.message }}" + title: PlusLifeWeb + data: + group: plweb +mode: single +``` + +**Wichtig:** Der Wert für `webhook_id` ist quasi das Paßwort für den Webhook - es gibt keine weitere +Authentifizierung für den Aufruf des Webhooks. Falls `local_only` auf `false` gesetzt ist, kann der +Webhook auch aus dem Internet aufgerufen werden (sofern HA von dort aus erreichbar ist). +Die Empfehlung wäre, den Webhook nur im lokalen LAN erreichbar zu machen (`local_only: true`) und eine +sichere `webhook_id` zu benutzen. +Dies setzt allerdings voraus, daß PlusLifeWeb und der HA-Server im gleichen lokalen LAN stehen. + +Die Benachrichtigung erfolgt im Beispiel an `notify.familie`, eine Benachrichtigungs-Gruppe. +Hier muß eine im eigenen HA vorhandene notify-Entität genutzt werden :) + + diff --git a/config.php b/config.php index 1c41073..96bebae 100644 --- a/config.php +++ b/config.php @@ -1,7 +1,16 @@ diff --git a/webhook.php b/webhook.php index d0b41dc..6948b74 100644 --- a/webhook.php +++ b/webhook.php @@ -42,16 +42,18 @@ switch ($data['event']) { function send_notification($msg, $url = null) { global $hawebhook; - $post = [ 'message' => $msg ]; - if ($url != null) $post['url'] = $url; - - $ch = curl_init($hawebhook); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post)); - $rc = curl_exec($ch); - curl_close($ch); + if ($hawebhook != "") { + $post = [ 'message' => $msg ]; + if ($url != null) $post['url'] = $url; + + $ch = curl_init($hawebhook); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post)); + $rc = curl_exec($ch); + curl_close($ch); + } } function test_finished() {