Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ devices.
## Version History

```txt
2.6.5 Try again after 4xx error
2.6.4 Handle "SYS_00004" response again (was removed in 2.6.3)
2.6.3 Differentiate response errors
2.6.2 Fix request wrapper
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='vsure',
version='2.6.4',
version='2.6.5',
description='Read and change status of verisure devices through mypages.',
long_description='A python3 module for reading and changing status of '
+ 'verisure devices through mypages.',
Expand Down
22 changes: 20 additions & 2 deletions verisure/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
'''

import json
import logging
import os
import pickle

import requests

_LOGGER = logging.getLogger(__name__)


class Error(Exception):
''' Verisure session error '''
Expand All @@ -19,6 +22,9 @@ class RequestError(Error):

class LoginError(Error):
''' Login failed '''
def __init__(self, text, status_code = None):
super().__init__(
f'Login error, status code: {status_code} - Data: {text}')


class LogoutError(Error):
Expand Down Expand Up @@ -102,20 +108,32 @@ def wrapper(url, *args, **kwargs):
try:
response = function(base_url+url, *args, **kwargs)
if response.status_code >= 500:
_LOGGER.warning(f'Error from {base_url}: {response.status_code} - {response.text}')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging a preemptive warning will only spam the logs if the second url is successful

last_exception = ResponseError(response.status_code, response.text)
self._base_urls.reverse()
continue
if response.status_code >= 400:
last_exception = LoginError(response.text)
break
_LOGGER.warning(f'Error from {base_url}: {response.status_code} - {response.text}')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging here is not adding any real value, as there will be an error anyway

if "Session has expired" in response.text:
raise LoginError(response.text, response.status_code)
elif "Username/password does not match any valid login" in response.text:
raise ResponseError(response.status_code, response.text)
Comment on lines +117 to +120
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4** indicates an authorization error. If we raise ResponseError here, we are not able to respond properly.


last_exception = LoginError(response.text, response.status_code)
self._base_urls.reverse()
continue
if response.status_code == 200:
_LOGGER.debug(f'OK from {base_url}: {response.status_code}')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging here is not adding any real value

if "SYS_00004" in response.text:
self._base_urls.reverse()
continue
return response
_LOGGER.debug(f'Unknown status from {base_url}: {response.status_code} - {response.text}')

except requests.exceptions.RequestException as ex:
_LOGGER.debug(f'Exception from {base_url}: {ex}')
last_exception = RequestError(str(ex))
_LOGGER.debug(f'Switching active base_url')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging here is not adding any real value

self._base_urls.reverse()
raise last_exception
return wrapper
Expand Down