lucien2k / sonoff-python

Make use of your sonoff smart switches without flashing them via the cloud APIs
MIT License
51 stars 32 forks source link

Some Issues for Python 3.6, 3.7 #10

Closed barryflower closed 4 years ago

barryflower commented 5 years ago

Hi, great initiative. WOuld be good to get it a little more robust. In running your suggested code after install using:

pip3 install sonoff-python

which is successful and then if I run the following test code:

import sonoff
import typing

class Config(typing.NamedTuple):
    username: str
    password: str
    api_region: str
    grace_period: int

config = Config('ba###@######le.com', 'S#######B', 'us', 600)

print(f"config = {config}")

s = sonoff.Sonoff(config.username, config.password, config.api_region)
devices = s.get_devices()
if devices:
    # We found a device, lets turn something on
    device_id = devices[0]['deviceid']
    s.switch('on', device_id, 0),

I get the following error:

 python3 test_sonoff.py 
Traceback (most recent call last):
  File "test_sonoff.py", line 3, in <module>
    import sonoff
  File "/usr/local/lib/python3.7/dist-packages/sonoff/__init__.py", line 3, in <module>
    from sonoff import Sonoff
ImportError: cannot import name 'Sonoff' from 'sonoff' (/usr/local/lib/python3.7/dist-packages/sonoff/__init__.py)

I have tried uninstalling the package and linking directly tothe sonoff.py file which partially works , (i.e. it can read the available devices and get their states) but I then get an error from websocket as per the following when the code tries to change the switch state:

Traceback (most recent call last):
  File "test_sonoff.py", line 22, in <module>
    s.switch('on', device_id, 0)
  File "sonoff-python-0.1/sonoff/sonoff.py", line 215, in switch
    self._ws = self._get_ws()
  File "sonoff-python-0.1/sonoff/sonoff.py", line 180, in _get_ws
    self._ws = create_connection(('wss://{}:8080/api/ws'.format(self._wshost)), timeout=10)
  File "/usr/lib/python3.6/site-packages/websocket/_core.py", line 514, in create_connection
    websock.connect(url, **options)
  File "/usr/lib/python3.6/site-packages/websocket/_core.py", line 223, in connect
    options.pop('socket', None))
  File "/usr/lib/python3.6/site-packages/websocket/_http.py", line 120, in connect
    sock = _open_socket(addrinfo_list, options.sockopt, options.timeout)
  File "/usr/lib/python3.6/site-packages/websocket/_http.py", line 161, in _open_socket
    sock = socket.socket(family, socktype, proto)
  File "/usr/lib/python3.6/socket.py", line 144, in __init__
    _socket.socket.__init__(self, family, type, proto, fileno)
OSError: [Errno 22] Invalid argument

Appreciate your help as this is a really useful tool/concept and if I can get it working will help me a lot!

Cheers Barry

Xiol commented 4 years ago

If you clone this repo, run python3 setup.py sdist && pip install dist/sonoff-python-0.1.tar.gz it'll work fine. I think the developer has fixed the issue in-repo but hasn't pushed a release up to PyPi.

barryflower commented 4 years ago

Thanks Xiol. That worked!