GadgetReactor / pyHS100

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

Trouble Connecting to Socket #22

Closed petebrowne closed 7 years ago

petebrowne commented 7 years ago

I'm having trouble connecting to the HS100 device with pyHS100.

I'm using homeassistant, and here's the error I get when I start it up:

Traceback (most recent call last):
  File "~/.virtualenvs/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_component.py", line 150, in _async_setup_platform
    entity_platform.add_entities, discovery_info
  File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py", line 361, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup
    future.result()
  File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "~/.virtualenvs/homeassistant/lib/python3.5/site-packages/homeassistant/components/switch/tplink.py", line 41, in setup_platform
    add_devices([SmartPlugSwitch(SmartPlug(host), name)], True)
  File "~/Code/homeassistant/deps/pyHS100/pyHS100.py", line 73, in __init__
    self.initialize()
  File "~/Code/homeassistant/deps/pyHS100/pyHS100.py", line 83, in initialize
    self.sys_info = self.get_sysinfo()
  File "~/Code/homeassistant/deps/pyHS100/pyHS100.py", line 161, in get_sysinfo
    return self._query_helper("system", "get_sysinfo")
  File "~/Code/homeassistant/deps/pyHS100/pyHS100.py", line 100, in _query_helper
    request={target: {cmd: arg}}
  File "~/Code/homeassistant/deps/pyHS100/pyHS100.py", line 560, in query
    sock.shutdown(socket.SHUT_RDWR)
OSError: [Errno 57] Socket is not connected

I tried interacting directly with pyHS100, and I still cannot connect. But I get a slightly different error:

>>> from pyHS100 import *
>>> p=SmartPlug('10.0.1.33')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/Code/homeassistant/deps/pyHS100/pyHS100.py", line 73, in __init__
    self.initialize()
  File "~/Code/homeassistant/deps/pyHS100/pyHS100.py", line 83, in initialize
    self.sys_info = self.get_sysinfo()
  File "~/Code/homeassistant/deps/pyHS100/pyHS100.py", line 161, in get_sysinfo
    return self._query_helper("system", "get_sysinfo")
  File "~/Code/homeassistant/deps/pyHS100/pyHS100.py", line 100, in _query_helper
    request={target: {cmd: arg}}
  File "~/Code/homeassistant/deps/pyHS100/pyHS100.py", line 548, in query
    sock.connect((host, port))
TimeoutError: [Errno 60] Operation timed out

I found this script, and I've used it to verify I can actually connect to the device (but I guess not with a socket?):

$ hs100 10.0.1.13 9999 status
found 0 associations
found 1 connections:
     1: flags=82<CONNECTED,PREFERRED>
  outif en2
  src 10.0.1.9 port 56448
  dst 10.0.1.13 port 9999
  rank info not available
  TCP aux info available

Connection to 10.0.1.13 port 9999 [tcp/distinct] succeeded!
{"system":{"get_sysinfo":{"err_code":0,"sw_ver":"1.1.1 Build 160725 Rel.163650","hw_ver":"1.0","type":"IOT.SMARTPLUGSWITCH","model":"HS100(US)","mac":"...","deviceId":"...","hwId":"...","fwId":"...","oemId":"...","alias":"...","dev_name":"Wi-Fi Smart Plug","icon_hash":"","relay_state":0,"on_time":0,"active_mode":"none","feature":"TIM","updating":0,"rssi":-50,"led_off":0,"latitude":0,"longitude":0}}}

Any help would be appreciated!

rytilahti commented 7 years ago

Are you running on OSX? If so, #23 is probably the fix you need.