Migrate to the Device Code Flow - removed unused code + api check fixed

This commit is contained in:
Philipp Wo 2025-03-10 10:03:43 +01:00
parent 321e24a440
commit 389034a61e

View File

@ -446,30 +446,19 @@ sub TadoAPI_LoadToken {
$Token = decode_json(<$TOKENFILE>) };
close($TOKENFILE);
if ( $@ || $tokenLifeTime < gettimeofday() ) {
Log3 $name, 5,
"TadoAPI $name" . ": "
. "Token expires at "
. localtime($tokenLifeTime);
# if token is about to expire, refresh him
if ( ( $tokenLifeTime - 45 ) < gettimeofday() ) {
Log3 $name, 5,
"TadoAPI $name" . ": "
. "Error while loading: $@ ,requesting new one"
if $@;
Log3 $name, 5,
"TadoAPI $name" . ": " . "Access Token is expired, requesting new one"
if $tokenLifeTime < gettimeofday();
"TadoAPI $name" . ": " . "Access Token will expire soon, refreshing";
$Token = TadoAPI_TokenRefresh($hash);
TadoAPI_CheckStatus($hash);
}
else {
Log3 $name, 5,
"TadoAPI $name" . ": "
. "Token expires at "
. localtime($tokenLifeTime);
# if token is about to expire, refresh him
if ( ( $tokenLifeTime - 45 ) < gettimeofday() ) {
Log3 $name, 5,
"TadoAPI $name" . ": " . "Access Token will expire soon, refreshing";
$Token = TadoAPI_TokenRefresh($hash);
}
}
return $Token if $Token;
}
TadoAPI_CheckStatus($hash);
@ -527,71 +516,6 @@ sub TadoAPI_RegisterNewDevice {
return;
}
sub TadoAPI_NewTokenRequest {
my $hash = shift;
my $name = $hash->{NAME};
my $device_code = $hash->{TADO_USER};
my $tokenFileName = $tokenFile . "_" . $name;
Log3 $name, 5, "TadoAPI $name" . ": " . "calling NewTokenRequest()";
my $data = {
client_id => $client_id,
device_code => $device_code,
grant_type => 'urn:ietf:params:oauth:grant-type:device_code'
};
my $param = {
url => $AuthURL,
method => 'POST',
timeout => 5,
hash => $hash,
data => $data
};
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
Log3 $name, $reqDebug, "TadoAPI $name" . ": " . "Request NEW $AuthURL";
my ( $err, $returnData ) = HttpUtils_BlockingGet($param);
if ( $err ne "" ) {
Log3 $name, 3,
"TadoAPI $name" . ": "
. "NewTokenRequest: Error while requesting "
. $param->{url}
. " - $err";
}
elsif ( $returnData ne "" ) {
Log3 $name, 5, "url " . $param->{url} . " - returned: $returnData";
my $decoded_data = eval { decode_json($returnData) };
if ($@) {
Log3 $name, 3, "TadoAPI $name" . ": "
. "NewTokenRequest: decode_json failed, invalid json. error: $@ ";
}
else {
#write token data in file
open( my $TOKENFILE, q{>}, $tokenFileName ) or do {
$apiStatus = 0;
$hash->{STATE} = "Token error";
return;
};
print $TOKENFILE $returnData . "\n";
close($TOKENFILE);
# token lifetime management
if ( defined($decoded_data->{'expires_in'}) ){
$hash->{TOKEN_LIFETIME} = gettimeofday() + $decoded_data->{'expires_in'};
}
$hash->{TOKEN_LIFETIME_HR} = localtime( $hash->{TOKEN_LIFETIME} );
Log3 $name, 5,
"TadoAPI $name" . ": "
. "Retrived new authentication token successfully. Valid until "
. localtime( $hash->{TOKEN_LIFETIME} );
$hash->{STATE} = "reachable";
return $decoded_data;
}
}
return;
}
sub TadoAPI_TokenRefresh {
my $hash = shift;
@ -947,9 +871,10 @@ sub TadoAPI_callback {
my $hash = $param->{hash};
my $name = $hash->{NAME};
print Dumper($param);
$param->{code} = 0 unless defined $param->{code};
if ( $param->{code} == 401 || $param->{code} == 400 ) {
if ( $param->{code} == 405 || $param->{code} == 400 ) {
$apiStatus = 1;
$hash->{STATE} = "reachable";
Log3 $name, 5,
@ -962,7 +887,7 @@ sub TadoAPI_callback {
$apiStatus = 0;
$hash->{STATE} = "error";
Log3 $name, 3,
"TadoAPI $name" . ": " . "API error: apiStatus $apiStatus ($err)";
"TadoAPI $name" . ": " . "API error (TadoAPI_callback): apiStatus $apiStatus ($err)";
}
return;
}