Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _request_json(self, method, *args, **kwargs):
if 'timeout' not in kwargs:
kwargs['timeout'] = self._timeout
resp = getattr(self._session, method)(*args, **kwargs)
req = args[0].replace(self._baseurl, '')
if resp.status_code == 200:
return self._resp_json(resp, req)
elif resp.status_code == 401:
raise APIRateLimited()
else:
_LOG.error('API returned %i from %s request',
resp.status_code, req)
raise APIError('Unexpected %i response from API' % (
resp.status_code))
def _resp_json(resp, req):
try:
return resp.json()
except:
# Any error doing this is probably because we didn't
# get JSON back (the API is terrible about this).
_LOG.exception('Failed to de-JSON %s response' % req)
raise APIError('Failed to process %s response', req)
pass
class ConnectionError(SomeComfortError):
pass
class AuthError(SomeComfortError):
pass
class APIError(SomeComfortError):
pass
class APIRateLimited(APIError):
def __init__(self):
super(APIRateLimited, self).__init__(
'You are being rate-limited. Try waiting a bit.')
class SessionTimedOut(SomeComfortError):
pass
def _convert_errors(fn):
def wrapper(*args, **kwargs):
try:
return fn(*args, **kwargs)
except requests.exceptions.Timeout:
raise ConnectionTimeout()
except requests.exceptions.ConnectionError:
def refresh(self):
data = self._client._get_thermostat_data(self.deviceid)
if not data['success']:
raise APIError('API reported failure to query device %s' % (
self.deviceid))
self._alive = data['deviceLive']
self._commslost = data['communicationLost']
self._data = data['latestData']
self._last_refresh = time.time()
def _set_thermostat_settings(self, thermostat_id, settings):
data = {'SystemSwitch': None,
'HeatSetpoint': None,
'CoolSetpoint': None,
'HeatNextPeriod': None,
'CoolNextPeriod': None,
'StatusHeat': None,
'DeviceID': thermostat_id,
}
data.update(settings)
url = '%s/Device/SubmitControlScreenChanges' % self._baseurl
with self._retries_login():
result = self._post_json(url, data=data)
if result.get('success') != 1:
raise APIError('API rejected thermostat settings')
def _get_hold(self, which):
try:
hold = HOLD_TYPES[self._data['uiData']['Status%s' % which]]
except KeyError:
raise APIError('Unknown hold mode %i' % (
self._data['uiData']['Status%s' % which]))
period = self._data['uiData']['%sNextPeriod' % which]
if hold == 'schedule':
return False
elif hold == 'permanent':
return True
else:
return _hold_deadline(period)