fixed define bug - better error handling if no token or device code set

This commit is contained in:
Philipp Wo 2025-03-20 03:11:11 +01:00
parent 602ee3f512
commit b4c5586ce7

View File

@ -104,8 +104,8 @@ sub TadoAPI_Define {
my ( $user, $homeID ) = @a; my ( $user, $homeID ) = @a;
Log3 $name, 3, "TadoAPI_Define $name: called "; Log3 $name, 3, "TadoAPI_Define $name: called ";
$hash->{STATE} = "defined";
$hash->{DEF} = "no device_code set"; defined $user ? ($hash->{STATE} = "defined") : ($hash->{DEF} = "no_device_code_set");
# Initialize the device # Initialize the device
return $@ unless ( FHEM::Meta::SetInternals($hash) ); return $@ unless ( FHEM::Meta::SetInternals($hash) );
@ -121,7 +121,7 @@ sub TadoAPI_Define {
return TadoAPI_Catch($@) if $@; return TadoAPI_Catch($@) if $@;
} }
if ( defined($user) && $user ne "no device_code set" ) { if ( defined($user) && $user ne "no_device_code_set" ) {
TadoAPI_CheckStatus($hash); TadoAPI_CheckStatus($hash);
TadoAPI_NewTokenRequest($hash); TadoAPI_NewTokenRequest($hash);
@ -140,7 +140,7 @@ sub TadoAPI_Define {
} }
} }
else { else {
$hash->{STATE} = "no device_code set"; $hash->{STATE} = "no_device_code_set";
} }
return; return;
@ -494,12 +494,12 @@ sub TadoAPI_RegisterNewDevice {
. "RegisterNewDevice: decode_json failed, invalid json. error: $@ "; . "RegisterNewDevice: decode_json failed, invalid json. error: $@ ";
} }
else { else {
Log3 $name, 5, Log3 $name, 3,
"TadoAPI $name" . ": " "TadoAPI $name" . ": "
. "New Device - Please go to " . "New Device - Please go to URL: "
. $decoded_data->{'verification_uri_complete'}; . $decoded_data->{'verification_uri_complete'};
$hash->{STATE} = "API-reachable"; $hash->{STATE} = "OK";
return ($decoded_data->{'verification_uri_complete'}, $decoded_data->{'device_code'}); return ($decoded_data->{'verification_uri_complete'}, $decoded_data->{'device_code'});
} }
@ -512,9 +512,27 @@ sub TadoAPI_NewTokenRequest {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $device_code = $hash->{TADO_USER}; my $device_code = $hash->{TADO_USER};
my $tokenFileName = $tokenFile . "_" . $name; my $tokenFileName = $tokenFile . "_" . $name;
my $Token = undef;
Log3 $name, 5, "TadoAPI $name" . ": " . "calling NewTokenRequest()"; Log3 $name, 5, "TadoAPI $name" . ": " . "calling NewTokenRequest()";
# Check if 'refresh_token' exists or a new token is needed
my $TOKENFILE;
eval {
open( $TOKENFILE, q{<}, $tokenFileName ) or do {
return;
};
$Token = decode_json(<$TOKENFILE>) };
close($TOKENFILE);
if (exists $Token->{'refresh_token'}) {
Log3 $name, 3, "TadoAPI $name" . ": " . "Refresh token exists - OK\n";
return;
} else {
Log3 $name, 3, "TadoAPI $name" . ": " . "No Refresh token - NewTokenRequest start\n";
}
my $data = { my $data = {
client_id => $client_id, client_id => $client_id,
device_code => $device_code, device_code => $device_code,
@ -566,7 +584,7 @@ sub TadoAPI_NewTokenRequest {
"TadoAPI $name" . ": " "TadoAPI $name" . ": "
. "Retrived new authentication token successfully. Valid until " . "Retrived new authentication token successfully. Valid until "
. localtime( $hash->{TOKEN_LIFETIME} ); . localtime( $hash->{TOKEN_LIFETIME} );
$hash->{STATE} = "API-reachable"; $hash->{STATE} = "reachable";
# refresh FHEM device # refresh FHEM device
TadoAPI_UpdateFn($hash); TadoAPI_UpdateFn($hash);
return $decoded_data; return $decoded_data;
@ -642,7 +660,7 @@ sub TadoAPI_TokenRefresh {
return; return;
} else { } else {
#write token data in file #write token data in file
open( my $TOKENFILE, q{>}, $tokenFileName ) or do { open( $TOKENFILE, q{>}, $tokenFileName ) or do {
$apiStatus = 0; $apiStatus = 0;
$hash->{STATE} = "Token open error"; $hash->{STATE} = "Token open error";
return; return;
@ -939,7 +957,7 @@ sub TadoAPI_callback {
if ( $param->{code} == 405 || $param->{code} == 400 ) { if ( $param->{code} == 405 || $param->{code} == 400 ) {
$apiStatus = 1; $apiStatus = 1;
$hash->{STATE} = "API-reachable"; $hash->{STATE} = "reachable";
Log3 $name, 5, Log3 $name, 5,
"TadoAPI $name" . ": " "TadoAPI $name" . ": "
. "API is reachable. Callback Status: " . "API is reachable. Callback Status: "