48_BlinkCamera: camenable/disable fixed and Oauth change completed

git-svn-id: https://svn.fhem.de/fhem/trunk@30486 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
viegener
2025-11-03 21:37:40 +00:00
parent b01b5ef90d
commit 5dcf6c8344

View File

@@ -102,7 +102,6 @@ my $repositoryID = '$Id$';
# camEnable/camDisable working now for doorbells/lotus # camEnable/camDisable working now for doorbells/lotus
# liveview cmd will also set liveCam reading to identify stream # liveview cmd will also set liveCam reading to identify stream
# getThumbnail for doorbells working # getThumbnail for doorbells working
# 28.10.25 New oAuth protocol and 2fa # 28.10.25 New oAuth protocol and 2fa
# replace verify pin with "authorize" # replace verify pin with "authorize"
# add request2fa to get a new 2fa code for authorization # add request2fa to get a new 2fa code for authorization
@@ -113,9 +112,13 @@ my $repositoryID = '$Id$';
# refresh (only once if atoken available but access denied) - to be checked # refresh (only once if atoken available but access denied) - to be checked
# 2.11.25 final oauth adaptations # 2.11.25 final oauth adaptations
# arm/disarm tested # arm/disarm tested
# refresh for authtoken completed # refresh for authtoken completed/fixed
# # 3.11.25 final oauth adaptations
# # handle urlencoding for forms
# log changes for polling
# allow custom attribute for useragent <major.minor>IOS_<build number>
# camenable/disable to be tested
# #
# #
############################################################################## ##############################################################################
@@ -124,9 +127,6 @@ my $repositoryID = '$Id$';
############################################################################## ##############################################################################
# TASKS # TASKS
# #
# camenable/disable not working
# check if oauth expired triggers refresh correctly
# allow custom attribute for user-agent <major.minor>IOS_<build number>
# #
# --- old --- # --- old ---
# subtype for syncmodule needed? # subtype for syncmodule needed?
@@ -267,6 +267,7 @@ sub BlinkCamera_Initialize($) {
"network ". "network ".
"pollingTimeout ". "pollingTimeout ".
"homeScreenV3:0,1 ". "homeScreenV3:0,1 ".
"useragent:textField ".
$readingFnAttributes; $readingFnAttributes;
} }
@@ -585,6 +586,13 @@ sub BlinkCamera_DoCmd($$;$$$)
$authtoken = undef; $authtoken = undef;
} }
my ($trerr, $reftoken) = getKeyValue("BlinkCamera_RTOKEN_".$fuuid);
if ( ( defined($trerr) ) || ( ! defined($reftoken) ) ) {
# ignore error message here
$reftoken = undef;
}
$retryCount = 0 if ( ! defined( $retryCount ) ); $retryCount = 0 if ( ! defined( $retryCount ) );
# increase retrycount for next try # increase retrycount for next try
@@ -645,10 +653,17 @@ sub BlinkCamera_DoCmd($$;$$$)
####################### #######################
# Check for invalid auth token and just remove cmds # Check for invalid auth token and just remove cmds
if ( ($cmd ne "refresh") && ( defined( $hash->{AuthToken} ) )&& ( $hash->{AuthToken} eq "INVALID" ) ) { if ( ($cmd ne "refresh") && ( defined( $authtoken ) )&& ( $authtoken eq "INVALID" ) ) {
Log3 $name, 2, "BlinkCamera_DoCmd $name: failed due to invalid auth token ".$cmdString; Log3 $name, 2, "BlinkCamera_DoCmd $name: failed due to invalid auth token ".$cmdString;
return; return;
} }
#######################
# Check for invalid ref token and just remove cmds
if ( ($cmd eq "refresh") && ( ! defined( $reftoken ) ) ) {
Log3 $name, 2, "BlinkCamera_DoCmd $name: failed due to missing refresh token ".$cmdString;
return;
}
## get actual network and attr V3 ## get actual network and attr V3
my $net = BlinkCamera_GetNetwork( $hash ); my $net = BlinkCamera_GetNetwork( $hash );
@@ -759,7 +774,9 @@ sub BlinkCamera_DoCmd($$;$$$)
if ( ($cmd ne "authorize") && ($cmd ne "request2fa") && ($cmd ne "refresh") ) { if ( ($cmd ne "authorize") && ($cmd ne "request2fa") && ($cmd ne "refresh") ) {
$hash->{HU_DO_PARAMS}->{header} .= "\r\n".$BlinkCamera_UserAgentHeader." ".$BlinkCamera_useragent; my $useragent = AttrVal( $name, "useragent", $BlinkCamera_useragent );
$hash->{HU_DO_PARAMS}->{header} .= "\r\n".$BlinkCamera_UserAgentHeader." ".$useragent;
$hash->{HU_DO_PARAMS}->{header} .= "\r\n".$BlinkCamera_AuthorizationHeader." ".$authtoken; $hash->{HU_DO_PARAMS}->{header} .= "\r\n".$BlinkCamera_AuthorizationHeader." ".$authtoken;
} else { } else {
$hash->{HU_DO_PARAMS}->{header} .= "\r\n"."Content-Type: application/x-www-form-urlencoded"; $hash->{HU_DO_PARAMS}->{header} .= "\r\n"."Content-Type: application/x-www-form-urlencoded";
@@ -796,11 +813,13 @@ sub BlinkCamera_DoCmd($$;$$$)
} }
} }
if ( ! $ret ) { if ( ! $ret ) {
$password = uri_escape( $password );
$email = uri_escape( $email );
$hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_oauthform; $hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_oauthform;
$hash->{HU_DO_PARAMS}->{data} =~ s/q_password_q/$password/g; $hash->{HU_DO_PARAMS}->{data} =~ s/q_password_q/$password/g;
$hash->{HU_DO_PARAMS}->{data} =~ s/q_email_q/$email/g; $hash->{HU_DO_PARAMS}->{data} =~ s/q_email_q/$email/g;
$hash->{HU_DO_PARAMS}->{data} =~ s/q_name_q/$name/g;
Log3 $name, 4, "BlinkCamera_DoCmd $name: oauthform data :".$hash->{HU_DO_PARAMS}->{data}.":"; Log3 $name, 4, "BlinkCamera_DoCmd $name: oauthform data :".$hash->{HU_DO_PARAMS}->{data}.":";
} }
@@ -808,14 +827,9 @@ sub BlinkCamera_DoCmd($$;$$$)
####################### #######################
} elsif ( $cmd eq "refresh" ) { } elsif ( $cmd eq "refresh" ) {
my ($trerr, $reftoken) = getKeyValue("BlinkCamera_RTOKEN_".$fuuid);
if ( ( defined($trerr) ) || ( ! defined($reftoken) ) ) {
# ignore error message here
$reftoken = undef;
}
$ret = "BlinkCamera_DoCmd $name: no refresh token found for $cmd" if ( !defined( $reftoken ) );
if ( ! $ret ) { if ( ! $ret ) {
$reftoken = uri_escape( $reftoken );
$hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_oauthformrefresh; $hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_oauthformrefresh;
$hash->{HU_DO_PARAMS}->{data} =~ s/q_token_q/$reftoken/g; $hash->{HU_DO_PARAMS}->{data} =~ s/q_token_q/$reftoken/g;
@@ -846,17 +860,23 @@ sub BlinkCamera_DoCmd($$;$$$)
my $alert = ($cmd eq "camEnable")?"true":"false"; my $alert = ($cmd eq "camEnable")?"true":"false";
if ( $ctype eq "camera" ) { if ( $ctype eq "camera" ) {
$hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/network/".$net."/camera/".$par1."/update"; my $alert = ($cmd eq "camEnable")?"true":"false";
$hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/network/".$net."/camera/".$par1."/".(($cmd eq "camEnable")?"enable":"disable");
$hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_configCamAlertjson; Log3 $name, 4, "BlinkCamera_DoCmd $name: cam type: ".$ctype.": - no data :";
$hash->{HU_DO_PARAMS}->{data} =~ s/q_id_q/$par1/g; # if ( $ctype eq "camera" ) {
$hash->{HU_DO_PARAMS}->{data} =~ s/q_network_q/$net/g; # $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/network/".$net."/camera/".$par1."/update";
$hash->{HU_DO_PARAMS}->{data} =~ s/q_alert_q/$alert/g;
Log3 $name, 4, "BlinkCamera_DoCmd $name: cam type: ".$ctype.": - data :".$hash->{HU_DO_PARAMS}->{data}.":"; # $hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_configCamAlertjson;
# $hash->{HU_DO_PARAMS}->{data} =~ s/q_id_q/$par1/g;
# $hash->{HU_DO_PARAMS}->{data} =~ s/q_network_q/$net/g;
# $hash->{HU_DO_PARAMS}->{data} =~ s/q_alert_q/$alert/g;
# Log3 $name, 4, "BlinkCamera_DoCmd $name: cam type: ".$ctype.": - data :".$hash->{HU_DO_PARAMS}->{data}.":";
} elsif ( $ctype eq "owl" ) { } elsif ( $ctype eq "owl" ) {
$hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/api/v1/accounts/".$account."/networks/".$net."/owls/".$par1."/config"; $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/api/v1/accounts/".$account."/networks/".$net."/owls/".$par1."/config";
$hash->{HU_DO_PARAMS}->{header} .= "\r\n"."content-type: application/json";
$hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_configOwljson; $hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_configOwljson;
$hash->{HU_DO_PARAMS}->{data} =~ s/q_value_q/$alert/g; $hash->{HU_DO_PARAMS}->{data} =~ s/q_value_q/$alert/g;
Log3 $name, 4, "BlinkCamera_DoCmd $name: cam type: ".$ctype.": - data :".$hash->{HU_DO_PARAMS}->{data}.":"; Log3 $name, 4, "BlinkCamera_DoCmd $name: cam type: ".$ctype.": - data :".$hash->{HU_DO_PARAMS}->{data}.":";
@@ -866,6 +886,7 @@ sub BlinkCamera_DoCmd($$;$$$)
if ($cmd eq "camEnable") { if ($cmd eq "camEnable") {
$hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/api/v1/accounts/".$account. $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/api/v1/accounts/".$account.
"/networks/".$net."/doorbells/".$par1."/config"; "/networks/".$net."/doorbells/".$par1."/config";
$hash->{HU_DO_PARAMS}->{header} .= "\r\n"."content-type: application/json";
$hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_configLotusjson; $hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_configLotusjson;
$hash->{HU_DO_PARAMS}->{data} =~ s/q_value_q/$alert/g; $hash->{HU_DO_PARAMS}->{data} =~ s/q_value_q/$alert/g;
} else { } else {
@@ -880,56 +901,6 @@ sub BlinkCamera_DoCmd($$;$$$)
} }
# #######################
# } elsif ( ($cmd eq "camEnable") || ($cmd eq "camDisable" ) ) {
# my $ctype = "invalid";
# if ( ! defined( $net ) ) {
# $ret = "BlinkCamera_DoCmd $name: no network identifier found for $cmd - set attribute";
# } else {
# $ctype = BlinkCamera_GetCamType( $hash, $par1 );
# }
# if ( ! $ret ) {
# my $alert = ($cmd eq "camEnable")?"true":"false";
# if ( $ctype eq "camera" ) {
# $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/network/".$net."/camera/".$par1."/update";
# $hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_configCamAlertjson;
# $hash->{HU_DO_PARAMS}->{data} =~ s/q_id_q/$par1/g;
# $hash->{HU_DO_PARAMS}->{data} =~ s/q_network_q/$net/g;
# $hash->{HU_DO_PARAMS}->{data} =~ s/q_alert_q/$alert/g;
# Log3 $name, 4, "BlinkCamera_DoCmd $name: cam type: ".$ctype.": - data :".$hash->{HU_DO_PARAMS}->{data}.":";
# } elsif ( $ctype eq "owl" ) {
# $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/api/v1/accounts/".$account."/networks/".$net."/owls/".$par1."/config";
# $hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_configOwljson;
# $hash->{HU_DO_PARAMS}->{data} =~ s/q_value_q/$alert/g;
# Log3 $name, 4, "BlinkCamera_DoCmd $name: cam type: ".$ctype.": - data :".$hash->{HU_DO_PARAMS}->{data}.":";
# } elsif ( $ctype eq "lotus" ) {
# # $ret = "BlinkCamera_DoCmd $name: camera type (".$ctype.") unsupported !!";
# if ($cmd eq "camEnable") {
# $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/api/v1/accounts/".$account.
# "/networks/".$net."/doorbells/".$par1."/config";
# $hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_configLotusjson;
# $hash->{HU_DO_PARAMS}->{data} =~ s/q_value_q/$alert/g;
# } else {
# $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/api/v1/accounts/".$account.
# "/networks/".$net."/doorbells/".$par1."/disable";
# $hash->{HU_DO_PARAMS}->{data} = "";
# }
# Log3 $name, 4, "BlinkCamera_DoCmd $name: cam type: ".$ctype.": - data :".$hash->{HU_DO_PARAMS}->{data}.":";
# } else {
# $ret = "BlinkCamera_DoCmd $name: camera type (".$ctype.") unknown !!";
# }
# }
####################### #######################
} elsif ( ($cmd eq "arm") || ($cmd eq "disarm" ) ) { } elsif ( ($cmd eq "arm") || ($cmd eq "disarm" ) ) {
@@ -1113,6 +1084,7 @@ sub BlinkCamera_DoCmd($$;$$$)
# } # }
$hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/api/v1/accounts/".$account."/media/delete"; $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/api/v1/accounts/".$account."/media/delete";
$hash->{HU_DO_PARAMS}->{header} .= "\r\n"."content-type: application/json";
$hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_deleteVideojson; $hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_deleteVideojson;
$hash->{HU_DO_PARAMS}->{data} =~ s/q_id_q/$vid/g; $hash->{HU_DO_PARAMS}->{data} =~ s/q_id_q/$vid/g;
Log3 $name, 4, "BlinkCamera_DoCmd $name: data :".$hash->{HU_DO_PARAMS}->{data}.":"; Log3 $name, 4, "BlinkCamera_DoCmd $name: data :".$hash->{HU_DO_PARAMS}->{data}.":";
@@ -1162,7 +1134,7 @@ sub BlinkCamera_DoCmd($$;$$$)
Log3 $name, 4, "BlinkCamera_DoCmd $name: call url :".$hash->{HU_DO_PARAMS}->{url}.": "; Log3 $name, 4, "BlinkCamera_DoCmd $name: call url :".$hash->{HU_DO_PARAMS}->{url}.": ";
Log3 $name, 4, "BlinkCamera_DoCmd $name: header :".$hash->{HU_DO_PARAMS}->{header}.": "; Log3 $name, 4, "BlinkCamera_DoCmd $name: header :".$hash->{HU_DO_PARAMS}->{header}.": ";
Log3 $name, 4, "BlinkCamera_DoCmd $name: data : ".$hash->{HU_DO_PARAMS}->{data}.": "; Log3 $name, 4, "BlinkCamera_DoCmd $name: data : ".$hash->{HU_DO_PARAMS}->{data}.": " if ( defined( $hash->{HU_DO_PARAMS}->{data} ) );
HttpUtils_NonblockingGet( $hash->{HU_DO_PARAMS} ); HttpUtils_NonblockingGet( $hash->{HU_DO_PARAMS} );
@@ -1838,8 +1810,12 @@ sub BlinkCamera_Callback($$$)
if ( ( $jo->{message} eq "Unauthorized Access" ) ) { if ( ( $jo->{message} eq "Unauthorized Access" ) ) {
# reset authtoken if {"message":"Unauthorized Access"} --> will be re checked on next call # reset authtoken if {"message":"Unauthorized Access"} --> will be re checked on next call
setKeyValue( "BlinkCamera_ATOKEN_".$fuuid, undef ); setKeyValue( "BlinkCamera_ATOKEN_".$fuuid, undef );
# also clean refreshtoken if refresh did fail if ( $cmd eq "refresh" ) {
setKeyValue( "BlinkCamera_RTOKEN_".$fuuid, undef ) if ( $cmd eq "refresh" ); Log3 $name, 4, "BlinkCamera_Callback $name: refresh failed (unauthorized) - remove refresh token";
# also clean refreshtoken if refresh did fail
setKeyValue( "BlinkCamera_RTOKEN_".$fuuid, undef );
} else {
}
} }
} else { } else {
$result = $jo; $result = $jo;
@@ -1848,7 +1824,8 @@ sub BlinkCamera_Callback($$$)
} }
} }
$ll = 2 if ( $hidden ); $ll = 3 if ( $polling );
$ll = 2 if ( $hidden && ( ! $polling ) );
$hash->{POLLING} = 0 if ( $polling ); $hash->{POLLING} = 0 if ( $polling );
################################################## ##################################################
@@ -2872,6 +2849,9 @@ sub BlinkCamera_AnalyzeAlertResults( $$$ ) {
NOTE: This attribute is deprecated and not needed anymore, since the old API has been switched off by Blink (default is on = 1) NOTE: This attribute is deprecated and not needed anymore, since the old API has been switched off by Blink (default is on = 1)
</li> </li>
<li><code>useragent &lt;text&gt;</code><br>This attribute can beu used to overwrite the default useragent used for communication with blink servers (default: 49.2IOS_2510241256). This might be relevant for newer app version to avoid warnings from blink. The format seems to be &lt;app version&gt;&lt;platform: e.g. IOS&gt;_&lt;buildnum&gt;.
</li>
</ul> </ul>