(pytuya in localtuya) vs tinytuya

Creado El diff nunca expira
119 eliminaciones
672 líneas
105 adiciones
669 líneas
# Python module to interface with Shenzhen Xenon ESP8266MOD WiFi smart devices
# TinyTuya Module
# E.g. https://wikidevi.com/wiki/Xenon_SM-PW701U
# -*- coding: utf-8 -*-
# SKYROKU SM-PW701U Wi-Fi Plug Smart Plug
"""
# Wuudi SM-S0301-US - WIFI Smart Power Socket Multi Plug with 4 AC Outlets and 4 USB Charging Works with Alexa
Python module to interface with Tuya WiFi smart devices
#

# This would not exist without the protocol reverse engineering from
Author: Jason A. Cox
# https://github.com/codetheweb/tuyapi by codetheweb and blackrozes
For more information see https://github.com/jasonacox/tinytuya
#

# Tested with Python 2.7 and Python 3.6.1 only
Classes
OutletDevice(dev_id, address, local_key=None)
CoverDevice(dev_id, address, local_key=None)
BulbDevice(dev_id, address, local_key=None)

dev_id (str): Device ID e.g. 01234567891234567890
address (str): Device Network IP Address e.g. 10.0.1.99
local_key (str, optional): The encryption key. Defaults to None.

Functions
json = status() # returns json payload
set_version(version) # 3.1 [default] or 3.3
set_dpsUsed(dpsUsed)
set_status(on, switch=1) # Set status of the device to 'on' or 'off' (bool)
set_value(index, value) # Set int value of any index.
turn_on(switch=1):
turn_off(switch=1):
set_timer(num_secs):

CoverDevice:
open_cover(switch=1):
close_cover(switch=1):
stop_cover(switch=1):


BulbDevice
set_colour(r, g, b):
set_white(brightness, colourtemp):
set_brightness(brightness):
set_colourtemp(colourtemp):
result = brightness():
result = colourtemp():
(r, g, b) = colour_rgb():
(h,s,v) = colour_hsv()
result = state():
Credits
* TuyaAPI https://github.com/codetheweb/tuyapi by codetheweb and blackrozes
For protocol reverse engineering
* PyTuya https://github.com/clach04/python-tuya by clach04
The origin of this python module (now abandoned)
* LocalTuya https://github.com/rospogrigio/localtuya-homeassistant by rospogrigio
Updated pytuya to support devices with Device IDs of 22 characters
"""


import base64
import base64
from hashlib import md5
from hashlib import md5
import json
import json
import logging
import logging
import socket
import socket
import sys
import sys
import time
import time
import colorsys
import colorsys
import binascii
import binascii


# Required module: pycryptodome
try:
try:
#raise ImportError
import Crypto
import Crypto
from Crypto.Cipher import AES # PyCrypto
from Crypto.Cipher import AES # PyCrypto
except ImportError:
except ImportError:
Crypto = AES = None
Crypto = AES = None
import pyaes # https://github.com/ricmoo/pyaes
import pyaes # https://github.com/ricmoo/pyaes



version_tuple = (7, 0, 9)
version_tuple = (1, 0, 0)
version = version_string = __version__ = '%d.%d.%d' % version_tuple
version = __version__ = '%d.%d.%d' % version_tuple
__author__ = 'rospogrigio'
__author__ = 'jasonacox'


log = logging.getLogger(__name__)
log = logging.getLogger(__name__)
logging.basicConfig() # TODO include function name/line numbers in log
#log.setLevel(level=logging.DEBUG) # Debug hack!
#log.setLevel(level=logging.DEBUG) # Uncomment to Debug


log.debug('%s version %s', __name__, version)
log.debug('%s version %s', __name__, __version__)
log.debug('Python %s on %s', sys.version, sys.platform)
log.debug('Python %s on %s', sys.version, sys.platform)
if Crypto is None:
if Crypto is None:
log.debug('Using pyaes version %r', pyaes.VERSION)
log.debug('Using pyaes version %r', pyaes.VERSION)
log.debug('Using pyaes from %r', pyaes.__file__)
log.debug('Using pyaes from %r', pyaes.__file__)
else:
else:
log.debug('Using PyCrypto %r', Crypto.version_info)
log.debug('Using PyCrypto %r', Crypto.version_info)
log.debug('Using PyCrypto from %r', Crypto.__file__)
log.debug('Using PyCrypto from %r', Crypto.__file__)


SET = 'set'
SET = 'set'
STATUS = 'status'
STATUS = 'status'


PROTOCOL_VERSION_BYTES_31 = b'3.1'
PROTOCOL_VERSION_BYTES_31 = b'3.1'
PROTOCOL_VERSION_BYTES_33 = b'3.3'
PROTOCOL_VERSION_BYTES_33 = b'3.3'


IS_PY2 = sys.version_info[0] == 2
IS_PY2 = sys.version_info[0] == 2


class AESCipher(object):
class AESCipher(object):
def __init__(self, key):
def __init__(self, key):