3
3
4
4
from requests .adapters import HTTPAdapter
5
5
from requests .packages .urllib3 .util .retry import Retry
6
+ from requests .exceptions import HTTPError , RequestException
6
7
7
8
API_URL = "https://home.nest.com"
8
9
CAMERA_WEBAPI_BASE = "https://webapi.camera.home.nest.com"
@@ -204,6 +205,16 @@ def update_camera(self, camera):
204
205
f"{ API_URL } /dropcam/api/cameras/{ camera } " ,
205
206
headers = {"cookie" : f'user_token={ self ._access_token } ' }
206
207
)
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
+
207
218
sensor_data = r .json ()[0 ]
208
219
self .device_data [camera ]['name' ] = \
209
220
sensor_data ["name" ]
@@ -219,17 +230,17 @@ def update_camera(self, camera):
219
230
sensor_data ["location" ]
220
231
self .device_data [camera ]['data_tier' ] = \
221
232
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 )
223
235
_LOGGER .error (e )
224
- _LOGGER .error ('Failed to update, trying again' )
225
- self .update_camera (camera )
226
236
except KeyError :
227
237
_LOGGER .debug ('Failed to update, trying to log in again' )
228
238
self .login ()
229
239
self .update_camera (camera )
230
240
231
241
def update (self ):
232
242
try :
243
+ self .login ()
233
244
# To get friendly names
234
245
r = self ._session .post (
235
246
f"{ API_URL } /api/0.1/user/{ self ._user_id } /app_launch" ,
@@ -239,6 +250,7 @@ def update(self):
239
250
},
240
251
headers = {"Authorization" : f"Basic { self ._access_token } " },
241
252
)
253
+ r .raise_for_status ()
242
254
243
255
for bucket in r .json ()["updated_buckets" ]:
244
256
sensor_data = bucket ["value" ]
@@ -257,6 +269,7 @@ def update(self):
257
269
},
258
270
headers = {"Authorization" : f"Basic { self ._access_token } " },
259
271
)
272
+ r .raise_for_status ()
260
273
261
274
for bucket in r .json ()["updated_buckets" ]:
262
275
sensor_data = bucket ["value" ]
@@ -353,10 +366,9 @@ def update(self):
353
366
sensor_data ['current_temperature' ]
354
367
self .device_data [sn ]['battery_level' ] = \
355
368
sensor_data ['battery_level' ]
356
- except requests .exceptions .RequestException as e :
369
+ except (HTTPError , RequestException ) as e :
370
+ _LOGGER .error ('Update failed' )
357
371
_LOGGER .error (e )
358
- _LOGGER .error ('Failed to update, trying again' )
359
- self .update ()
360
372
except KeyError :
361
373
_LOGGER .debug ('Failed to update, trying to log in again' )
362
374
self .login ()
0 commit comments