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

Commit

Permalink
Merge pull request #1 from mattsch/bugfix/retries_and_exceptions
Browse files Browse the repository at this point in the history
Bugfix/retries and exceptions
  • Loading branch information
mattsch authored May 23, 2020
2 parents a6f0669 + 8f26ced commit 1467577
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions custom_components/badnest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from requests.exceptions import HTTPError, RequestException

API_URL = "https://home.nest.com"
CAMERA_WEBAPI_BASE = "https://webapi.camera.home.nest.com"
Expand Down Expand Up @@ -204,6 +205,16 @@ def update_camera(self, camera):
f"{API_URL}/dropcam/api/cameras/{camera}",
headers={"cookie": f'user_token={self._access_token}'}
)

if r.status_code == 403:
self.login()
r = self._session.get(
f"{API_URL}/dropcam/api/cameras/{camera}",
headers={"cookie": f'user_token={self._access_token}'}
)

r.raise_for_status()

sensor_data = r.json()[0]
self.device_data[camera]['name'] = \
sensor_data["name"]
Expand All @@ -219,17 +230,17 @@ def update_camera(self, camera):
sensor_data["location"]
self.device_data[camera]['data_tier'] = \
sensor_data["properties"]["streaming.data-usage-tier"]
except requests.exceptions.RequestException as e:
except (HTTPError, RequestException) as e:
_LOGGER.error('Upstream error trying to update camera %s', camera)
_LOGGER.error(e)
_LOGGER.error('Failed to update, trying again')
self.update_camera(camera)
except KeyError:
_LOGGER.debug('Failed to update, trying to log in again')
self.login()
self.update_camera(camera)

def update(self):
try:
self.login()
# To get friendly names
r = self._session.post(
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
Expand All @@ -239,6 +250,7 @@ def update(self):
},
headers={"Authorization": f"Basic {self._access_token}"},
)
r.raise_for_status()

for bucket in r.json()["updated_buckets"]:
sensor_data = bucket["value"]
Expand All @@ -257,6 +269,7 @@ def update(self):
},
headers={"Authorization": f"Basic {self._access_token}"},
)
r.raise_for_status()

for bucket in r.json()["updated_buckets"]:
sensor_data = bucket["value"]
Expand Down Expand Up @@ -353,10 +366,9 @@ def update(self):
sensor_data['current_temperature']
self.device_data[sn]['battery_level'] = \
sensor_data['battery_level']
except requests.exceptions.RequestException as e:
except (HTTPError, RequestException) as e:
_LOGGER.error('Update failed')
_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()
Expand Down

0 comments on commit 1467577

Please sign in to comment.