Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check login with an info call #90

Merged
merged 7 commits into from
Dec 18, 2021
Merged
Changes from 5 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
31 changes: 28 additions & 3 deletions pynetgear/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ def __init__(
self.force_login_v1 = force_login_v1
self.cookie = None
self.config_started = False
self._logging_in = False

self._info = None

def login(self):
"""
Expand All @@ -110,14 +113,22 @@ def login(self):
"""
# cookie is also used to track if at least
# one login attempt has been made for v1
if self._logging_in:
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
_LOGGER.debug("Login re-attempt within the login, ignoring.")
return False

self._logging_in = True
self.cookie = None

if not self.force_login_v1:
v2_result = self.login_v2()
if v2_result:
self._logging_in = False
return v2_result

return self.login_v1()
v1_result = self.login_v1()
self._logging_in = False
return v1_result

def login_v2(self):
_LOGGER.debug("Login v2")
Expand All @@ -143,6 +154,10 @@ def login_v2(self):
_LOGGER.debug(response.headers)
return False

# check login succes with info call
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
if self.get_info(use_cache=False) is None:
return False

return True

def login_v1(self):
Expand All @@ -161,9 +176,13 @@ def login_v1(self):

self.cookie = success

# check login succes with info call
if self.get_info(use_cache=False) is None:
return False

return success

def get_info(self):
def get_info(self, use_cache=True):
"""
Return router informations, like:
- ModelName
Expand All @@ -179,6 +198,10 @@ def get_info(self):
"""
_LOGGER.debug("Get Info")

if self._info is not None and use_cache:
_LOGGER.debug("Info from cache.")
return self._info

success, response = self._make_request(
SERVICE_DEVICE_INFO,
"GetInfo"
Expand All @@ -192,7 +215,9 @@ def get_info(self):
if not success:
return None

return {t.tag: t.text for t in node}
self._info = {t.tag: t.text for t in node}

return self._info

def get_attached_devices(self):
"""
Expand Down