Make use of your sonoff smart switches without flashing them via the cloud APIs
Project description
sonoff-python
Make use of your sonoff smart switches without flashing them via the cloud APIs, this should work in Python 2 or Python 3.
This project is heavily inspired (read: almost entirely borrowed) by the work that Peter Buga did on a Simple Home Assistant integration for Ewelink https://github.com/peterbuga/HASS-sonoff-ewelink
I spent a day looking into various ways to work with Sonoff switches and drew a bit of a blank. There seeem to be quite a few projects that are designed to replace the Ewelink cloud platform either by flashing the Sonoff switches with new firmware, or hijacking the setup process and running a fake cloud service locally on a Raspberry Pi or similar.
I tried this approach but it didn't work for me as I was using a 4 channel switch, and it seems that most of them had only been tested with single channel switches. Also many of these were not maintained actively or had clearly been written for a specific use case.
I finally came across Peter's work written in python and it was exactly what I was looking for, a class that I could instantiate and control my switch remotely without flashing or having to use Charles to sniff my authentication code etc.
Installation
Use pip or easy_install
pip install sonoff-python
The requirements are requests and websocket-client, see requirements.txt
Configuration
Configuration is simple and basically passed to the class when you instantiate it. Username is either the email address you use to log in to Ewelink, or your phone number with the country code in front.
username - The email address or phone number you signed up with on Ewelink. Preface phone number with the country code
password - Your password to Ewelink.
api_region - The API region you use, valid ones are apparently 'us', 'eu' and 'cn'
user_apikey - The API key of authenticated user, defaults to None
bearer_token - The Bearer token of authenticated user, defaults to None
grace_period - This defaults to 600, I don't know why yet.
Usage
Here's a really simple example of how you can use this library.
import sonoff
import 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, None)
# update config
config.api_region = s.get_api_region
config.user_apikey = s.get_user_apikey
config.bearer_token = s.get_bearer_token
Support
I have tested in Python 2 and Python 3, however as we all know there may be some library weirdness.
I mainly put this together for my own use, I have learned a little about how the Sonoff kit works but for support it might be better to look at the library Peter Buga put together. I'm happy to look at any issues though.
Troubleshooting
Ewelink registration for 4 channel switches
The Sonoff switches have one of the most non-intuitive installation processes I have encountered. For registering my 4 channel switch I had to:
- Hold one of the buttons until it flashed quick, quick, slow.
- Hold a second time until it rapidly flashed in a constant pattern. I did not see the ITEAD-xx access point until it rapidly flashed.
- Once it is rapidly flashing, connect to the ITEAD-xx network.
- Choose the Compatible Pairing Mode (AP) option, then press Next. (This looks like a help page, but it is actually a fourth option (and the one you want!!)).
- Follow the onscreen instructions.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sonoff-python-0.2.1.tar.gz
.
File metadata
- Download URL: sonoff-python-0.2.1.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2901c10a92358ace3f3b0fd35e3bba84c982dcad9068cc3b4ca7b745396ae6be |
|
MD5 | c87f762c1ef14fec5ade1053c543820f |
|
BLAKE2b-256 | 27ad08bd190314790b8d5381591fcfcc223467295cd5a2bae01d81c194350bd9 |
File details
Details for the file sonoff_python-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: sonoff_python-0.2.1-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f383b2a07bf4c9e5e47bfe3df79cf91411d1110e300269a5b2174aae3b992f45 |
|
MD5 | 3bc178342d849cb251edbaaa9e96e681 |
|
BLAKE2b-256 | 5a563c4043d4afd705e70a2e7a211c82f61cca49c29460be743345ceeb9d2b3c |