Skip to content

Commit

Permalink
Merge 51d403c into 166f6ef
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsaxon committed Jan 9, 2020
2 parents 166f6ef + 51d403c commit 7d7a637
Show file tree
Hide file tree
Showing 32 changed files with 1,935 additions and 1,496 deletions.
10 changes: 7 additions & 3 deletions .travis.yml
@@ -1,5 +1,6 @@
language: python
python:
- 3.8
- 3.7
- 3.6
- 3.5
Expand All @@ -12,10 +13,13 @@ after_success:
deploy:
provider: pypi
distributions: sdist bdist_wheel
user: beveradb
user: "__token__"
server: https://test.pypi.org/legacy/ # Remove for deployment to official PyPi repo
password:
secure: B2GjQNy6YqcxJyjqVifLIdcSbrfWQdwxROj//4Ii6EX4bEohwo+Apdec488kJZ8CLYf6Fry1lf8wpN6OYU531Pt5BNPNG8MCAGfr5HTdtsV8ihrjr6vhC1USE9cqmnNWjpIPB7k6UaWELcsYSloeYrfgY/PQECigUtgmZt1BZHHO/3RfdaVxzO58Ye4gLV7uq7DPHOq8aExhgwO2WZyFLI21hiG03BxJ27gUKVIxz3bG+SDrbS3OCdOKCUr4471nCHCcVMAJl0g2FTAR6XqpBeUqveCfKu0dNDM7EtsloCaBZOzD2xJzJY5tTOKEZGLCCaEZlx+jMbZrI4XJyEOBNyYhXTS6lkR3xQHSAIL/Is0b+MulK0Zx1rXKdf/iijXo4dWmn89ca9s4iUu8Ks/ajzZ4LCyJzXZeKfWdB/M/4RAt6cysIWLkttkYSu1YTxF0+c85yBdlZh4b7/DfOJMzWQ6vf/AyeEwQrje+pdABeiAkk16LZZtv02jiqEXOFgmq+msDQWLf2C34z7hoQf97fRlkJ56wr0tmneFYUR30xWrRp/aZW0dqy/wREfj3xGZm6sW9PbgliXyAnWS8R1Yq7mCtA4j8MtiE40lg08WZVeG0GED9zgXXTFTx8qX3xMiwRu4TioSjAk9+5vYVkD04Vtax7uut0U1U6fY3dQSLjTE=
secure: NYNI4VfvwiF46/06n6NjEygtrddOTRxle5mSCxwSh38ITQU3x0piowSULDH6dS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rSf4NWzAEDIXGmakbS71zJxirQbzgb0mQXu1UveIhFevlEAr/z6YzZdX49QhNmlWYq+fgD+fEcXA7s2PcydDuaU7bKxH//PykF+wj8x7buZx5m2FzHTGRSTLUulg+iULYKy69X0mhcVAnYbMkSydoIAbUF90JKlyciZmfOoIVQGK66ZGc05YeJhrFVq1I9bdbbQf+5dCW1hSbgDSwWTrIN4fKbB2CnW3qSJZ/baZEZn03Gik= # PyPi
on:
tags: true
repo: beveradb/pysonofflan
repo: mattsaxon/pysonofflan
branch: V3-Firmware
python: 3.7
15 changes: 15 additions & 0 deletions .vscode/launch.json
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
14 changes: 12 additions & 2 deletions AUTHORS.rst
Expand Up @@ -5,9 +5,19 @@ Credits
Development Lead
----------------

* Andrew Beveridge <andrew@beveridge.uk>
* Matt Saxon <saxonmatt@hotmail.com>


Contributors
------------

None yet. Why not be the first?
* Gustavo Sverzut Barbieri https://github.com/barbieri : assisted with a number of broken code issues


Acknolwledgements
-----------------

* Andrew Beveridge <andrew@beveridge.uk>

