55from abc import ABC
66import asyncio
77from collections .abc import Callable
8+ import contextlib
89from http .cookies import SimpleCookie
910import json
1011import logging
@@ -243,10 +244,15 @@ async def _request_json(
243244 request_headers .update (headers )
244245
245246 try :
246- if url not in self ._login_urls .values () and not self .connected :
247+ if (
248+ url not in self ._login_urls .values ()
249+ and url != "/"
250+ and not self .connected
251+ ):
247252 _LOGGER .error ("Not connected, login first" )
248253 raise AirOSDeviceConnectionError from None
249254
255+ _LOGGER .error ("TESTv6 - Trying with URL: %s" , url )
250256 async with self .session .request (
251257 method ,
252258 url ,
@@ -257,11 +263,19 @@ async def _request_json(
257263 response .raise_for_status ()
258264 response_text = await response .text ()
259265 _LOGGER .debug ("Successfully fetched JSON from %s" , url )
266+ _LOGGER .error ("TESTv6 - Response: %s" , response_text )
260267
261268 # If this is the login request, we need to store the new auth data
262- if url in self ._login_urls .values ():
269+ if url in self ._login_urls .values () or url == "/" :
263270 self ._store_auth_data (response )
264271 self .connected = True
272+ # Assume v6 doesn't return JSON yet
273+ if url == self ._login_urls ["v6_alternative" ]:
274+ return response_text
275+
276+ # Just there for the cookies
277+ if method == "GET" and url == "/" :
278+ return {}
265279
266280 return json .loads (response_text )
267281 except aiohttp .ClientResponseError as err :
@@ -287,22 +301,47 @@ async def login(self) -> None:
287301 """Login to AirOS device."""
288302 payload = {"username" : self .username , "password" : self .password }
289303 try :
304+ _LOGGER .error ("TESTv6 - Trying default v8 login URL" )
290305 await self ._request_json (
291306 "POST" , self ._login_urls ["default" ], json_data = payload
292307 )
293308 except AirOSUrlNotFoundError :
294- pass # Try next URL
309+ _LOGGER .error ("TESTv6 - gives URL not found, trying alternative v6 URL" )
310+ # Try next URL
295311 except AirOSConnectionSetupError as err :
296312 raise AirOSConnectionSetupError ("Failed to login to AirOS device" ) from err
297313 else :
298314 return
299315
316+ # Start of v6, go for cookies
317+ _LOGGER .error ("TESTv6 - Trying to get / first for cookies" )
318+ with contextlib .suppress (Exception ):
319+ cookieresponse = await self ._request_json (
320+ "GET" , f"{ self .base_url } /" , authenticated = False
321+ )
322+ _LOGGER .error ("TESTv6 - Cookie response: %s" , cookieresponse )
323+
300324 try : # Alternative URL
301- await self ._request_json (
325+ v6_payload = {
326+ "username" : self .username ,
327+ "password" : self .password ,
328+ "uri" : "/index.cgi" ,
329+ }
330+ login_headers = {
331+ "User-Agent" : "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0" ,
332+ "Referer" : self ._login_urls ["v6_alternative" ],
333+ }
334+
335+ v6_response = await self ._request_json (
302336 "POST" ,
303337 self ._login_urls ["v6_alternative" ],
304- form_data = payload ,
338+ headers = login_headers ,
339+ form_data = v6_payload ,
305340 ct_form = True ,
341+ authenticated = True ,
342+ )
343+ _LOGGER .error (
344+ "TESTv6 - Trying to authenticate v6 responded in : %s" , v6_response
306345 )
307346 except AirOSConnectionSetupError as err :
308347 raise AirOSConnectionSetupError (
0 commit comments