@@ -32,14 +32,18 @@ class KarcherHome:
32
32
"""Main class to access Karcher Home Robots API"""
33
33
34
34
@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 ):
36
36
"""Create Karcher Home Robots API instance"""
37
37
38
38
self = KarcherHome ()
39
39
self ._country = country .upper ()
40
40
self ._base_url = REGION_URLS [get_region_by_country (self ._country )]
41
41
self ._language = language
42
42
43
+ if session is not None :
44
+ self ._http_external = True
45
+ self ._http = session
46
+
43
47
d = await self .get_urls ()
44
48
# Update base URLs
45
49
if d .app_api != '' :
@@ -62,11 +66,27 @@ def __init__(self):
62
66
self ._device_props = {}
63
67
self ._wait_events = {}
64
68
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
+
65
86
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 ()
70
90
71
91
headers = {}
72
92
if kwargs .get ('headers' ) is not None :
@@ -113,27 +133,32 @@ async def _request(self, method: str, url: str, **kwargs) -> aiohttp.ClientRespo
113
133
headers ['nonce' ] = nonce
114
134
115
135
kwargs ['headers' ] = headers
136
+ # TODO: Fix SSL
116
137
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 )
118
139
119
140
async def _download (self , url ) -> bytes :
120
- session = aiohttp .ClientSession ()
121
141
headers = {
122
142
'User-Agent' : 'Android_' + TENANT_ID ,
123
143
}
124
144
125
- resp = await session .get (url , headers = headers )
145
+ resp = await self . _http .get (url , headers = headers )
126
146
if resp .status != 200 :
127
147
raise KarcherHomeException (- 1 ,
128
148
'HTTP error: ' + str (resp .status_code ))
129
149
130
- return await resp .content .read (- 1 )
150
+ data = await resp .content .read (- 1 )
151
+ resp .close ()
152
+
153
+ return data
131
154
132
155
async def _process_response (self , resp : aiohttp .ClientResponse , prop = None ) -> Any :
133
156
if resp .status != 200 :
134
157
raise KarcherHomeException (- 1 ,
135
158
'HTTP error: ' + str (resp .status ))
136
159
data = await resp .json ()
160
+ resp .close ()
161
+
137
162
# Check for error response
138
163
if data ['code' ] != 0 :
139
164
handle_error_code (data ['code' ], data ['msg' ])
@@ -252,9 +277,7 @@ async def logout(self):
252
277
'POST' , '/user-center/auth/logout' ))
253
278
self ._session = None
254
279
255
- if self ._mqtt is not None :
256
- self ._mqtt .disconnect ()
257
- self ._mqtt = None
280
+ await self .close ()
258
281
259
282
async def get_devices (self ) -> List [Device ]:
260
283
"""Get all user devices."""
0 commit comments