sampsyo / wideq

reverse-engineered client for the LG SmartThinQ API
https://pypi.org/project/wideq/
MIT License
331 stars 159 forks source link

APIError 9003 session creation failure #122

Closed pifou25 closed 4 years ago

pifou25 commented 4 years ago

Hi,

I try to use wideq lib as a REST API with a Flask server integration. It works for a while, then it fail with api error 9003 when the session times out I suppose:

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): eic.lgthinq.com:46030 DEBUG:urllib3.connectionpool:https://eic.lgthinq.com:46030 "POST /api/member/login HTTP/1.1" 200 86 DEBUG:root:(9003, '세션생성실패') Traceback (most recent call last): File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1799, in dispatch_request return self.view_functionsrule.endpoint File "/var/www/html/plugins/lgthinq/resources/daemon/wideqServer.py", line 267, in monitor device = client.get_device(device_id) File "/usr/local/lib/python3.7/dist-packages/wideq/client.py", line 135, in get_device for device in self.devices: File "/usr/local/lib/python3.7/dist-packages/wideq/client.py", line 126, in devices self._devices = self.session.get_devices() File "/usr/local/lib/python3.7/dist-packages/wideq/client.py", line 117, in session self._session, self._devices = self.auth.start_session() File "/usr/local/lib/python3.7/dist-packages/wideq/core.py", line 362, in start_session self.gateway.country, self.gateway.language) File "/usr/local/lib/python3.7/dist-packages/wideq/core.py", line 258, in login return lgedm_post(url, data) File "/usr/local/lib/python3.7/dist-packages/wideq/core.py", line 213, in lgedm_post raise APIError(code, message) wideq.core.APIError: (9003, '세션생성실패')

The korean message is "session creation failure" Is there a way to catch the error, renew the session and try again the request in the same transaction? Thanks

sampsyo commented 4 years ago

Hi! While I haven't seen that exact error happen when the session times out, it is a good idea to catch errors and reinitiate sessions. That's what we do in the Home Assistant component, for instance: https://github.com/sampsyo/hass-smartthinq/blob/baed4b8bb5464de1e53f83539ffc468b99762705/climate.py#L333-L339

You can see there that we call client.refresh() in a loop, up to a maximum number of retries. Something like this is important for any long-running client program.