Tuya devices use AES encryption which is not available in the Python standard library. PyCA/cryptography is recommended and installed by default. Other options include PyCryptodome , PyCrypto and pyaes.
Deprecation notice for pyaes: The pyaes library works for Tuya Protocol <= 3.4 but will not work for 3.5 devices.
This is because pyaes does not support GCM which is required for v3.5 devices.
Misschien heb je een tuya device met een versie 3.5? En is dat de reden
Misschien kun je wat van gebruiken uit mijn script welke ik geschreven heb voor bepalen van data welke niet door TinyTUYA (Cloud) opgehoest wordt.
Je zult vast wel wat aan moeten passen zoals $wpused dit haal ik uit domoticz.
#!/usr/bin/php
<?php
// Save as /home/pi/domoticz/scripts/python/WP_COP.php
//Next add a Crontab rule:
//crontab -e
//Add the following line at the end:
// */1 * * * * /home/pi/domoticz/scripts/python/WP_COP.php >/dev/null 2>&1
//of
// */1 * * * * /home/pi/domoticz/scripts/python/WP_COP.php >/home/pi/domoticz/scripts/python/WP_COP.log 2>&1
echo "run script \n";
$domoticzurl = 'http://127.0.0.1:8080/';
$wpusedIDX = '285'; // WP (Current Use power Watt)
$flowIDX = '279'; // WP Waterflow liter/min
$tempOutIDX = '212'; // WP Aanvoer °C
$tempInIDX = '213'; // WP Retour °C
$setpowerIDX = '281'; // $WP power tbv Grafana
$SetCOPIDX = '230'; // WP current COP
$deltaTIDX = '231'; // $WP_deltaT
$wpused2IDX = '281'; // $WP power tbv Grafana
$DefrostingIDX = '355';
$AfgegevenVermogenIDX = '326'; // $WP AfgegevenVermogen tbv Grafana
$WP_previous_used_powerIDX = '42';
$WPGeneraterdDayIDX = '44';
$WPGeneraterdDay2 = 'WPGeneraterdDay';
$SMwater = 998; //soortelijke massa(pw), deze is voor water 998 kg/m³
$SWwater = 4187; //Soortelijke warmte(cw), deze is voor water 4190 J/kg.
$wpused = Getvalue($wpusedIDX,'Usage');
$wpused= trim($wpused,"Watt");
$wpused= intval( $wpused );
echo "wpused = ".$wpused." \n";
if ($wpused >= 20) {
$output = shell_exec('python3 -m tinytuya ');
echo "$output\n"; // uitzetten na test!!
// waterflow
$output = shell_exec('python3 -m tinytuya ');
$output = shell_exec ('grep "39" /home/pi/snapshot.json');
$output = trim($output);
$flow= trim(substr($output, -3, 2));
echo "waterflow = ".$flow." \n";
// Working mode
$output = shell_exec('grep "\"5\"\:" /home/pi/snapshot.json');
$output = trim($output);
$size=strlen($output);
$WorkingMode= trim(substr($output, 6, ($size-8)));
echo "Working mode = ".$WorkingMode." \n";
// CurveSetting
$output = shell_exec('grep "\"11\"\: \"" /home/pi/snapshot.json');
$output = trim($output);
$size=strlen($output);
$CurveSetting= trim(substr($output, 7, ($size-9)));
echo "CurveSetting= ".$CurveSetting." \n";
// Defrosting
$output = shell_exec('grep "\"33\"\:" /home/pi/snapshot.json');
$output = trim($output);
$size=strlen($output);
$Defrosting = trim(substr($output, 6, ($size-7)));
echo "Defrosting = ".$Defrosting." \n";
if ($Defrosting == "true") {
$Defrosting = "On" ;
}
if ($Defrosting == "false") {
$Defrosting = "Off" ;
}
// CompressorFrequency
$output = shell_exec('grep "\"20\"\:" /home/pi/snapshot.json');
$output = trim($output);
$size=strlen($output);
$CompressorFrequency = trim(substr($output, 6, ($size-7)));
echo "CompressorFrequency = ".$CompressorFrequency." \n";
// FanFrequency
$output = shell_exec('grep "\"40\"\:" /home/pi/snapshot.json');
$output = trim($output);
$size=strlen($output);
$FanFrequency = trim(substr($output, 6, ($size-7)));
echo "FanFrequency = ".$FanFrequency." \n";
$Qv = $flow/60000;
// echo "waterflow = ".$Qv." m3/seconde \n";
$wpused = Getvalue($wpusedIDX,'Usage');
$wpused= trim($wpused,"Watt");
$wpused= intval( $wpused );
// echo "wpused = ".$wpused." \n";
$tempOut = Getvalue($tempOutIDX,'Temp');
$tempOut = substr( $tempOut, 0,4);
// echo "tempOut= ".$tempOut." \n";
// $temperatuurIn WP Retour
$tempIn = Getvalue($tempInIDX,'Temp');
$tempIn = substr( $tempIn, 0,4);
echo "tempIn = ".$tempIn." \n";
// deltaT = WP Aanvoer - WP Retour
$deltaT = $tempOut - $tempIn;
if ($deltaT <= 0 ) { $deltaT = 0; }
//echo "deltaT = ".$tempOut." - ".$tempIn." = ".$deltaT." \n";
// AfgegevenVermogen = Qv x pw x cw x deltaT
$AfgegevenVermogen = $Qv * $SMwater * $SWwater * $deltaT ;
$AfgegevenVermogen = round($AfgegevenVermogen);
/ echo "AfgegevenVermogen = ".$Qv." * ".$SMwater." * ".$SWwater." * ".$deltaT." = " .$AfgegevenVermogen." \n";
// COP
$COP = $AfgegevenVermogen / $wpused;
// echo "COP = ".$AfgegevenVermogen." / ".$wpused." = ".$COP." \n";
$COP = $AfgegevenVermogen / $wpused;
$COP = round($COP,2);
if ($wpused <= 20) {
$AfgegevenVermogen = $wpused ;
$COP = 0;
$AfgegevenVermogen = 17 ;
}
if ($COP >= 7) {
$AfgegevenVermogen = $wpused ;
$COP = 7;
}
echo "COP = ".$COP." \n";
UpdateSensor($flowIDX,$flow);
UpdateSensor($SetCOPIDX,"$COP");
UpdateSensor($deltaTIDX,"$deltaT");
UpdateSensor($DefrostingIDX,$Defrosting);
UpdateSensor($AfgegevenVermogenIDX,$AfgegevenVermogen);
$afgegevenkWh = round(($AfgegevenVermogen) * ( 1 / 60),2);
echo "afgegevenWarmteInWh = $afgegevenkWh\n";
$afgegevenkWhsaved = Getvariable($WPGeneraterdDayIDX,"Value");
echo "afgegevenkhsaved = $afgegevenkWhsaved\n";
$afgegevenkWhTotal = $afgegevenkWhsaved + $afgegevenkWh;
echo "afgegevenkhTotal = $afgegevenkWhTotal \n";
Setvariabel($WPGeneraterdDay2,2,$afgegevenkWhTotal);
}
else {
UpdateSensor($AfgegevenVermogenIDX,17);
UpdateSensor($flowIDX,7);
UpdateSensor($SetCOPIDX,0);
$afgegevenkWh = round((17) * ( 1 / 60),2);
echo "afgegevenWartmteInWh = $afgegevenkWh\n";
$afgegevenkWhsaved = Getvariable($WPGeneraterdDayIDX,"Value");
echo "afgegevenWhsaved = $afgegevenkWhsaved\n";
$afgegevenkWhTotal = $afgegevenkWhsaved + $afgegevenkWh;
echo "afgegevenWhTotal = $afgegevenkWhTotal \n";
Setvariabel($WPGeneraterdDay2,2,$afgegevenkWhTotal);
}
// hieronder niets wijzigen
//###############################################
function Getvalue($idx,$value) {
global $domoticzurl;
$json_string = file_get_contents($domoticzurl.'json.htm?type=command¶m=getdevices&rid='.$idx);
$parsed_json = json_decode($json_string, true);
$test_link = "/home/pi/domoticz/scripts/wpCOP.tmp";
$test_data = fopen ($test_link, "w+");
fwrite ($test_data, print_R($parsed_json, TRUE));
fclose ($test_data);
$parsed_json = $parsed_json['result'][0];
$reply = $parsed_json[$value];
return $reply;
}
function Setvariabel($name,$type,$value) {
global $domoticzurl;
$reply=json_decode(file_get_contents($domoticzurl.'json.htm?type=command¶m=updateuservariable&vname='.$name.'&vtype='.$type.'&vvalue='.$value),true);
return $reply;
}
function Getvariable($idx,$value) {
global $domoticzurl;
$json_string = file_get_contents($domoticzurl.'json.htm?type=command¶m=getuservariable&idx='.$idx);
$parsed_json = json_decode($json_string, true);
$test_link = "/home/pi/domoticz/scripts/Getvariable.tmp";
$test_data = fopen ($test_link, "w+");
fwrite ($test_data, print_R($parsed_json, TRUE));
fclose ($test_data);
$parsed_json = $parsed_json['result'][0];
$reply = $parsed_json[$value];
return $reply;
}
function UpdateSensor($idx,$val) {
global $domoticzurl;
$reply=json_decode(file_get_contents($domoticzurl.'json.htm?type=command¶m=udevice&idx='.$idx.'&nvalue=0&svalue='.$val));
return $reply;
}
?>
Just because someone's opinion is different than your own it does not mean they are wrong.