This R3 fork was prompted by the excellent work reverse engineering the original firmware by Andrew
Additionally, whilst the workings of the R3 firmware is completely different, this project is structured on Andrew's work
14 changes: 7 additions & 7 deletions CONTRIBUTING.rst
Expand Up @@ -15,7 +15,7 @@ Types of Contributions
Report Bugs
~~~~~~~~~~~

Report bugs at https://github.com/beveradb/pysonofflan/issues.
Report bugs at https://github.com/mattsaxon/pysonofflan/issues.

If you are reporting a bug, please include:

Expand All @@ -38,14 +38,14 @@ and "help wanted" is open to whoever wants to implement it.
Write Documentation
~~~~~~~~~~~~~~~~~~~

pySonoffLAN could always use more documentation, whether as part of the
pySonoffLANR3 could always use more documentation, whether as part of the
official pySonoffLAN docs, in docstrings, or even on the web in blog posts,
articles, and such.

Submit Feedback
~~~~~~~~~~~~~~~

The best way to send feedback is to file an issue at https://github.com/beveradb/pysonofflan/issues.
The best way to send feedback is to file an issue at https://github.com/mattsaxon/pysonofflan/issues.

If you are proposing a feature:

Expand All @@ -57,7 +57,7 @@ If you are proposing a feature:
Get Started!
------------

Ready to contribute? Here's how to set up `pysonofflan` for local development.
Ready to contribute? Here's how to set up `pysonofflanr3` for local development.

1. Fork the `pysonofflan` repo on GitHub.
2. Clone your fork locally::
Expand All @@ -79,7 +79,7 @@ Ready to contribute? Here's how to set up `pysonofflan` for local development.
5. When you're done making changes, check that your changes pass flake8 and the
tests, including testing other Python versions with tox::

$ flake8 pysonofflan tests
$ flake8 pysonofflanr3 tests
$ python setup.py test or py.test
$ tox

Expand All @@ -102,8 +102,8 @@ Before you submit a pull request, check that it meets these guidelines:
2. If the pull request adds functionality, the docs should be updated. Put
your new functionality into a function with a docstring, and add the
feature to the list in README.rst.
3. The pull request should work for Python 2.7, 3.4, 3.5 and 3.6, and for PyPy. Check
https://travis-ci.org/beveradb/pysonofflan/pull_requests
3. The pull request should work for Python 3.6, 3.7 and 3.8. Check
https://travis-ci.org/mattsaxon/pysonofflan/pull_requests
and make sure that the tests pass for all supported Python versions.

Tips
Expand Down
64 changes: 5 additions & 59 deletions HISTORY.rst
@@ -1,65 +1,11 @@
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)
1.1.0 (2020-01-04)
------------------

* 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
* 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
Binary file added Itead/SONOFF DIY MODE Protocol Doc.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions LICENSE
@@ -1,5 +1,6 @@
MIT License

Copyright (c) 2019-2020, Matt Saxon
Copyright (c) 2019, Andrew Beveridge

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -51,7 +51,7 @@ clean-test: ## remove test and coverage artifacts
rm -fr .pytest_cache

lint: ## check style with flake8
flake8 pysonofflan tests
flake8 pysonofflanr3 tests

test: ## run tests quickly with the default Python
python setup.py test
Expand All @@ -60,7 +60,7 @@ test-all: ## run tests on every Python version with tox
tox

coverage: ## check code coverage quickly with the default Python
coverage run --source pysonofflan setup.py test
coverage run --source pysonofflanr3 setup.py test
coverage report -m
coverage html
$(BROWSER) htmlcov/index.html
Expand Down
75 changes: 39 additions & 36 deletions README.rst
@@ -1,40 +1,52 @@
===========
pySonoffLAN
===========
=============
pysonofflanr3
=============


.. image:: https://img.shields.io/pypi/v/pysonofflan.svg
:target: https://pypi.python.org/pypi/pysonofflan
.. image:: https://img.shields.io/pypi/v/pysonofflanr3.svg
:target: https://pypi.python.org/pypi/pysonofflanr3
:alt: Latest PyPi Release

