mirror of
https://github.com/USA-RedDragon/badnest.git
synced 2025-01-18 23:00:43 +00:00
API: Handle KeyError to fix the expiration of tokens
This commit is contained in:
parent
68e71850be
commit
77bd36b4bd
@ -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,38 +303,56 @@ class NestTemperatureSensorAPI(NestAPI):
|
|||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def get_devices(self):
|
def get_devices(self):
|
||||||
r = self._session.post(
|
try:
|
||||||
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
r = self._session.post(
|
||||||
json={
|
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
||||||
"known_bucket_types": ["buckets"],
|
json={
|
||||||
"known_bucket_versions": [],
|
"known_bucket_types": ["buckets"],
|
||||||
},
|
"known_bucket_versions": [],
|
||||||
headers={"Authorization": f"Basic {self._access_token}"},
|
},
|
||||||
)
|
headers={"Authorization": f"Basic {self._access_token}"},
|
||||||
devices = []
|
)
|
||||||
buckets = r.json()['updated_buckets'][0]['value']['buckets']
|
devices = []
|
||||||
for bucket in buckets:
|
buckets = r.json()['updated_buckets'][0]['value']['buckets']
|
||||||
if bucket.startswith('kryptonite.'):
|
for bucket in buckets:
|
||||||
devices.append(bucket.replace('kryptonite.', ''))
|
if bucket.startswith('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):
|
||||||
r = self._session.post(
|
try:
|
||||||
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
r = self._session.post(
|
||||||
json={
|
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
||||||
"known_bucket_types": ["kryptonite"],
|
json={
|
||||||
"known_bucket_versions": [],
|
"known_bucket_types": ["kryptonite"],
|
||||||
},
|
"known_bucket_versions": [],
|
||||||
headers={"Authorization": f"Basic {self._access_token}"},
|
},
|
||||||
)
|
headers={"Authorization": f"Basic {self._access_token}"},
|
||||||
|
)
|
||||||
|
|
||||||
for bucket in r.json()["updated_buckets"]:
|
for bucket in r.json()["updated_buckets"]:
|
||||||
if bucket["object_key"].startswith(
|
if bucket["object_key"].startswith(
|
||||||
f"kryptonite.{self._device_id}"):
|
f"kryptonite.{self._device_id}"):
|
||||||
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):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user