jasonacox / tuyapower

Python module to read status and energy monitoring data from Tuya based WiFi smart devices. This includes state (on/off), current (mA), voltage (V), and power (wattage).
MIT License
140 stars 22 forks source link

Tuya switch - firmware 1.4.2 #2

Closed pesor closed 4 years ago

pesor commented 4 years ago

I just tested this, and I get no data, only a rubbish string.

The plug is a Shenzhen WiFi smart plug I have a suspicion that the protocol again have changed, the firmware ver in the plug is 1.4.2. I am situated in Europe, I do not know if that is an issue.

I got the local key from tuya-cli list-app: Set your HTTP proxy IP to 192.168.1.238 with port 8001. This machine has 3 IPs; to choose a different one, use the --ip option. ✔ Response intercepted. Devices(s): [ { name: 'DEV123456789', id: 'bf20d8688f946386bd7rjo', key: '582a2be355a6ffb4' } ]

this is the output from your test.py, with additional print statements from the Crypto module. Crypto importet crypto is NOT none in if crypto AESCipher in if crypto self._pad(raw) b'{"gwId":"bf20d8688f946386bd7rj0","devId":"bf20d8688f946386bd7rj0"}\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10' crypted_text 1 %r b'\x86a\x85\x92\xeaD\xc5 ,\xa9]vLz>\x1ceY\xc6\xc2\x94\x94\xc4\x99\x0e\x8aE\xe7\xd2\xbe\x9fz]\xa1\x18!\x9b\xe8\x94Y\x0c\x9d\x9f\xac\r\xde;\xa8AN\x1b\xef\x11F\xb9]\xa7o\xb0{1^\x9c\xe4A\xb4\x0c\x1c\x90\xa7\xe4\xdd\xa0"\x83zp\x13\x1e' crypted_text 2 lenght (%d) %r 80 in if crypto AESCipher in if crypto self._pad(raw) b'{"gwId":"bf20d8688f946386bd7rj0","devId":"bf20d8688f946386bd7rj0"}\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10' crypted_text 1 %r b'\x86a\x85\x92\xeaD\xc5 ,\xa9]vLz>\x1ceY\xc6\xc2\x94\x94\xc4\x99\x0e\x8aE\xe7\xd2\xbe\x9fz]\xa1\x18!\x9b\xe8\x94Y\x0c\x9d\x9f\xac\r\xde;\xa8AN\x1b\xef\x11F\xb9]\xa7o\xb0{1^\x9c\xe4A\xb4\x0c\x1c\x90\xa7\xe4\xdd\xa0"\x83zp\x13\x1e' crypted_text 2 lenght (%d) %r 80 ERROR: Timeout polling device

TuyaPower (Tuya Power Stats)

Device bf20d8688f946386bd7rj0 at 192.168.1.98 key 582a2be355a6ffb4 protocol 3.3: Switch On: False Power (W): -99.000000 Current (mA): -99.000000 Voltage (V): -99.000000 Projected usage (kWh): Day: -2.376000 Week: -16.632000 Month: -72.072000

{ "datetime": "2020-01-12T13:00:50Z", "switch": "False", "power": "-99", "current": "-99", "voltage": "-99" }

jasonacox commented 4 years ago

It shows you are using protocol 3.3. Try forcing protocol 3.1 and see what happens:

export PLUGVERS=3.1 && python tuyatest.py bf20d8688f946386bd7rj0 192.168.1.98 582a2be355a6ffb4

pesor commented 4 years ago

I tried with your suggestion, and got the following result:

Device bf6d7bd92f9f526e6enec9 at 192.168.1.109 key 0123456789abcdef protocol 3.1: Switch On: False Power (W): -99.000000 Current (mA): -99.000000 Voltage (V): -99.000000 Projected usage (kWh): Day: -2.376000 Week: -16.632000 Month: -72.072000

{ "datetime": "2020-01-24T09:53:55Z", "switch": "False", "power": "-99", "current": "-99", "voltage": "-99" }

