Auto Actions: Difference between revisions
Jump to navigation
Jump to search
| Line 3,249: | Line 3,249: | ||
email_result:[email protected] | email_result:[email protected] | ||
remarks:Text, z.B. mit Template Variablen [GUESTFULLNAME] und [BOOKID] | remarks:Text, z.B. mit Template Variablen [GUESTFULLNAME] und [BOOKID] | ||
</div> | |||
</div> | |||
====V2 version of the code for Shelly ==== | |||
</div> | |||
<div class="mw-collapsible-content"> | |||
Here is the second version v2 of the PHP script to switch on a Shelly device via an autoaction. | |||
Now you can also store the device IDs in the script instead of adding them to the body parameters. This makes it possible to switch a Shelly device with a single autoaction. The device ID is then determined via the template variable [INTERNALROOMNAME]. | |||
Code: Select all | |||
]<?php | |||
/* | |||
* - Anschalten der Lichterkette aus beds24 mit einem PHP Skript über die Shelly Cloud - | |||
* Version 2 vom 10.12.2024 - mehrere Devices im Array im Skript möglich | |||
* | |||
* https://domainname.de/scripte/beds24-shelly-steuern-v2.php | |||
* | |||
* curl -X POST https://shelly-123-eu.shelly.cloud/device/relay/control -d "channel=0&turn=on&id=b0b21c123456&auth_key=abcdefghijklmnopqrstuvwxyz1234567890" | |||
* Einstellungen in beds24 | |||
* - in einer Autoaction unter 'Webhook' das Script eintragen: https://domainname.de/pfad_zum_script/beds24-shelly-steuern-v2.php | |||
* - alternativ wenn Geräte-Id überINTERNALROOMNAME im Skript selbst: https://domainname.de/pfad_zum_script/beds24-shelly-steuern-v2?roomname=[INTERNALROOMNAME] | |||
* - unter 'Daten im Body' die Parameter zur Steuerung eintragen: | |||
* - id:<Shelly Geräte-Id> ===> alternativ mehrere Geräte-Id im Skript selbst | |||
* - shelly_server:<Shelly Cloud Server> | |||
* - auth_key:<Shelly Cloud-Autorisierungs-Schlüssel> | |||
* - parameter:<Shelly Befehle> | |||
* - email_result:<*optional* e-mail für Empfänger des Schaltergebnisses> | |||
* - remarks:<*optional* beliebige Anmerkungen für E-Mail> | |||
* | |||
* Beispiel: | |||
* id:b0b21c123456 | |||
* shelly_server:shelly-123-eu.shelly.cloud | |||
* auth_key:abcdefghijklmnopqrstuvwxyz1234567890 | |||
* shelly_url:device/relay/control | |||
* parameter:channel=0&turn=on | |||
* email_result:[email protected] | |||
* remarks:Text, auch mit Template Variablen [GUESTFULLNAME] und [BOOKID]/[INTERNALROOMNAME] möglich | |||
*/ | |||
/* ----------------------------------------------------------- */ | |||
/* Parameter & Einstellungen */ | |||
/* ----------------------------------------------------------- */ | |||
/* Shelly Geräte-ID entweder beim Aufruf in beds24 über die 'Daten im Body' übergeben oder hier im Skript eintragen. Wenn im Script, dann beim Aufruf die Templatevariable [INTERNALROOMNAME] mitgeben, über die erfolgt dann die Auswahl hier im Array */ | |||
$p_geraete_ids = array ( | |||
"chalet" => "e465b8123456", | |||
"fewo" => "b0b21c123456", | |||
"linx" => "e465b8123456" | |||
); | |||
$p_test = false; /* im Testmodus kommen die Parameter direkt aus dem Script statt über beds24 */ | |||
/* Body Daten zum direkten Testen d.h. Direktaufruf des Scripts ohne Autoaction */ | |||
$test_body = array( /* zum direkten Aufruf des Scripts die Parameter hier testweise eintragen */ | |||
/* https://shelly-api-docs.shelly.cloud/cloud-control-api/communication */ | |||
"id" => "xxx", /* rausnehmen, wenn ids über Array kommen sollen */ | |||
"shelly_server" => "shelly-123-eu.shelly.cloud", | |||
"auth_key" => "abcdefghijklmnopqrstuvwxyz1234567890", | |||
"shelly_url" => "device/relay/control", | |||
"parameter" => "channel=0&turn=on", | |||
"email_result" => "[email protected]", | |||
"remarks" => "Text, z.B. mit Template Variablen [GUESTFULLNAME] und [BOOKID]" | |||
); | |||
/* ----------------------------------------------------------- */ | |||
/* ab hier normalerweise keine Änderungen mehr vornehmen | |||
/* ----------------------------------------------------------- */ | |||
$hf_internalroomname = filter_var($_GET["roomname"], FILTER_SANITIZE_STRING); /* wenn über ?roomname=[INTERNALROOMNAME] übergeben | |||
/* Body aus Post message lesen und in Variablen $body[keyname] einlesen */ | |||
if ($p_test == false ) { | |||
$hf_body = explode( "\n", file_get_contents('php://input') ); | |||
$body = array(); | |||
foreach ($hf_body as $v) { | |||
list($body_key,$body_value) = explode( ':', $v ); | |||
$body[$body_key] = $body_value; | |||
} | |||
} | |||
else { | |||
$body = $test_body; /* im Testmodus Parameter direkt aus dem Script nehmen */ | |||
} | |||
ob_start(); /* Bildschirmausgabe buffern */ | |||
/* wenn übergeben, dann Geräte-ID aus den body-parametern übernehmen, ansonsten über [INTERNALROOMNAME] ermitteln */ | |||
if ( $body["id"]) { /* in Body Parameter gesetzt */ | |||
$hf_geraete_id = $body["id"]; | |||
} else { | |||
$hf_geraete_id = $p_geraete_ids[$hf_internalroomname]; /* Geräte-Id aus Skript Array nehmen */ | |||
} | |||
/* Prüfen ob wichtige Argumente übergeben */ | |||
if ( isset($body["shelly_server"]) == false ) { echo("Argument shelly_server fehlt, z.B.: shelly_server:servername<br>"); $body["shelly_server"] = "fehlt";} | |||
if ( isset($body["shelly_url"]) == false ) { echo("Argument shelly_url fehlt, z.B. : shelly_url:device/relay/control<br>"); $body["shelly_url"] = "fehlt";} | |||
if ( isset($body["parameter"]) == false ) { echo("Argument parameter fehlt, z.B.: parameter:channel=0&turn=on><br>"); $body["parameter"] = "fehlt";} | |||
if ( isset($body["auth_key"]) == false ) { echo("Argument auth_key fehlt, z.B.: auth_key:abcdefghijklmnopqrstuvwxyz1234567890<br>"); $body["auth_key"] = "fehlt";} | |||
$url = 'https://'.$body["shelly_server"].'/'.trim($body["shelly_url"], " /"); | |||
$ch=curl_init(); | |||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); /* Zertifikatswarnung übergehen */ | |||
curl_setopt($ch, CURLOPT_SSL_VERIFYSTATUS, false); | |||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |||
curl_setopt($ch, CURLOPT_PROXY_SSL_VERIFYPEER, false); | |||
curl_setopt($ch, CURLOPT_POST, true); | |||
curl_setopt($ch, CURLOPT_POSTFIELDS, | |||
$body["parameter"]."&id=".$hf_geraete_id."&auth_key=".$body["auth_key"]); | |||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); | |||
curl_setopt($ch, CURLOPT_URL, $url); | |||
$result = curl_exec($ch); | |||
curl_close ($ch); | |||
if ( $body["remarks"] ) { /* Anmerkungen fürs Protokoll/E-Mail ausgeben */ | |||
echo ("<br>".$body["remarks"]."<br>"); | |||
}; | |||
$output = ob_get_contents(); /* Bildschirmausgabe aus Buffer lesen */ | |||
ob_end_clean(); | |||
echo $output; | |||
if ( $body["email_result"] ) { | |||
mail($body["email_result"], 'Ergebnis Shelly Skript - Device: '.$hf_geraete_id, $output, 'From: beds24 Shelly Script <'.$body["email_result"].'>'); | |||
echo ("<br>Mail an: ".$body["email_result"]); | |||
} | |||
?> | |||
</div> | </div> | ||