diff --git a/fhem/FHEM/48_BlinkCamera.pm b/fhem/FHEM/48_BlinkCamera.pm index 10899be30..491923c9f 100644 --- a/fhem/FHEM/48_BlinkCamera.pm +++ b/fhem/FHEM/48_BlinkCamera.pm @@ -102,7 +102,6 @@ my $repositoryID = '$Id$'; # camEnable/camDisable working now for doorbells/lotus # liveview cmd will also set liveCam reading to identify stream # getThumbnail for doorbells working - # 28.10.25 New oAuth protocol and 2fa # replace verify pin with "authorize" # 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 # 2.11.25 final oauth adaptations # 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 IOS_ +# camenable/disable to be tested + # # ############################################################################## @@ -124,9 +127,6 @@ my $repositoryID = '$Id$'; ############################################################################## # TASKS # -# camenable/disable not working -# check if oauth expired triggers refresh correctly -# allow custom attribute for user-agent IOS_ # # --- old --- # subtype for syncmodule needed? @@ -267,6 +267,7 @@ sub BlinkCamera_Initialize($) { "network ". "pollingTimeout ". "homeScreenV3:0,1 ". + "useragent:textField ". $readingFnAttributes; } @@ -585,6 +586,13 @@ sub BlinkCamera_DoCmd($$;$$$) $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 ) ); # increase retrycount for next try @@ -645,10 +653,17 @@ sub BlinkCamera_DoCmd($$;$$$) ####################### # 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; 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 my $net = BlinkCamera_GetNetwork( $hash ); @@ -759,7 +774,9 @@ sub BlinkCamera_DoCmd($$;$$$) 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; } else { $hash->{HU_DO_PARAMS}->{header} .= "\r\n"."Content-Type: application/x-www-form-urlencoded"; @@ -796,11 +813,13 @@ sub BlinkCamera_DoCmd($$;$$$) } } if ( ! $ret ) { + $password = uri_escape( $password ); + $email = uri_escape( $email ); + $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_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}.":"; } @@ -808,14 +827,9 @@ sub BlinkCamera_DoCmd($$;$$$) ####################### } 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 ) { + $reftoken = uri_escape( $reftoken ); + $hash->{HU_DO_PARAMS}->{data} = $BlinkCamera_oauthformrefresh; $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"; 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; - $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}.":"; + Log3 $name, 4, "BlinkCamera_DoCmd $name: cam type: ".$ctype.": - no data :"; + # 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}->{header} .= "\r\n"."content-type: application/json"; $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}.":"; @@ -866,6 +886,7 @@ sub BlinkCamera_DoCmd($$;$$$) if ($cmd eq "camEnable") { $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."/api/v1/accounts/".$account. "/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} =~ s/q_value_q/$alert/g; } 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" ) ) { @@ -1113,6 +1084,7 @@ sub BlinkCamera_DoCmd($$;$$$) # } $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} =~ s/q_id_q/$vid/g; 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: 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} ); @@ -1838,8 +1810,12 @@ sub BlinkCamera_Callback($$$) if ( ( $jo->{message} eq "Unauthorized Access" ) ) { # reset authtoken if {"message":"Unauthorized Access"} --> will be re checked on next call setKeyValue( "BlinkCamera_ATOKEN_".$fuuid, undef ); - # also clean refreshtoken if refresh did fail - setKeyValue( "BlinkCamera_RTOKEN_".$fuuid, undef ) if ( $cmd eq "refresh" ); + 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 { $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 ); ################################################## @@ -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) +
  • useragent <text>
    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 <app version><platform: e.g. IOS>_<buildnum>. +
  • +