Skip to content

Instantly share code, notes, and snippets.

@michabbb
Created January 15, 2017 16:37
Show Gist options
  • Save michabbb/dff00e6906fec90aa58284d3b1f27d1c to your computer and use it in GitHub Desktop.
Save michabbb/dff00e6906fec90aa58284d3b1f27d1c to your computer and use it in GitHub Desktop.
Control AVM DECT Switches with php
<?php
// User defined (only part where something needs to be changed)
$fritzbox_User = 'user';
$fritzbox_Password = 'pass';
//
// /* AVM Fritz!Box Dect 200 actuator set script user303 on http://www.harmony-remote-forum.de/
// Parameters used fpr request in URL http://nas:800/fritz.php?actname=Media&action=on actname = <name of the AVM DECT actor as defined in the Fritz!Box GUI> action = on | off | toggle | poll (poll checks the current state of the actuator 0=off 1=on)
// Ressources https://avm.de/fileadmin/user_upload/Global/Service/Schnittstellen/AHA-HTTP-Interface.pdf Commands are sent via HTTP GET Request to the following URL
// http://fritz.box/webservices/homeautoswitch.lua?ain=<ain>&switchcmd=<cmd>&sid=<sid> (!)
// https was not used on purpose, as the Fritz!Box was blocking access Used commands: getdevicelistinfos setswitchon setswitchoff setswitchtoggle getswitchstate
// Stream infos getdevicelistinfos: text/xml; charset=utf­8 all others: text/plain; charset=utf­8 */
//
// //Parse Arguments
$actuatorname = htmlspecialchars($_GET["actname"]);
$actuatoraction = htmlspecialchars(
$_GET["action"]
);
// Connect to FritzBox for SID
$ch = curl_init('http://fritz.box/login_sid.lua?username=' . $fritzbox_User);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$login = curl_exec($ch);
$session_status_simplexml = simplexml_load_string($login);
if ($session_status_simplexml->SID != '0000000000000000') {
echo "Already logged on with SID " . $SID . "\n";
$SID = $session_status_simplexml->SID;
}
else // Calculate Session ID with Challenge, Password and MD5-Encoding
{
$challenge = $session_status_simplexml->Challenge;
$response = $challenge . '-' . md5(mb_convert_encoding($challenge . '-' . $fritzbox_Password, "UCS-2LE", "UTF-8"));
curl_setopt($ch, CURLOPT_POSTFIELDS, "response={$response}&page=/login_sid.lua");
$sendlogin = curl_exec($ch);
$session_status_simplexml = simplexml_load_string($sendlogin);
if ($session_status_simplexml->SID != '0000000000000000') {
$SID = $session_status_simplexml->SID;
}
else {
echo "Error: Cannot connect to Fritz!Box";
http_response_code(403);
return;
}
} // Get getdevicelistinfos
$opts = array(
'http' => array(
'method' => 'GET',
'header' => 'Content­Type: text/xml; charset=utf­8'
)
);
$context = stream_context_create($opts);
$switchcmd = 'getdevicelistinfos';
$url = 'http://fritz.box/webservices/homeautoswitch.lua?switchcmd=' . $switchcmd . '&sid=' . $SID . ''; //retrieving the device list infos in XML structure
$result = simplexml_load_string(
file_get_contents($url, false, $context)
); //looking for the acutator to retrieve the AIN which is needed for the switching
$count = 0;
foreach ($result->device as $node) {
$tmpname = $node->name;
if (strcmp($tmpname, $actuatorname) == 0) {
echo $tmpname . "<br>";
$ain = str_replace(' ', '', $node->attributes()->identifier);
echo "retrieved AIN: " . $ain . "<br>";
$count++;
}
}
//Verifying if more that one actuators have been identified with the same name. Danger of inadvertantly switching the wrong actuator. Use unique names.
if ($count > 1) {
echo "More than one actuators found with the name " . $actuatorname . "<br>";
http_response_code(400);
return;
}
if ($count == 0) {
echo "No actuator was found with the name " . $actuatorname . "<br>";
http_response_code(400);
exit ("No actuator was found with the name " . $actuatorname);
}
$opts = array(
'http' => array(
'method' => 'GET',
'header' => 'Content­Type: text/plain; charset=utf­8'
)
);
$context = stream_context_create(
$opts
);
echo $actuatoraction . "<br>";
switch ($actuatoraction) {
case "on":
$switchcmd = 'setswitchon';
$url = 'http://fritz.box/webservices/homeautoswitch.lua?ain=' . $ain . '&switchcmd=' . $switchcmd . '&sid=' . $SID . '';
echo $url . "<br>";
$result = file_get_contents($url, false, $context);
echo $result . "<br>";
http_response_code(200);
break;
case "off":
$switchcmd = 'setswitchoff';
$url = 'http://fritz.box/webservices/homeautoswitch.lua?ain=' . $ain . '&switchcmd=' . $switchcmd . '&sid=' . $SID . '';
$result = file_get_contents($url, false, $context);
echo $result . "<br>";
http_response_code(200);
break;
case "toggle":
$switchcmd = 'setswitchtoggle';
$url = 'http://fritz.box/webservices/homeautoswitch.lua?ain=' . $ain . '&switchcmd=' . $switchcmd . '&sid=' . $SID . '';
$result = file_get_contents($url, false, $context);
echo $result . "<br>";
http_response_code(200);
break;
case "poll":
$switchcmd = 'getswitchstate';
$url = 'http://fritz.box/webservices/homeautoswitch.lua?ain=' . $ain . '&switchcmd=' . $switchcmd . '&sid=' . $SID . '';
$result = file_get_contents($url, false, $context);
echo $result;
http_response_code(200);
break;
default:
http_response_code(400);
return;
} // Close Session curl_close($ch);
@michabbb
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment