@@ -144,6 +144,7 @@ async def request(
144144 method : str ,
145145 returns : str ,
146146 url : str ,
147+ websession : ClientSession = None ,
147148 headers : dict = None ,
148149 params : dict = None ,
149150 data : dict = None ,
@@ -166,12 +167,12 @@ async def request(
166167 return await call_method (
167168 method = method ,
168169 url = url ,
170+ websession = websession ,
169171 headers = headers ,
170172 params = params ,
171173 data = data ,
172174 json = json ,
173175 allow_redirects = allow_redirects ,
174- use_websession = False ,
175176 )
176177 except ClientResponseError as err :
177178 message = (
@@ -243,6 +244,7 @@ async def request(
243244 return await call_method (
244245 method = method ,
245246 url = url ,
247+ websession = websession ,
246248 headers = headers ,
247249 params = params ,
248250 data = data ,
@@ -270,6 +272,7 @@ async def request(
270272 return await call_method (
271273 method = method ,
272274 url = url ,
275+ websession = websession ,
273276 headers = headers ,
274277 params = params ,
275278 data = data ,
@@ -299,114 +302,119 @@ async def request(
299302
300303 async def _oauth_authenticate (self ) -> (str , int ):
301304
302- # retrieve authentication page
303- _LOGGER .debug ("Retrieving authentication page" )
304- resp , text = await self .request (
305- method = "get" ,
306- returns = "text" ,
307- url = OAUTH_AUTHORIZE_URI ,
308- headers = {
309- "redirect" : "follow" ,
310- },
311- params = {
312- "client_id" : OAUTH_CLIENT_ID ,
313- "code_challenge" : get_code_challenge (self ._code_verifier ),
314- "code_challenge_method" : "S256" ,
315- "redirect_uri" : OAUTH_REDIRECT_URI ,
316- "response_type" : "code" ,
317- "scope" : "MyQ_Residential offline_access" ,
318- },
319- login_request = True ,
320- )
321-
322- # Perform login to MyQ
323- _LOGGER .debug ("Performing login to MyQ" )
324- parser = HTMLElementFinder (
325- tag = "input" ,
326- return_attr = "value" ,
327- with_attr = ("name" , "__RequestVerificationToken" ),
328- )
329-
330- # Verification token is within the returned page as <input name="__RequestVerificationToken" value=<token>>
331- # Retrieve token from the page.
332- parser .feed (text )
333- request_verification_token = parser .result [0 ]
334-
335- resp , _ = await self .request (
336- method = "post" ,
337- returns = "response" ,
338- url = resp .url ,
339- headers = {
340- "Content-Type" : "application/x-www-form-urlencoded" ,
341- "Cookie" : resp .cookies .output (attrs = []),
342- "User-Agent" : "null" ,
343- },
344- data = {
345- "Email" : self .username ,
346- "Password" : self .__credentials .get ("password" ),
347- "__RequestVerificationToken" : request_verification_token ,
348- },
349- allow_redirects = False ,
350- login_request = True ,
351- )
305+ async with ClientSession () as session :
306+ # retrieve authentication page
307+ _LOGGER .debug ("Retrieving authentication page" )
308+ resp , text = await self .request (
309+ method = "get" ,
310+ returns = "text" ,
311+ url = OAUTH_AUTHORIZE_URI ,
312+ websession = session ,
313+ headers = {
314+ "redirect" : "follow" ,
315+ },
316+ params = {
317+ "client_id" : OAUTH_CLIENT_ID ,
318+ "code_challenge" : get_code_challenge (self ._code_verifier ),
319+ "code_challenge_method" : "S256" ,
320+ "redirect_uri" : OAUTH_REDIRECT_URI ,
321+ "response_type" : "code" ,
322+ "scope" : "MyQ_Residential offline_access" ,
323+ },
324+ login_request = True ,
325+ )
352326
353- # We're supposed to receive back at least 2 cookies. If not then authentication failed.
354- if len (resp .cookies ) < 2 :
355- message = (
356- "Invalid MyQ credentials provided. Please recheck login and password."
327+ # Perform login to MyQ
328+ _LOGGER .debug ("Performing login to MyQ" )
329+ parser = HTMLElementFinder (
330+ tag = "input" ,
331+ return_attr = "value" ,
332+ with_attr = ("name" , "__RequestVerificationToken" ),
357333 )
358- self ._invalid_credentials = True
359- _LOGGER .debug (message )
360- raise InvalidCredentialsError (message )
361334
362- # Intercept redirect back to MyQ iOS app
363- _LOGGER .debug ("Calling redirect page" )
364- resp , _ = await self .request (
365- method = "get" ,
366- returns = "response" ,
367- url = f"{ OAUTH_BASE_URI } { resp .headers ['Location' ]} " ,
368- headers = {
369- "Cookie" : resp .cookies .output (attrs = []),
370- "User-Agent" : "null" ,
371- },
372- allow_redirects = False ,
373- login_request = True ,
374- )
335+ # Verification token is within the returned page as <input name="__RequestVerificationToken" value=<token>>
336+ # Retrieve token from the page.
337+ parser .feed (text )
338+ request_verification_token = parser .result [0 ]
339+
340+ resp , _ = await self .request (
341+ method = "post" ,
342+ returns = "response" ,
343+ url = resp .url ,
344+ websession = session ,
345+ headers = {
346+ "Content-Type" : "application/x-www-form-urlencoded" ,
347+ "Cookie" : resp .cookies .output (attrs = []),
348+ "User-Agent" : "null" ,
349+ },
350+ data = {
351+ "Email" : self .username ,
352+ "Password" : self .__credentials .get ("password" ),
353+ "__RequestVerificationToken" : request_verification_token ,
354+ },
355+ allow_redirects = False ,
356+ login_request = True ,
357+ )
375358
376- # Retrieve token
377- _LOGGER .debug ("Getting token" )
378- redirect_url = f"{ OAUTH_BASE_URI } { resp .headers ['Location' ]} "
379-
380- resp , data = await self .request (
381- returns = "json" ,
382- method = "post" ,
383- url = OAUTH_TOKEN_URI ,
384- headers = {
385- "Content-Type" : "application/x-www-form-urlencoded" ,
386- "User-Agent" : "null" ,
387- },
388- data = {
389- "client_id" : OAUTH_CLIENT_ID ,
390- "client_secret" : OAUTH_CLIENT_SECRET ,
391- "code" : parse_qs (urlsplit (redirect_url ).query ).get ("code" , "" ),
392- "code_verifier" : self ._code_verifier ,
393- "grant_type" : "authorization_code" ,
394- "redirect_uri" : OAUTH_REDIRECT_URI ,
395- "scope" : parse_qs (urlsplit (redirect_url ).query ).get (
396- "code" , "MyQ_Residential offline_access"
397- ),
398- },
399- login_request = True ,
400- )
359+ # We're supposed to receive back at least 2 cookies. If not then authentication failed.
360+ if len (resp .cookies ) < 2 :
361+ message = (
362+ "Invalid MyQ credentials provided. Please recheck login and password."
363+ )
364+ self ._invalid_credentials = True
365+ _LOGGER .debug (message )
366+ raise InvalidCredentialsError (message )
367+
368+ # Intercept redirect back to MyQ iOS app
369+ _LOGGER .debug ("Calling redirect page" )
370+ resp , _ = await self .request (
371+ method = "get" ,
372+ returns = "response" ,
373+ url = f"{ OAUTH_BASE_URI } { resp .headers ['Location' ]} " ,
374+ websession = session ,
375+ headers = {
376+ "Cookie" : resp .cookies .output (attrs = []),
377+ "User-Agent" : "null" ,
378+ },
379+ allow_redirects = False ,
380+ login_request = True ,
381+ )
401382
402- token = f"{ data .get ('token_type' )} { data .get ('access_token' )} "
403- try :
404- expires = int (data .get ("expires_in" , DEFAULT_TOKEN_REFRESH ))
405- except ValueError :
406- _LOGGER .debug (
407- f"Expires { data .get ('expires_in' )} received is not an integer, using default."
383+ # Retrieve token
384+ _LOGGER .debug ("Getting token" )
385+ redirect_url = f"{ OAUTH_BASE_URI } { resp .headers ['Location' ]} "
386+
387+ resp , data = await self .request (
388+ returns = "json" ,
389+ method = "post" ,
390+ url = OAUTH_TOKEN_URI ,
391+ websession = session ,
392+ headers = {
393+ "Content-Type" : "application/x-www-form-urlencoded" ,
394+ "User-Agent" : "null" ,
395+ },
396+ data = {
397+ "client_id" : OAUTH_CLIENT_ID ,
398+ "client_secret" : OAUTH_CLIENT_SECRET ,
399+ "code" : parse_qs (urlsplit (redirect_url ).query ).get ("code" , "" ),
400+ "code_verifier" : self ._code_verifier ,
401+ "grant_type" : "authorization_code" ,
402+ "redirect_uri" : OAUTH_REDIRECT_URI ,
403+ "scope" : parse_qs (urlsplit (redirect_url ).query ).get (
404+ "code" , "MyQ_Residential offline_access"
405+ ),
406+ },
407+ login_request = True ,
408408 )
409- expires = DEFAULT_TOKEN_REFRESH * 2
409+
410+ token = f"{ data .get ('token_type' )} { data .get ('access_token' )} "
411+ try :
412+ expires = int (data .get ("expires_in" , DEFAULT_TOKEN_REFRESH ))
413+ except ValueError :
414+ _LOGGER .debug (
415+ f"Expires { data .get ('expires_in' )} received is not an integer, using default."
416+ )
417+ expires = DEFAULT_TOKEN_REFRESH * 2
410418
411419 if expires < DEFAULT_TOKEN_REFRESH * 2 :
412420 _LOGGER .debug (
0 commit comments