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
Issue connecting to the plug (rev A1, firmware 1.25) #22
Comments
Hi Fabien, Thanks for the bug report! 298 xmlData = response.read().decode()
299 root = ET.fromstring(xmlData)
300
301 # Find responses
302 Challenge = root.find('.//{http://purenetworks.com/HNAP1/}Challenge').text
303 Cookie = root.find('.//{http://purenetworks.com/HNAP1/}Cookie').text
304 Publickey = root.find('.//{http://purenetworks.com/HNAP1/}PublicKey').text Could you add in a If you could get me the reponse when the plugs works and one where it does not work we can take it from there. |
Thank you for the quick answer, I got it to print the response, here it is when it is failing:
And when it is succeeding:
For reference I am using the following pyhton script:
And I realise that pretty much always the first time I am running the script after reconnecting the plug, it runs fine, but from the second time, it does not anymore... Let me know if I can test more! |
It seems as well that that new firmware 1.25B03 from the 20 July 17 implements:
according to the release notes... Can this have something to do with the behaviour of the plug? |
That is interesting. Might be the back off time. 10 min does however seem a long time to wait. I am guessing you can get updates more frequently than 10 min using the app? I will try to update one of my plugs and see what happens. |
Hi, I have done some more investigations and found a possible way forward! A few points:
But therefore I have to keep the pyW215 object alive in my own script:
Now the issue is going to be to retain the auth() results somewhere if we don't wont to keep the script alive indefinitely. Where do you think we can go from here ? |
Further to yesterday testing, When the plug has been disconnected from power, and then reconnected, if we try to use the old auth() the following error shows up: And with this kind of modification, there is no need to modify the Home Assistant plugin component as it keeps the object alive through the session |
Great you got it working! I would simply add a class attribute and do something similar to what you did, def __init__(self, ip, password, user = "admin",
use_legacy_protocol = False):
.....
self.authenticated = None
def SOAPAction(self, Action, responseElement, params = ""):
...
# Authenticate client
if self.authenticated is None:
self.authenticated = self.auth()
auth = self.authenticated
... I think that is a nice solution. If there was a timeout we could also add a About the re-authentication. I expect it to fail during init since that has a SOAPAction call. Then you could do something like this, def __init__(self, ip, password, user = "admin",
use_legacy_protocol = False):
.....
self.model_name = self.SOAPAction(Action="GetDeviceSettings", responseElement="ModelName", params = "")
if self.model_name is None:
self.model_name = self.reauthenticate(Action="GetDeviceSettings", responseElement="ModelName", params = "")
def reauthenticate(self, action, responseElement, params):
"""
some informative docstring
"""
_LOGGER.warning(" Attempting to re-authenticate with plug {}".format(self.ip))
self.authenticated = None
response = self.SOAPAction(action, responseElement, params)
if response is not None:
_LOGGER.warning(" Re-authenticate was a success. Now connected to {}".format(self.ip))
else:
_LOGGER.warning(" Re-authenticate failed. Please check the plug {} is turned on".format(self.ip))
return response I hacked this together in github so it may not run but you get the idea. I can do a update with this but since you have the plug to test on (and I think you should have the credit for this) can you make me a pull request for this? Then I will build the package ready for HA. |
Thanks for the answer!
I will give it a go and submit for feedback |
def SOAPAction(self, Action, responseElement, params = "", recursive = False):
......
try:
response = urlopen(Request(self.url, payload.encode(), headers))
except (HTTPError, URLError):
# Try to re-authenticate
self.authenticated = None
# Recursive call to retry action
if not recursive:
return_value = self.SOAPAction(Action, responseElement, params, True)
if recursive or return_value is None:
_LOGGER.warning("Failed to open url to {}".format(self.ip))
self._error_report = True
return None
else:
return return_value |
Submitted version bump for HASS |
Merged with HASS. |
Awesome. |
Hi,
First, thanks for creating that code!
I am trying to setup (to put in Home assistant) the plug recently bought (Australian plug) it is rev A1 and it is on firmware 1.25.
It is working fine in the dlink app, however, when running the python scripts, it throws errors 95% of the time:
Traceback (most recent call last): File "./testw215.py", line 9, in <module> sp = SmartPlug('192.168.1.243', '258798',use_legacy_protocol=True) File "/home/hcooh/hass/lib/python3.5/site-packages/pyW215/pyW215.py", line 60, in __init__ self.model_name = self.SOAPAction(Action="GetDeviceSettings", responseElement="ModelName", params = "") File "/home/hcooh/hass/lib/python3.5/site-packages/pyW215/pyW215.py", line 127, in SOAPAction auth = self.auth() File "/home/hcooh/hass/lib/python3.5/site-packages/pyW215/pyW215.py", line 302, in auth Challenge = root.find('.//{http://purenetworks.com/HNAP1/}Challenge').text AttributeError: 'NoneType' object has no attribute 'text'
it seems the plug does not return expected response...
Surprisingly, it does work from time to time (seems very random to me and not very often)
Are you aware of this issue ? Is there any possible fix?
Happy to help debug if necessary, but just not too sure what to try now.
Thanks a lot
Fabien
The text was updated successfully, but these errors were encountered: