@@ -32,14 +32,18 @@ class KarcherHome:
3232    """Main class to access Karcher Home Robots API""" 
3333
3434    @classmethod  
35-     async  def  create (cls , country : str  =  'GB' , language : Language  =  Language .EN ):
35+     async  def  create (cls , country : str  =  'GB' , language : Language  =  Language .EN ,  session :  aiohttp . ClientSession   =   None ):
3636        """Create Karcher Home Robots API instance""" 
3737
3838        self  =  KarcherHome ()
3939        self ._country  =  country .upper ()
4040        self ._base_url  =  REGION_URLS [get_region_by_country (self ._country )]
4141        self ._language  =  language 
4242
43+         if  session  is  not None :
44+             self ._http_external  =  True 
45+             self ._http  =  session 
46+ 
4347        d  =  await  self .get_urls ()
4448        # Update base URLs 
4549        if  d .app_api  !=  '' :
@@ -62,11 +66,27 @@ def __init__(self):
6266        self ._device_props  =  {}
6367        self ._wait_events  =  {}
6468
69+     def  __del__ (self ):
70+         """Destructor""" 
71+ 
72+         self .close ()
73+ 
74+     async  def  close (self ):
75+         """Close underlying connections""" 
76+ 
77+         if  self ._mqtt  is  not None :
78+             self ._mqtt .disconnect ()
79+             self ._mqtt  =  None 
80+ 
81+         if  self ._http  is  not None :
82+             if  self ._http_external :
83+                 self ._http .close ()
84+             self ._http  =  None 
85+ 
6586    async  def  _request (self , method : str , url : str , ** kwargs ) ->  aiohttp .ClientResponse :
66-         session  =  aiohttp .ClientSession ()
67-         # TODO: Fix SSL 
68-         # requests.packages.urllib3.disable_warnings() 
69-         # session.skip = False 
87+         if  self ._http  is  None :
88+             self ._http_external  =  False 
89+             self ._http  =  aiohttp .ClientSession ()
7090
7191        headers  =  {}
7292        if  kwargs .get ('headers' ) is  not None :
@@ -113,27 +133,32 @@ async def _request(self, method: str, url: str, **kwargs) -> aiohttp.ClientRespo
113133        headers ['nonce' ] =  nonce 
114134
115135        kwargs ['headers' ] =  headers 
136+         # TODO: Fix SSL 
116137        kwargs ['verify_ssl' ] =  False 
117-         return  await  session .request (method , self ._base_url  +  url , ** kwargs )
138+         return  await  self . _http .request (method , self ._base_url  +  url , ** kwargs )
118139
119140    async  def  _download (self , url ) ->  bytes :
120-         session  =  aiohttp .ClientSession ()
121141        headers  =  {
122142            'User-Agent' : 'Android_'  +  TENANT_ID ,
123143        }
124144
125-         resp  =  await  session .get (url , headers = headers )
145+         resp  =  await  self . _http .get (url , headers = headers )
126146        if  resp .status  !=  200 :
127147            raise  KarcherHomeException (- 1 ,
128148                                       'HTTP error: '  +  str (resp .status_code ))
129149
130-         return  await  resp .content .read (- 1 )
150+         data  =  await  resp .content .read (- 1 )
151+         resp .close ()
152+ 
153+         return  data 
131154
132155    async  def  _process_response (self , resp : aiohttp .ClientResponse , prop = None ) ->  Any :
133156        if  resp .status  !=  200 :
134157            raise  KarcherHomeException (- 1 ,
135158                                       'HTTP error: '  +  str (resp .status ))
136159        data  =  await  resp .json ()
160+         resp .close ()
161+ 
137162        # Check for error response 
138163        if  data ['code' ] !=  0 :
139164            handle_error_code (data ['code' ], data ['msg' ])
@@ -252,9 +277,7 @@ async def logout(self):
252277            'POST' , '/user-center/auth/logout' ))
253278        self ._session  =  None 
254279
255-         if  self ._mqtt  is  not None :
256-             self ._mqtt .disconnect ()
257-             self ._mqtt  =  None 
280+         await  self .close ()
258281
259282    async  def  get_devices (self ) ->  List [Device ]:
260283        """Get all user devices.""" 
0 commit comments