Skip to content

Commit

Permalink
Added refresh token support and fixed infinite token expired loop
Browse files Browse the repository at this point in the history
  • Loading branch information
w1ll1am23 committed Jul 13, 2018
1 parent ccbcbe7 commit ef296e6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
44 changes: 38 additions & 6 deletions src/simplipy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, username, password, basic_auth=None):
BASIC_AUTH_HEADERS["Authorization"] = "Basic " + basic_auth
else:
BASIC_AUTH_HEADERS["Authorization"] = "Basic " + BASIC_AUTH_STRING
if self._get_token() and self._get_user_id() and self.get_subscriptions():
if self._get_token() and self.get_subscriptions():
_LOGGER.info("Setup complete")
else:
_LOGGER.error("Failed to complete setup")
Expand Down Expand Up @@ -81,7 +81,39 @@ def _get_token(self):
self.access_token = _json.get("access_token")
self.refresh_token = _json.get("refresh_token")

_LOGGER.info("Logged into SimpliSafe")
if self._get_user_id():
_LOGGER.info("Logged into SimpliSafe")
return True
return False

def _refresh_token(self):
"""
Attempt to refresh the token.
"""

refresh_data = {
'grant_type': "refresh_token",
'refresh_token': self.refresh_token
}

response = requests.post(TOKEN_URL, data=json.dumps(refresh_data),
headers=BASIC_AUTH_HEADERS)
_LOGGER.debug(response.content)
if response.status_code != 200:
_LOGGER.error("Failed to get refresh token, logging in again")
self._get_token()
return False
try:
_json = response.json()
except ValueError:
_LOGGER.error("Failed to decode JSON")
return False

self.access_token = _json.get("access_token")
self.refresh_token = _json.get("refresh_token")
self._get_user_id()

_LOGGER.info("Token was refreshed")
return True

def _get_user_id(self):
Expand All @@ -93,7 +125,7 @@ def _get_user_id(self):
_LOGGER.debug(response.content)
if response.status_code == 401:
_LOGGER.error("Token expired, getting new token")
self._get_token()
self._refresh_token()
return False
if response.status_code != 200:
_LOGGER.error("Failed to get user ID")
Expand All @@ -113,7 +145,7 @@ def get_subscriptions(self):
_LOGGER.debug(response.content)
if response.status_code == 401:
_LOGGER.error("Token expired, getting new token")
self._get_token()
self._refresh_token()
return False
if response.status_code != 200:
_LOGGER.error("Failed to get subscriptions")
Expand Down Expand Up @@ -152,7 +184,7 @@ def set_system_state(self, location_id, state):
_log_string = "Failed to set {} state to {}".format(subscription["location"]["street1"], state)
if response.status_code == 401:
_LOGGER.error("Token expired, getting new token")
self._get_token()
self._refresh_token()
return False
if response.status_code != 200:
_LOGGER.error(_log_string)
Expand Down Expand Up @@ -198,7 +230,7 @@ def _get_systems_devices_states(self, location_id, cached=True):
_LOGGER.debug(response.content)
if response.status_code == 401:
_LOGGER.error("Token expired, getting new token")
self._get_token()
self._refresh_token()
return False
if response.status_code != 200:
_LOGGER.error("Failed to pull updated sensor states")
Expand Down
18 changes: 10 additions & 8 deletions src/simplipy/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,22 @@ def update(self):
"""
Fetch all of the latest states from the API.
"""
self.api.get_subscriptions()
response = self.api.get_system_state(self.location_id)
if response:
self.state = response["alarmState"]
self.alarm_active = response["isAlarming"]
self.temperature = response["temperature"]
if self.api.get_subscriptions():
response = self.api.get_system_state(self.location_id)
if response:
self.state = response["alarmState"]
self.alarm_active = response["isAlarming"]
self.temperature = response["temperature"]
else:
_LOGGER.error("Empty system state, failed to update.")
else:
_LOGGER.error("Empty system state, failed to update.")
_LOGGER.error("Failed to get update.")

def set_state(self, state, retry=True):
"""
Set the state of the alarm system.
"""
if self.api.set_state(state):
if self.api.set_system_state(self.location_id, state):
_LOGGER.debug("Successfuly set alarm state")
self.update()
else:
Expand Down

0 comments on commit ef296e6

Please sign in to comment.