jasonacox commented 4 years ago

Thanks @pesor I'm not sure if this is an encryption issue or a response change for the 1.4.2 firmware. Can you try running the following commands from python directly and paste the output?

$ python
>>> import pytuya
>>> d = pytuya.OutletDevice('bf6d7bd92f9f526e6enec9', '192.168.1.109', '582a2be355a6ffb4')
>>> d.set_version(3.3)
>>> d.status()

Also, in case it isn't using protocol 3.3, try this:

$ python
>>> import pytuya
>>> d = pytuya.OutletDevice('bf6d7bd92f9f526e6enec9', '192.168.1.109', '582a2be355a6ffb4')
>>> d.status()

Sometimes d.status() will fail with a socket error so try it a few times. If it works, you should get something like this: {u'devId': u'bf6d7bd92f9f526e6enec9', u'dps': {u'1': True, u'2': 0, u'5': 59, u'4': 54, u'6': 1203}}

jasonacox commented 4 years ago

I have added a tool scan.py to this repo that will listen to your local network for Tuya compatible devices that are broadcasting their IP, Device ID and protocol Version. Give this a try. Also scan-enc.py provides a bit more information and attempts to decrypt payloads from some devices.

magostinelli commented 4 years ago

I have the same problem, with the newfirmware the port is 6668 in TCP.

jasonacox commented 4 years ago

@magostinelli do you see the device if you run a scan: python -m tuyapower?

Also I'll do some research on firmwares using 6668. What device are you using?

magostinelli commented 4 years ago

I use a docker image build from the repository, inside there is a scan.py that i run, It doesn't found anything: docker run -it tuyapower Scanning on UDP ports 6666 and 6668 for devices...

Scan Complete! Found 0 devices.

I have this devices: tekin smart plug (https://www.amazon.it/gp/product/B07CWQGP9C/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1)

kaeffin commented 4 years ago

I think i have the same problem. My Wifi-switch: https://www.amazon.de/gp/product/B07B7G1TF7/ref=ppx_yo_dt_b_asin_title_o08_s00?ie=UTF8&psc=1

Firmware: 1.0.2

First I run scan.py. everything okay. Then when I run test.py i get following output: `` TuyaPower (Tuya Power Stats)

Device 107200463c71bf3d9f13 at 192.168.1.120 key fcLzUB4zWk95xYdk protocol 3.3: Switch On: False Power (W): -99.000000 Current (mA): -99.000000 Voltage (V): -99.000000 Projected usage (kWh): Day: -2.376000 Week: -16.632000 Month: -72.072000

{ "datetime": "2020-04-13T16:45:35Z", "switch": "False", "power": "-99", "current": "-99", "voltage": "-99" }

I tried your hint with: $ python

import pytuya d = pytuya.OutletDevice('bf6d7bd92f9f526e6enec9', '192.168.1.109', '582a2be355a6ffb4') d.set_version(3.3) d.status() or $ python import pytuya d = pytuya.OutletDevice('bf6d7bd92f9f526e6enec9', '192.168.1.109', '582a2be355a6ffb4') d.status() This is the output: Traceback (most recent call last): File "", line 1, in File "/home/pi/env/lib/python3.5/site-packages/pytuya/init.py", line 281, in status data = self._send_receive(payload) File "/home/pi/env/lib/python3.5/site-packages/pytuya/init.py", line 179, in _send_receive data = s.recv(1024) socket.timeout: timed out ``

jasonacox commented 4 years ago

I think I found the problem! I just realized that your Device ID is 22 character. The pytuya library was designed to only support 20 character Device IDs which is what all of my devices have.

Since pytuya is no longer maintained, I created a forked and modified version tinytuya that incorporates 22 character support discovered by rospogrigio (https://github.com/rospogrigio/localtuya-homeassistant) and added support for that intuyapower v0.0.23.

Try updating and see if this correct the timeout issue:

pip install tinytuya
pip install --upgrade tuyapower