Skip to content

Commit

Permalink
Merge pull request #30 from WilliamRandol/patch-1
Browse files Browse the repository at this point in the history
Prevent "AttributeError" when authentication fails
  • Loading branch information
LinuxChristian authored Oct 29, 2018
2 parents f175728 + 6378ede commit 63e50b8
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pyW215/pyW215.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def state(self, value):
else:
raise TypeError("State %s is not valid." % str(value))

def get_state(self):
def get_state(self):
"""Get the device state (i.e. ON or OFF)."""
return self.state

Expand Down Expand Up @@ -332,15 +332,22 @@ def auth(self):
root = ET.fromstring(xmlData)

# Find responses
Challenge = root.find('.//{http://purenetworks.com/HNAP1/}Challenge').text
Cookie = root.find('.//{http://purenetworks.com/HNAP1/}Cookie').text
Publickey = root.find('.//{http://purenetworks.com/HNAP1/}PublicKey').text
ChallengeResponse = root.find('.//{http://purenetworks.com/HNAP1/}Challenge')
CookieResponse = root.find('.//{http://purenetworks.com/HNAP1/}Cookie')
PublickeyResponse = root.find('.//{http://purenetworks.com/HNAP1/}PublicKey')

if (Challenge == None or Cookie == None or Publickey == None) and self._error_report is False:
if (ChallengeResponse == None or CookieResponse == None or PublickeyResponse == None) and self._error_report is False:
_LOGGER.warning("Failed to receive initial authentication from smartplug.")
self._error_report = True
return None

if self._error_report is True:
return None

Challenge = ChallengeResponse.text
Cookie = CookieResponse.text
Publickey = PublickeyResponse.text

# Generate hash responses
PrivateKey = hmac.new((Publickey+self.password).encode(), (Challenge).encode()).hexdigest().upper()
login_pwd = hmac.new(PrivateKey.encode(), Challenge.encode()).hexdigest().upper()
Expand Down

0 comments on commit 63e50b8

Please sign in to comment.