1
0
mirror of https://github.com/USA-RedDragon/badnest.git synced 2025-01-18 22:50:44 +00:00

API: Handle KeyError to fix the expiration of tokens

This commit is contained in:
Jacob McSwain 2019-10-23 21:29:24 -05:00
parent 68e71850be
commit 77bd36b4bd
No known key found for this signature in database
GPG Key ID: 59957AD09B94EDF5

View File

@ -126,7 +126,10 @@ class NestThermostatAPI(NestAPI):
return devices return devices
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
_LOGGER.error(e) _LOGGER.error(e)
_LOGGER.error('Failed to get devices, trying to log in again') _LOGGER.error('Failed to get devices, trying again')
return self.get_devices()
except KeyError:
_LOGGER.debug('Failed to get devices, trying to log in again')
self.login() self.login()
return self.get_devices() return self.get_devices()
@ -193,7 +196,10 @@ class NestThermostatAPI(NestAPI):
self.mode = temp_mode self.mode = temp_mode
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
_LOGGER.error(e) _LOGGER.error(e)
_LOGGER.error('Failed to update, trying to log in again') _LOGGER.error('Failed to update, trying again')
self.update()
except KeyError:
_LOGGER.debug('Failed to update, trying to log in again')
self.login() self.login()
self.update() self.update()
@ -297,6 +303,7 @@ class NestTemperatureSensorAPI(NestAPI):
self.update() self.update()
def get_devices(self): def get_devices(self):
try:
r = self._session.post( r = self._session.post(
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch", f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
json={ json={
@ -312,8 +319,17 @@ class NestTemperatureSensorAPI(NestAPI):
devices.append(bucket.replace('kryptonite.', '')) devices.append(bucket.replace('kryptonite.', ''))
return devices return devices
except requests.exceptions.RequestException as e:
_LOGGER.error(e)
_LOGGER.error('Failed to get devices, trying again')
return self.get_devices()
except KeyError:
_LOGGER.debug('Failed to get devices, trying to log in again')
self.login()
return self.get_devices()
def update(self): def update(self):
try:
r = self._session.post( r = self._session.post(
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch", f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
json={ json={
@ -329,6 +345,14 @@ class NestTemperatureSensorAPI(NestAPI):
sensor_data = bucket["value"] sensor_data = bucket["value"]
self.temperature = sensor_data["current_temperature"] self.temperature = sensor_data["current_temperature"]
self.battery_level = sensor_data["battery_level"] self.battery_level = sensor_data["battery_level"]
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()
self.update()
class NestCameraAPI(NestAPI): class NestCameraAPI(NestAPI):