.. image:: https://img.shields.io/pypi/pyversions/pysonofflan.svg?style=flat
:target: https://pypi.python.org/pypi/pysonofflan
.. image:: https://img.shields.io/pypi/pyversions/pysonofflanr3.svg?style=flat
:target: https://pypi.python.org/pypi/pysonofflanr3
:alt: Supported Python Versions

.. image:: https://img.shields.io/travis/beveradb/pysonofflan.svg
:target: https://travis-ci.org/beveradb/pysonofflan
.. image:: https://img.shields.io/travis/mattsaxon/pysonofflan.svg
:target: https://travis-ci.org/mattsaxon/pysonofflan
:alt: Build Status

.. image:: https://readthedocs.org/projects/pysonofflan/badge/?version=latest
:target: https://pysonofflan.readthedocs.io/en/latest/?badge=latest
.. image:: https://readthedocs.org/projects/pysonofflanr3/badge/?version=latest
:target: https://pysonofflanr3.readthedocs.io/
:alt: Documentation Status

.. image:: https://coveralls.io/repos/github/beveradb/pysonofflan/badge.svg?branch=master
:target: https://coveralls.io/github/beveradb/pysonofflan?branch=master
.. image:: https://coveralls.io/repos/github/mattsaxon/pysonofflan/badge.svg
:target: https://coveralls.io/github/mattsaxon/pysonofflan
:alt: Code Coverage

.. image:: https://img.shields.io/pypi/wheel/pysonofflan.svg
:target: https://pypi.org/project/pysonofflan/#files
.. image:: https://img.shields.io/pypi/wheel/pysonofflanr3.svg
:target: https://pypi.org/project/pysonofflanr3/#files
:alt: Has Wheel Package

.. image:: https://pyup.io/repos/github/beveradb/pysonofflan/shield.svg
:target: https://pyup.io/repos/github/beveradb/pysonofflan/
.. image:: https://pyup.io/repos/github/mattsaxon/pysonofflan/shield.svg
:target: https://pyup.io/repos/github/mattsaxon/pysonofflan/
:alt: Updates

.. image:: https://pyup.io/repos/github/mattsaxon/pysonofflan/python-3-shield.svg
:target: https://pyup.io/repos/github/mattsaxon/pysonofflan/
:alt: Python 3

.. image:: https://www.buymeacoffee.com/assets/img/guidelines/download-assets-sm-2.svg
:target: https://www.buymeacoffee.com/XTOsBAc
:alt: 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 <https://pypi.org/project/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.
Expand All @@ -49,20 +61,10 @@ 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)
* 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)
Expand All @@ -71,19 +73,19 @@ Features
Documentation
------------------

* Documentation: https://pysonofflan.readthedocs.io.
* Documentation: https://pysonofflanr3.readthedocs.io.

Install
------------------
::

$ pip install pysonofflan
$ pip install pysonofflanr3

Command-Line Usage
------------------
::

Usage: pysonofflan [OPTIONS] COMMAND [ARGS]...
Usage: pysonofflanr3 [OPTIONS] COMMAND [ARGS]...

A cli tool for controlling Sonoff Smart Switches/Plugs in LAN Mode.

Expand All @@ -92,12 +94,13 @@ Command-Line Usage
--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
-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 (takes ~1...
listen Connect to device, print state, then print...
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.
Expand Down
25 changes: 25 additions & 0 deletions debug/Decrypt.py
@@ -0,0 +1,25 @@
from Crypto.Hash import MD5
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
from base64 import b64decode


class SonoffR3:
def __init__(self, Apikey):

h = MD5.new()
h.update(Apikey)
self._key = h.digest()

def Decrypt(self, encoded, iv):

cipher = AES.new(self._key, AES.MODE_CBC, iv=b64decode(iv))
ciphertext = b64decode(encoded)
padded = cipher.decrypt(ciphertext)
plaintext = unpad(padded, AES.block_size)

return plaintext


device = SonoffR3(b"apikey")
print(device.Decrypt(b"ciphertext", b"iv"))

0 comments on commit 7d7a637

Please sign in to comment.