Ich würde gerne versuchen, meine HS100 nicht direkt, sondern über die Cloud steuern. Es gibt da viele Ansätze, meine PHP-Kenntnisse sind aber sehr eingeschränkt und Beispiele transferieren ist nicht ganz einfach.
Ich hab da aber jetzt einen Ansatz gefunden, der vielversprechend aussieht, leider ist der Code für Windows.
Die UUID wird hier generiert Online UUID Generator Tool
Token ermitteln
curl -X POST -H "Content-Type: application/json"
-d "{ \"method\" : \"login\", \"params\" : {\"appType\" : \"Kasa_Android\",
\"cloudPassword\" : \"OUR PASSWORD HERE\", \"cloudUserName\" : \"OUR USER NAME HERE\",
\"terminalUUID\" : \"YOUR JUST ACQUIRED UUID HERE\" }}" https://eu-wap.tplinkcloud.com
Device ID ermitteln
curl -X POST -H "Content-Type: application/json" -d "{ \"method\" : \"getDeviceList\" }"
https://eu-wap.tplinkcloud.com?token=YOUR JUST ACQUIRED TOKEN HERE
Ein- und Ausschalten
curl -X POST -H "Content-Type: application/json" -d
"{ \"method\" : \"passthrough\", \"params\" : { \"deviceId\" : \"OUR DEVICE ID HERE\",
\"requestData\" : '{ \"system\" : { \"set_relay_state\" : { \"state\" : 1}}}'}}"
https://eu-wap.tplinkcloud.com/?token=OUR TOKEN HERE
curl -X POST -H "Content-Type: application/json"
-d "{ \"method\" : \"passthrough\", \"params\" : { \"deviceId\" : \"OUR DEVICE ID HERE\",
\"requestData\" : '{ \"system\" : { \"set_relay_state\" : { \"state\" : 0}}}'}}"
https://eu-wap.tplinkcloud.com/?token=OUR TOKEN HERE
Den aktuellen Status bekommt man sicher auch noch eirgendwie raus, da such ich noch.
Meine finale Version
Token ermitteln
<?php
$username="";
$password="";
//Token ermitteln
$url = 'https://eu-wap.tplinkcloud.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$content = array (
'method' => 'login',
'params' => array (
'appType' => 'Kasa_Android',
'cloudUserName' => $username,
'cloudPassword' => $password,
'terminalUUID' => ''
)
);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-type: application/json"));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content));
$result = curl_exec($ch);
$token=json_decode($result,true)["result"]["token"];
setvalue(28026/*IPS Variable die den Wert von Token aufnimmt*/,$token);
Instanzen pro Schalter erstellen
<?php
//Token ermitteln
include(IPS_GetScriptFile(18209/*Skript für die Ermittlung des Tokens*/));
$url = 'https://eu-wap.tplinkcloud.com?token='.GetValue(28026/*Token*/);
$devicelist="";
$InsID=0;
$VarID=0;
$root=getvalue(37924/*ID der Kategorie wo die Instanzen erstellt werden sollen*/);
$GUIDDummy=getvalue(39342/*UID der Dummy Instanz*/);
$ActionScriptID=getvalue(50777); /*ID des Skripts füs Schalten */
if ($root>0 and $GUIDDummy != "") /*nur zur Sicherheit, sollten die Werte mal verloren gehen */
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$content = array (
'method' => 'getDeviceList'
);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-type: application/json"));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content));
$result = curl_exec($ch);
$devicelist=json_decode($result,true)["result"]["deviceList"];
foreach ($devicelist as $device)
{
$instanceID=@IPS_GetInstanceIDByName($device["alias"],$root); /*Prüfen ob die Instanz schon vorhanden ist*/
if ($instanceID == false)
{
//Schalterinstanz
$InsID = IPS_CreateInstance($GUIDDummy);
IPS_SetName($InsID, $device["alias"]);
IPS_SetParent($InsID, $root);
//Schaltervariable
$VarID = IPS_CreateVariable(0);
IPS_SetName($VarID,$device["alias"]);
IPS_SetParent($VarID,$InsID);
IPS_SetVariableCustomProfile($VarID,"~Switch");
IPS_SetVariableCustomAction($VarID,$ActionScriptID);
//DeviceID Variable
$VarID = IPS_CreateVariable(3);
IPS_SetName($VarID,$device["alias"]."ID");
IPS_SetParent($VarID,$InsID);
setvalue($VarID,$device["deviceId"]);
}
}
}
/* Instanz anlegen eindeutig mit deviceMac
varialbe boolean für ein aus
variable string mit deviceId string*/
Schalten
<?php
$DeviceID="";
$newStatus = 0;
$SchalterID=0;
SetValue($_IPS['VARIABLE'], $_IPS['VALUE']);
$SchalterID=$_IPS['VARIABLE'];
include(IPS_GetScriptFile(18209/*Skript für die Ermittlung des Tokens*/));
$Variablen=IPS_GetChildrenIDs(IPS_GetParent($SchalterID));
foreach ($Variablen as $Variable)
{
if(IPS_GetVariable($Variable)["VariableType"]==3) $DeviceID=getvalue($Variable);
}
//Ein- und Ausschalten
$url = 'https://eu-wap.tplinkcloud.com?token='.$token;
if (getvalue($SchalterID)==true){
$newStatus=1;
}
else
{
$newStatus=0;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$content = array ( 'method' => 'passthrough',
"params" => array( "deviceId" => $DeviceID,
"requestData" => "{\"system\":{\"set_relay_state\":{\"state\": $newStatus }}}" ) );
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-type: application/json"));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content));
$result = curl_exec($ch);
if (json_decode($result,true)["error_code"] == 0){
//Gerätestatus abfragen
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$content = array ( 'method' => 'passthrough',
"params" => array( "deviceId" => $DeviceID,
"requestData" => json_encode(array("system" => array("get_sysinfo" => null,),))));
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-type: application/json"));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content));
$result = curl_exec($ch);
$result=json_decode(json_decode($result,true)["result"]["responseData"],true);
setvalue($SchalterID,$result["system"]["get_sysinfo"]["relay_state"]==1);
}
else{
setvalue($SchalterID,false);
}
Statusaktualisierung
<?php
$Kategorie= getvalue(37924);
$Instanzen=IPS_GetChildrenIDs($Kategorie);
$IDSchalter=0;
$IDDeviceID=0;
include(IPS_GetScriptFile(18209/*Skript für die Ermittlung des Tokens*/)); /*Kategorie wo die Intanzen für die Schalter liegen*/
/*Es werden die Instanzen, also die Schalter, und dann die Variablen der jeweiligen Instanz durchlaufen*/
foreach ($Instanzen as $Instanz)
{
$Variablen = IPS_GetChildrenIDs($Instanz);
foreach ($Variablen as $Variable)
{
if(IPS_GetVariable($Variable)["VariableType"]==0)
{
$IDSchalter=$Variable;
}
else
{
$IDDeviceID=$Variable;
}
}
//Gerätestatus abfragen
$url = 'https://eu-wap.tplinkcloud.com?token='.$token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$content = array ( 'method' => 'passthrough',
"params" => array( "deviceId" => getvalue($IDDeviceID),
"requestData" => json_encode(array("system" => array("get_sysinfo" => null,),))));
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-type: application/json"));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content));
$result = curl_exec($ch);
if (json_decode($result,true)["error_code"] == 0){
$result=json_decode(json_decode($result,true)["result"]["responseData"],true);
setvalue($IDSchalter,$result["system"]["get_sysinfo"]["relay_state"]==1);
}
else
{
setvalue($IDSchalter,false);
}
}
Anbei noch ein paar Screenshots von den Skripten und Instanzen im Objektbaum