Interface for Sonoff devices running original Itead firmware, in LAN mode.
Project description
Control Sonoff devices running original firmware, in LAN mode.
This module provides a way to interface with Sonoff smart home devices, such as smart switches (e.g. Sonoff Basic), plugs (e.g. Sonoff S20), and wall switches (e.g. Sonoff Touch), when these devices are in LAN Mode.
LAN Mode is a feature introduced by manufacturer Itead, to allow operation locally when their servers are unavailable. Further details can be found in the eWeLink LAN Mode guide.
Since mid 2018, the firmware Itead have shipped with most Sonoff devices has provided this feature, allowing devices to be controlled directly on the local network using a WebSocket connection on port 8081.
The feature is designed to only be used when there is no connection to the Itead cloud servers, (e.g. if your internet connection is down, or their servers are down). As such, it is only enabled when the device is connected to your WiFi network, but unable to reach the Itead servers.
Most users will only be able to use this by deliberately blocking internet access to their Sonoff devices.
Features
Discover all devices on local network (192.168.0.0/24, 192.168.1.0/24)
Discover IP address for device with known Device ID (from eWeLink app)
Read device state
Switch device ON/OFF
Listen for state changes announced by the device (e.g. by physical switch)
Activate inching/momentary device, with variable ON time (e.g. 1s)
Documentation
Documentation: https://pysonofflan.readthedocs.io.
Install
$ pip install pysonofflan
Command-Line Usage
Usage: pysonofflan [OPTIONS] COMMAND [ARGS]... A cli tool for controlling Sonoff Smart Switches/Plugs in LAN Mode. Options: --host TEXT IP address or hostname of the device to connect to. --device_id TEXT Device ID of the device to connect to. --inching TEXT Number of seconds of "on" time if this is an Inching/Momentary switch. -v, --verbosity LVL Either CRITICAL, ERROR, WARNING, INFO or DEBUG --help Show this message and exit. Commands: discover Discover devices in the network (takes ~1... listen Connect to device, print state, then print... off Turn the device off. on Turn the device on. state Connect to device and print current state.
Usage Example
$ pysonofflan discover 2019-01-31 00:45:32,074 - info: Attempting to discover Sonoff LAN Mode devices on the local network, please wait... 2019-01-31 00:46:24,007 - info: Found Sonoff LAN Mode device at IP 192.168.0.77 $ pysonofflan --host 192.168.0.77 state 2019-01-31 00:41:34,931 - info: Initialising SonoffSwitch with host 192.168.0.77 2019-01-31 00:41:35,016 - info: == Device: 10006866e9 (192.168.0.77) == 2019-01-31 00:41:35,016 - info: State: OFF $ pysonofflan --host 192.168.0.77 on 2019-01-31 00:49:40,334 - info: Initialising SonoffSwitch with host 192.168.0.77 2019-01-31 00:49:40,508 - info: 2019-01-31 00:49:40,508 - info: Initial state: 2019-01-31 00:49:40,508 - info: == Device: 10006866e9 (192.168.0.77) == 2019-01-31 00:49:40,508 - info: State: OFF 2019-01-31 00:49:40,508 - info: 2019-01-31 00:49:40,508 - info: New state: 2019-01-31 00:49:40,508 - info: == Device: 10006866e9 (192.168.0.77) == 2019-01-31 00:49:40,508 - info: State: ON
Library Usage
All common, shared functionality is available through SonoffSwitch
class:
x = SonoffSwitch("192.168.1.50")
Upon instantiating the SonoffSwitch class, a connection is initiated and device state is populated, but no further action is taken.
For most use cases, you’ll want to make use of the callback_after_update
parameter to do something with the device after a connection has been
initialised, for example:
async def print_state_callback(device): if device.basic_info is not None: print("ON" if device.is_on else "OFF") device.shutdown_event_loop() SonoffSwitch( host="192.168.1.50", callback_after_update=print_state_callback )
This example simply connects to the device, prints whether it is currently “ON” or “OFF”, then closes the connection. Note, the callback must be asynchronous.
Module-specific errors are raised as Exceptions, and are expected to be handled by the user of the library.
License
Free software: MIT license
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
History
0.3.0 (2019-05-16)
Cleaned up shutdown code
Create retry logic throughout to deal with disconnect and other failures
Added functionality to deal with state maintenance
Updated dependencies
Removed flaky test
0.2.1 (2019-02-02)
Made websocket library usage backwards-compatible with older versions
0.2.0 (2019-02-02)
Rewrote discovery to use multiple threads so it only takes 1 second
Improved logging in client class with logger passed through
Improved handling of event loop to allow usage within existing async codebase
Added –version argument to CLI
0.1.7 (2019-02-01)
Migrated from bumpversion to bump2version for cleaner release process
0.1.6 (2019-02-01)
Another bump to let Travis upload to PyPi itself, to fix the build
0.1.5 (2019-02-01)
Bumped version after tweaking build workflow
0.1.4 (2019-01-31)
Tweaked various build and testing parameters to get code coverage and distribution working
0.1.3 (2019-01-31)
Updated README layout
Added coveralls config for travis and CHANGELOG/HISTORY link
0.1.2 (2019-01-31)
Fixed documentation build for ReadTheDocs
Fixed restructuredtext in history which was breaking PyPi formatting
0.1.1 (2019-01-30)
Improved discovery logging
Added documentation
Fixed tests
0.1.0 (2019-01-27)
First release on PyPI.
Basic functional CLI client, to allow basic control (on, off, check state)
Added comprehensive logging with verbosity option to help debug new devices
Control of device is via async websocket, so should be usable in async code
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 pysonofflan-0.3.0.tar.gz
.
File metadata
- Download URL: pysonofflan-0.3.0.tar.gz
- Upload date:
- Size: 24.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2767783f2a33b9990542966ee52af2fbf3a54a8cd915de70ea6dc5b50fd5c80 |
|
MD5 | 280525ca730b7b4cf2f6bfc7bc6662f6 |
|
BLAKE2b-256 | 0973da709f74762aaf42f6ead9cf4db69b9be7455ce94e07e9f44ceb2b136cd9 |
File details
Details for the file pysonofflan-0.3.0-py2.py3-none-any.whl
.
File metadata
- Download URL: pysonofflan-0.3.0-py2.py3-none-any.whl
- Upload date:
- Size: 20.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96210d66c50cbc534149d16ad257831180a7f8b75d6edf851af68cc220668e8c |
|
MD5 | 5a405296aa28baceb32f6bfa5f93f426 |
|
BLAKE2b-256 | cf6ce98d0949b8c1204626d5ab45d4892e8ab5cd06af0124b42852b378b82a0b |