GadgetReactor / pyHS100

Python Library to control TPLink Switch (HS100 / HS110)
Other
409 stars 128 forks source link

HS200 weird communication error #168

Closed airfield20 closed 4 years ago

airfield20 commented 5 years ago

When I run this command pyhs100 --plug --host "192.168.1.60" sysinfo, I get his error message:

== System info ==
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/pyHS100/smartdevice.py", line 116, in _query_helper
    request=request,
  File "/usr/local/lib/python3.6/dist-packages/pyHS100/protocol.py", line 47, inquery
    sock = socket.create_connection((host, port), timeout)
  File "/usr/lib/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/usr/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
socket.timeout: timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/bin/pyhs100", line 11, in <module>
    sys.exit(cli())
  File "/usr/lib/python3/dist-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3/dist-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3/dist-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3/dist-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/usr/lib/python3/dist-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/pyHS100/cli.py", line 119, in sysinfo
    click.echo(pf(dev.sys_info))
  File "/usr/local/lib/python3.6/dist-packages/pyHS100/smartdevice.py", line 185, in sys_info
    return defaultdict(lambda: None, self.get_sysinfo())
  File "/usr/local/lib/python3.6/dist-packages/pyHS100/smartdevice.py", line 195, in get_sysinfo
    return self._query_helper("system", "get_sysinfo")
  File "/usr/local/lib/python3.6/dist-packages/pyHS100/smartdevice.py", line 119, in _query_helper
    raise SmartDeviceException('Communication error') from ex
pyHS100.smartdevice.SmartDeviceException: Communication error

For some reason if I run this short python script 2 times, the device will work properly for about 30 seconds before it starts giving me that error message again. This is the script:

from pyHS100 import Discover, TPLinkSmartHomeProtocol, SmartBulb, SmartPlug

def main():
    plug = SmartPlug("192.168.1.60")
    print(plug.get_sysinfo())

if __name__ == "__main__":
    main()

Any idea why this might be happening?

kirichkov commented 5 years ago

Very likely this is a network issue. Poor wifi signal or routing issues come to mind. If you're using a dedicated wifi network for your IoT devices, the routing between the two networks is worth a look.

airfield20 commented 5 years ago

Hmm, I do have a dynamic QoS system on my router. Could that be causing these weird results? My wifi signal is definitely strong where my machine and the hs200 device are.

Maybe it doesnt have enough time to connect. When running this command pyhs100 --plug --host "192.168.1.60" sysinfo is there a way to give it more time with each connection attempt?

rytilahti commented 5 years ago

Unfortunately there is currently no way to adjust the timeout neither programatically nor from the cli tool. See https://github.com/GadgetReactor/pyHS100/blob/master/pyHS100/protocol.py#L44 and search for timeout in that file -- the default of 5 seconds should be more than enough in a local network, though.

rytilahti commented 5 years ago

https://github.com/GadgetReactor/pyHS100/pull/172 may improve the situation, after the refactoring PR gets merged, I'll prepare a PR to make the timeout also configurable.

rytilahti commented 5 years ago

After some deliberation I'm not sure if we would convolute the constructor of SmartDevice even more, you can always adjust the timeout by changing the timeout value after creating an object (for testing), and if the timeout is really the issue, we may adjust the default and/or reconsider this.

So could you give the current git master a try to see if it works for you better now, thanks!

rytilahti commented 4 years ago

We are closing this repository for new changes in favor of a new, asyncio-enabled continuation project: https://github.com/python-kasa/python-kasa

If the issue is still present in the new project, please feel free to open an issue there, thanks!