Skip to main content

Interface for Sonoff devices running v3+ Itead firmware.

Project description

Latest PyPi Release Supported Python Versions Build Status Documentation Status Code Coverage Has Wheel Package Updates Python 3 Buy Me A Coffee

Control Sonoff devices running original firmware, in LAN mode.

To control Sonoff switches running the V3+ Itead firmware (tested on 3.0, 3.0.1, 3.1.0, 3.3.0), locally (LAN mode).

This will only work for Sonoff devices running V3+ of the stock (Itead / eWeLink) firmware. For users of V1.8.0 - V2.6.1, please use PySonoffLAN

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.

Features

  • Discover all devices on local network

  • 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

Install

$ pip install pysonofflanr3

Command-Line Usage

Usage: pysonofflanr3 [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.
  -l, --level LVL  Either CRITICAL, ERROR, WARNING, INFO or DEBUG
  --help               Show this message and exit.
  --api_key KEY        Needed for devices not in DIY mode. See https://github.com/mattsaxon/pysonofflan/wiki/Finding-the-API__Key

Commands:
  discover  Discover devices in the network
  listen    Connect to device, print state and repeat
  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

1.1.4 (2020-03-29)

1.1.3 (2020-02-16)

  • Fixed issue of reconnection that device remains unavailable until state changes

  • Fixed retry code for strip type devices

1.1.2 (deleted release)

1.1.1 (2020-02-01)

  • Optimisations to deal with later zeroconf versions which have some different behaviour

  • Improved error handling of unexpected errors

1.1.0 (2020-01-10)

  • First release on PyPI.

  • Forked from PySonoffLAN package (courtesy of Andrew Beveridge)

  • Works on V3 Itead firmware using mDNS for service discovery and REST for service invocation

  • Supports DIY mode as well as ‘standard’ mode (for standard mode API key is needed to be obtained, e.g. by sniffing LAN)

  • Supports all known devices for switching, although no sensors added at this point

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysonofflanr3-1.1.4.tar.gz (28.2 kB view details)

Uploaded Source

Built Distribution

pysonofflanr3-1.1.4-py2.py3-none-any.whl (20.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pysonofflanr3-1.1.4.tar.gz.

File metadata

  • Download URL: pysonofflanr3-1.1.4.tar.gz
  • Upload date:
  • Size: 28.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.44.0 CPython/3.7.1

File hashes

Hashes for pysonofflanr3-1.1.4.tar.gz
Algorithm Hash digest
SHA256 fd77c2e4e8dec6b3d3408eaca41b333389720d75d722f630a64e999b0ea52a4a
MD5 51303f9123055a283cb52014bc2b021a
BLAKE2b-256 e0b6019eba10a301db90c3cb84cd696957ac12fcca38941a6e4576c4aa029888

See more details on using hashes here.

File details

Details for the file pysonofflanr3-1.1.4-py2.py3-none-any.whl.

File metadata

  • Download URL: pysonofflanr3-1.1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 20.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.44.0 CPython/3.7.1

File hashes

Hashes for pysonofflanr3-1.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fa8c4a2078de97266d15932c60e3c1e1b8b7032cc88ea1beef8d9b5ed1d75a72
MD5 3c823ba5cf68634f759422b0b6dcf759
BLAKE2b-256 3b5b9b5ae14700154a828d55c2fbb8906db7315f0ab8f8de8d9bce40cc7a0ea9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page