Skip to content
This repository was archived by the owner on Mar 24, 2021. It is now read-only.

Commit 1467577

Browse files
authored
Merge pull request #1 from mattsch/bugfix/retries_and_exceptions
Bugfix/retries and exceptions
2 parents a6f0669 + 8f26ced commit 1467577

File tree

1 file changed

+18
-6
lines changed
  • custom_components/badnest

1 file changed

+18
-6
lines changed

custom_components/badnest/api.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from requests.adapters import HTTPAdapter
55
from requests.packages.urllib3.util.retry import Retry
6+
from requests.exceptions import HTTPError, RequestException
67

78
API_URL = "https://home.nest.com"
89
CAMERA_WEBAPI_BASE = "https://webapi.camera.home.nest.com"
@@ -204,6 +205,16 @@ def update_camera(self, camera):
204205
f"{API_URL}/dropcam/api/cameras/{camera}",
205206
headers={"cookie": f'user_token={self._access_token}'}
206207
)
208+
209+
if r.status_code == 403:
210+
self.login()
211+
r = self._session.get(
212+
f"{API_URL}/dropcam/api/cameras/{camera}",
213+
headers={"cookie": f'user_token={self._access_token}'}
214+
)
215+
216+
r.raise_for_status()
217+
207218
sensor_data = r.json()[0]
208219
self.device_data[camera]['name'] = \
209220
sensor_data["name"]
@@ -219,17 +230,17 @@ def update_camera(self, camera):
219230
sensor_data["location"]
220231
self.device_data[camera]['data_tier'] = \
221232
sensor_data["properties"]["streaming.data-usage-tier"]
222-
except requests.exceptions.RequestException as e:
233+
except (HTTPError, RequestException) as e:
234+
_LOGGER.error('Upstream error trying to update camera %s', camera)
223235
_LOGGER.error(e)
224-
_LOGGER.error('Failed to update, trying again')
225-
self.update_camera(camera)
226236
except KeyError:
227237
_LOGGER.debug('Failed to update, trying to log in again')
228238
self.login()
229239
self.update_camera(camera)
230240

231241
def update(self):
232242
try:
243+
self.login()
233244
# To get friendly names
234245
r = self._session.post(
235246
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
@@ -239,6 +250,7 @@ def update(self):
239250
},
240251
headers={"Authorization": f"Basic {self._access_token}"},
241252
)
253+
r.raise_for_status()
242254

243255
for bucket in r.json()["updated_buckets"]:
244256
sensor_data = bucket["value"]
@@ -257,6 +269,7 @@ def update(self):
257269
},
258270
headers={"Authorization": f"Basic {self._access_token}"},
259271
)
272+
r.raise_for_status()
260273

261274
for bucket in r.json()["updated_buckets"]:
262275
sensor_data = bucket["value"]
@@ -353,10 +366,9 @@ def update(self):
353366
sensor_data['current_temperature']
354367
self.device_data[sn]['battery_level'] = \
355368
sensor_data['battery_level']
356-
except requests.exceptions.RequestException as e:
369+
except (HTTPError, RequestException) as e:
370+
_LOGGER.error('Update failed')
357371
_LOGGER.error(e)
358-
_LOGGER.error('Failed to update, trying again')
359-
self.update()
360372
except KeyError:
361373
_LOGGER.debug('Failed to update, trying to log in again')
362374
self.login()

0 commit comments

Comments
 (0)