From 9f3f5570510d73a9299178b7d6ea3cd1e9986e1b Mon Sep 17 00:00:00 2001 From: Guy Badman <61918526+badguy99@users.noreply.github.com> Date: Sat, 9 May 2020 21:25:49 +0100 Subject: [PATCH] Fix JSONDecodeError in update procedure in api.py When the JSON error was being seen, the requests post (r = self._session.post) was getting a 502 response with the following text: '''
Please try again in 30 seconds.
''' added code to catch the exception, retry after 30 seconds if a 502 status code was return to requests. Also added extra debugging if other status codes were returned. fixes: #88 --- custom_components/badnest/api.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/custom_components/badnest/api.py b/custom_components/badnest/api.py index 6695414..3597e81 100644 --- a/custom_components/badnest/api.py +++ b/custom_components/badnest/api.py @@ -1,6 +1,8 @@ import logging - import requests +import simplejson + +from time import sleep API_URL = "https://home.nest.com" CAMERA_WEBAPI_BASE = "https://webapi.camera.home.nest.com" @@ -321,10 +323,24 @@ class NestAPI(): sensor_data["location"] self.device_data[camera]['data_tier'] = \ sensor_data["properties"]["streaming.data-usage-tier"] + + except simplejson.errors.JSONDecodeError as e: + _LOGGER.error(e) + if r.status_code != 200 and r.status_code != 502: + _LOGGER.error('Information for further debugging: ' + + 'return code {} '.format(r.status_code) + + 'and returned text {}'.format(r.text)) + + if r.status_code == 502: + _LOGGER.error('Error 502, Failed to update, retrying in 30s') + sleep(30) + self.update() + except requests.exceptions.RequestException as e: _LOGGER.error(e) _LOGGER.error('Failed to update, trying again') self.update() + except KeyError: _LOGGER.debug('Failed to update, trying to log in again') self.login()