@@ -172,69 +172,61 @@ def __init__(self, host: str, user: str = None, pwd: str = None) -> None:
172172 self ._ws_listening = False
173173 self .websocket : Optional [OpenEVSEWebsocket ] = None # type: ignore
174174
175- def send_command (self , command : str ) -> tuple | None :
175+ async def send_command (self , command : str ) -> tuple | None :
176176 """Send a RAPI command to the charger and parses the response."""
177+ auth = None
177178 url = f"{ self .url } r"
178179 data = {"json" : 1 , "rapi" : command }
179180
181+ if self ._user and self ._password :
182+ auth = aiohttp .BasicAuth (self ._user , self ._password )
183+
180184 _LOGGER .debug ("Posting data: %s to %s" , command , url )
181- if self ._user is not None :
182- value = requests .post (url , data = data , auth = (self ._user , self ._pwd ))
183- else :
184- value = requests .post (url , data = data )
185+ async with aiohttp .ClientSession () as session :
186+ async with session .post (url , data = data , auth = auth ) as resp :
187+ if resp .status == 400 :
188+ _LOGGER .debug ("JSON error: %s" , await resp .text ())
189+ raise ParseJSONError
190+ if resp .status == 401 :
191+ _LOGGER .debug ("Authentication error: %s" , await resp )
192+ raise AuthenticationError
185193
186- if value .status_code == 400 :
187- _LOGGER .debug ("JSON error: %s" , value .text )
188- raise ParseJSONError
189- if value .status_code == 401 :
190- _LOGGER .debug ("Authentication error: %s" , value )
191- raise AuthenticationError
194+ value = await resp .json ()
192195
193- if "ret" not in value .json ():
194- return False , ""
195- resp = value .json ()
196- return resp ["cmd" ], resp ["ret" ]
196+ if "ret" not in value :
197+ return False , ""
198+ return value ["cmd" ], value ["ret" ]
197199
198200 async def update (self ) -> None :
199201 """Update the values."""
202+ auth = None
203+ urls = [f"{ self .url } config" ]
204+
205+ if self ._user and self ._password :
206+ auth = aiohttp .BasicAuth (self ._user , self ._password )
207+
200208 if not self ._ws_listening :
201209 urls = [f"{ self .url } status" , f"{ self .url } config" ]
202210
211+ async with aiohttp .ClientSession () as session :
203212 for url in urls :
204213 _LOGGER .debug ("Updating data from %s" , url )
205- if self . _user is not None :
206- value = requests . get ( url , auth = ( self . _user , self . _pwd ))
207- else :
208- value = requests . get ( url )
214+ async with session . get ( url , auth = auth ) as resp :
215+ if resp . status == 401 :
216+ _LOGGER . debug ( "Authentication error: %s" , resp )
217+ raise AuthenticationError
209218
210- if value .status_code == 401 :
211- _LOGGER .debug ("Authentication error: %s" , value )
212- raise AuthenticationError
213-
214- if "/status" in url :
215- self ._status = value .json ()
216- else :
217- self ._config = value .json ()
219+ if "/status" in url :
220+ self ._status = await resp .json ()
221+ else :
222+ self ._config = await resp .json ()
218223
219224 # Start Websocket listening
220225 self .websocket = OpenEVSEWebsocket (
221226 self .url , self ._update_status , self ._user , self ._pwd
222227 )
223228 await self .websocket .listen ()
224229 self ._ws_listening = True
225- else :
226- url = f"{ self .url } /config"
227- _LOGGER .debug ("Updating data from %s" , url )
228- if self ._user is not None :
229- value = requests .get (url , auth = (self ._user , self ._pwd ))
230- else :
231- value = requests .get (url )
232-
233- if value .status_code == 401 :
234- _LOGGER .debug ("Authentication error: %s" , value )
235- raise AuthenticationError
236-
237- self ._config = value .json ()
238230
239231 def _update_status (self , msgtype , data , error ):
240232 """Update data from websocket listener."""
0 commit comments