[REQUEST] Plugin for Tuya

Python and python framework

Moderator: leecollings

tixi7
Posts: 52
Joined: Monday 10 September 2018 23:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by tixi7 »

Good to know. To turn on/off the complete plug, you can use a group in domoticz.
I never try but no one report a bug on that. Let me know if it's ok.
DAVIZINHO
Posts: 234
Joined: Sunday 27 August 2017 18:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Spain
Contact:

Re: [REQUEST] Plugin for Tuya

Post by DAVIZINHO »

tixi7 wrote: Sunday 18 November 2018 18:04 Good to know. To turn on/off the complete plug, you can use a group in domoticz.
I never try but no one report a bug on that. Let me know if it's ok.
hi,
yes the group is one method but in the tuya app there is a button for the complet plug. this not exist in the api?

thanks a lot!
tixi7
Posts: 52
Joined: Monday 10 September 2018 23:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by tixi7 »

The problem is not the api for that.
I can add another switch for all the plugs but the problem i have is how to define its state.
If some sockets are on and some others are off, that switch will be in one of the two modes and the on (or the off) button will not be available.
I can add 2 switches one for off and one for on but it will become not usable with many different devices.
Except if i miss something for that in domoticz, groups seems to be a better option.
DAVIZINHO
Posts: 234
Joined: Sunday 27 August 2017 18:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Spain
Contact:

Re: [REQUEST] Plugin for Tuya

Post by DAVIZINHO »

tixi7 wrote: Sunday 18 November 2018 20:51 The problem is not the api for that.
I can add another switch for all the plugs but the problem i have is how to define its state.
If some sockets are on and some others are off, that switch will be in one of the two modes and the on (or the off) button will not be available.
I can add 2 switches one for off and one for on but it will become not usable with many different devices.
Except if i miss something for that in domoticz, groups seems to be a better option.
ok. i understand.
The app of tuya have this button and works like this:
- If some plug are on and i push the "master button" all the plugs power offs
- If the master button is off, and i push, then all the plugs power on

I dont know if this is the best way but is the way that works the tuya app.
vrega
Posts: 15
Joined: Wednesday 13 December 2017 10:12
Target OS: Linux
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by vrega »

tixi7 wrote: Sunday 18 November 2018 0:41 Sorry for the delay, I didn't get mail notifications from the forum...

For the smart plug i put a script on github to get the dps values (get_dps.py present in the multiplug branch).
It is a simple script that get the state and returns dps values with a boolean field.

Some comments for the bug that seems to be present in multi socket and bulb:

- You have to close the tuya app before using the plugin otherwise the plugin will not be able to connect to the device. Be sure that the app is not running in background. This is mandatory since the device support only one connection at a time.
It may be the cause of the plugin crash and unfortunately domoticz as well. If it is your case and you do not get an error when the tuya app is close, please can you send me the domoticz version you use (for a bug report to domoticz)

- If you see the update in domoticz it means that the devID and dps is valid.
- If you are not able to change the state from domoticz, the first thing to do is to double check the Local Key. The local key is needed only to change the state.

Now if the problem persist:
- you can try the turnON.py and turnOFF.py. (present in the multiplug branch) you have to provide : <IP> <DevID> <Local key> <DPS value>
for smartbulb the dps value should be 1 but please check before with the get_dps.py script.
These scripts will do exactly the job but outside domoticz. You have to deactivate the device in domoticz first.

If with the script, the problem persist, it means that it is not plugin related but the library. I guess your problem is related to the issue discussed here: https://github.com/codetheweb/tuyapi/is ... -437593018

In this case, I will not be able to fix it until they found a way to deal with such devices.



Hi,
i've tried to use the turnON.py script but i've this error:
Spoiler: show
Traceback (most recent call last):
File "turnON.py", line 31, in <module>
import pytuya
File "/opt/domoticz/plugins/Domoticz-Tuya-SmartBulb-Plugin/pytuya/__init__.py", line 27, in <module>
import pyaes # https://github.com/ricmoo/pyaes
ImportError: No module named pyaes

Can you help me to solve this problem?
vrega
Posts: 15
Joined: Wednesday 13 December 2017 10:12
Target OS: Linux
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by vrega »

This is what happens in my domoticz log when i add tuya hardware:
Spoiler: show
2018-11-20 15:12:33.975 Status: (tuya) Started.
2018-11-20 15:12:34.137 (tuya) Debug logging mask set to: PYTHON PLUGIN QUEUE IMAGE DEVICE CONNECTION MESSAGE ALL
2018-11-20 15:12:34.137 (tuya) onStart called
2018-11-20 15:12:34.137 (tuya) Creating device 'Tuya SmartBulb'.
2018-11-20 15:12:34.138 (tuya) Tuya SmartBulb Device created.
2018-11-20 15:12:34.138 (tuya) Pushing 'ConnectDirective' on to queue
2018-11-20 15:12:34.139 (tuya) Processing 'ConnectDirective' message
2018-11-20 15:12:34.139 (tuya) Protocol for 'Tuya' not specified, 'None' assumed.
2018-11-20 15:12:34.139 (tuya) Transport set to: 'TCP/IP', 192.168.70.69:6668.
2018-11-20 15:12:34.139 (tuya) Connect directive received, action initiated successfully.
2018-11-20 15:12:34.253 (tuya) Pushing 'onConnectCallback' on to queue
2018-11-20 15:12:34.256 (tuya) Pushing 'DisconnectedEvent' on to queue
2018-11-20 15:12:34.295 (tuya) Processing 'onConnectCallback' message
2018-11-20 15:12:34.295 (tuya) Calling message handler 'onConnect'.
2018-11-20 15:12:34.295 (tuya) Connected successfully to: 192.168.70.69:6668
2018-11-20 15:12:34.295 (tuya) Pushing 'WriteDirective' on to queue
2018-11-20 15:12:34.295 (tuya) Processing 'DisconnectedEvent' message
2018-11-20 15:12:34.295 (tuya) Disconnect event received for '192.168.70.69:6668'.
2018-11-20 15:12:34.295 (tuya) Pushing 'onDisconnectCallback' on to queue
2018-11-20 15:12:34.295 (tuya) Processing 'WriteDirective' message
2018-11-20 15:12:34.295 (tuya) Processing 'onDisconnectCallback' message
2018-11-20 15:12:34.295 (tuya) Calling message handler 'onDisconnect'.
2018-11-20 15:12:34.295 (tuya) Disconnected from: 192.168.70.69:6668
2018-11-20 15:12:34.086 Status: (tuya) Entering work loop.
2018-11-20 15:12:34.086 Status: (tuya) Initialized version 1.0.1, author 'tixi'
2018-11-20 15:12:34.256 Error: (tuya): Async Read Exception: 104, Connection reset by peer
2018-11-20 15:12:34.295 Error: (tuya) No transport, write directive to 'Tuya' ignored.
2018-11-20 15:12:43.587 (tuya) Pushing 'onHeartbeatCallback' on to queue
2018-11-20 15:12:43.612 (tuya) Processing 'onHeartbeatCallback' message
2018-11-20 15:12:43.612 (tuya) Calling message handler 'onHeartbeat'.
2018-11-20 15:12:53.588 (tuya) Pushing 'onHeartbeatCallback' on to queue
2018-11-20 15:12:53.637 (tuya) Processing 'onHeartbeatCallback' message
2018-11-20 15:12:53.637 (tuya) Calling message handler 'onHeartbeat'.
2018-11-20 15:13:03.590 (tuya) Pushing 'onHeartbeatCallback' on to queue
2018-11-20 15:13:03.603 (tuya) Processing 'onHeartbeatCallback' message
2018-11-20 15:13:03.603 (tuya) Calling message handler 'onHeartbeat'.
2018-11-20 15:13:13.591 (tuya) Pushing 'onHeartbeatCallback' on to queue

....

2018-11-20 15:15:33.608 (tuya) Pushing 'onHeartbeatCallback' on to queue
2018-11-20 15:15:33.641 (tuya) Processing 'onHeartbeatCallback' message
2018-11-20 15:15:33.641 (tuya) Calling message handler 'onHeartbeat'.
2018-11-20 15:15:33.641 (tuya) Pushing 'ConnectDirective' on to queue
2018-11-20 15:15:33.641 (tuya) Processing 'ConnectDirective' message
2018-11-20 15:15:33.641 (tuya) Transport set to: 'TCP/IP', 192.168.70.69:6668.
2018-11-20 15:15:33.641 (tuya) Connect directive received, action initiated successfully.
2018-11-20 15:15:33.660 (tuya) Pushing 'onConnectCallback' on to queue
2018-11-20 15:15:33.692 (tuya) Processing 'onConnectCallback' message
2018-11-20 15:15:33.692 (tuya) Calling message handler 'onConnect'.
2018-11-20 15:15:33.692 (tuya) Connected successfully to: 192.168.70.69:6668
2018-11-20 15:15:33.692 (tuya) Pushing 'WriteDirective' on to queue
2018-11-20 15:15:33.692 (tuya) Processing 'WriteDirective' message
2018-11-20 15:15:33.692 (tuya) Sending 86 bytes of data
2018-11-20 15:15:33.692 (tuya) 00 00 55 aa 00 00 00 00 00 00 00 0a 00 00 00 46 7b 22 64 65 ..U�...........F{"de
2018-11-20 15:15:33.692 (tuya) 76 49 64 22 3a 22 30 35 36 37 37 32 37 30 63 63 35 30 65 33 vId":"05677270cc50e3
2018-11-20 15:15:33.692 (tuya) 30 34 33 36 36 65 22 2c 22 67 77 49 64 22 3a 22 30 35 36 37 04366e","gwId":"0567
2018-11-20 15:15:33.692 (tuya) 37 32 37 30 63 63 35 30 65 33 30 34 33 36 36 65 22 7d 00 00 7270cc50e304366e"}..
2018-11-20 15:15:33.692 (tuya) 00 00 00 00 aa 55 .. .. .. .. .. .. .. .. .. .. .. .. .. .. ....�U
2018-11-20 15:15:33.696 (tuya) Pushing 'ReadEvent' on to queue
2018-11-20 15:15:33.742 (tuya) Processing 'ReadEvent' message
2018-11-20 15:15:33.742 (tuya) Received 286 bytes of data
2018-11-20 15:15:33.742 (tuya) 00 00 55 aa 00 00 00 00 00 00 00 0a 00 00 01 0e 00 00 00 00 ..U�................
2018-11-20 15:15:33.742 (tuya) 7b 22 64 65 76 49 64 22 3a 22 30 35 36 37 37 32 37 30 63 63 {"devId":"05677270cc
2018-11-20 15:15:33.742 (tuya) 35 30 65 33 30 34 33 36 36 65 22 2c 22 64 70 73 22 3a 7b 22 50e304366e","dps":{"
2018-11-20 15:15:33.742 (tuya) 31 22 3a 66 61 6c 73 65 2c 22 32 22 3a 22 63 6f 6c 6f 75 72 1":false,"2":"colour
2018-11-20 15:15:33.742 (tuya) 22 2c 22 33 22 3a 32 35 35 2c 22 35 22 3a 22 66 66 30 30 66 ","3":255,"5":"ff00f
2018-11-20 15:15:33.742 (tuya) 62 30 31 32 64 66 66 66 66 22 2c 22 36 22 3a 22 30 30 66 66 b012dffff","6":"00ff
2018-11-20 15:15:33.742 (tuya) 30 30 30 30 30 30 30 30 30 30 22 2c 22 37 22 3a 22 66 66 66 0000000000","7":"fff
2018-11-20 15:15:33.742 (tuya) 66 35 30 30 31 30 30 66 66 30 30 22 2c 22 38 22 3a 22 66 66 f500100ff00","8":"ff
2018-11-20 15:15:33.742 (tuya) 66 66 38 30 30 33 66 66 30 30 30 30 30 30 66 66 30 30 30 30 ff8003ff000000ff0000
2018-11-20 15:15:33.742 (tuya) 30 30 66 66 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 00ff0000000000000000
2018-11-20 15:15:33.742 (tuya) 30 30 22 2c 22 39 22 3a 22 66 66 66 66 35 30 30 31 66 66 30 00","9":"ffff5001ff0
2018-11-20 15:15:33.742 (tuya) 30 30 30 22 2c 22 31 30 22 3a 22 66 66 66 66 30 35 30 35 66 000","10":"ffff0505f
2018-11-20 15:15:33.742 (tuya) 66 30 30 30 30 30 30 66 66 30 30 66 66 66 66 30 30 66 66 30 f000000ff00ffff00ff0
2018-11-20 15:15:33.742 (tuya) 30 66 66 30 30 30 30 66 66 30 30 30 30 30 30 22 7d 7d ff a1 0ff0000ff000000"}}��
2018-11-20 15:15:33.742 (tuya) dc b6 00 00 aa 55 .. .. .. .. .. .. .. .. .. .. .. .. .. .. ܶ..�U
2018-11-20 15:15:33.742 (tuya) Pushing 'onMessageCallback' on to queue
2018-11-20 15:15:33.742 (tuya) Processing 'onMessageCallback' message
2018-11-20 15:15:33.742 (tuya) Calling message handler 'onMessage'.
2018-11-20 15:15:33.742 (tuya) onMessage called: 192.168.70.69:6668 b'\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x01\x0e\x00\x00\x00\x00{"devId":"05677270cc50e304366e","dps":{"1":false,"2":"colour","3":255,"5":"ff00fb012dffff","6":"00ff0000000000","7":"ffff500100ff00","8":"ffff8003ff000000ff000000ff000000000000000000","9":"ffff5001ff0000","10":"ffff0505ff000000ff00ffff00ff00ff0000ff000000"}}\xff\xa1\xdc\xb6\x00\x00\xaaU'
2018-11-20 15:15:43.609 (tuya) Pushing 'onHeartbeatCallback' on to queue
2018-11-20 15:15:43.611 (tuya) Processing 'onHeartbeatCallback' message
2018-11-20 15:15:43.611 (tuya) Calling message handler 'onHeartbeat'.
As you can see it gives me the error:

Async Read Exception: 104, Connection reset by peer
tixi7
Posts: 52
Joined: Monday 10 September 2018 23:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by tixi7 »

For the import error, check the readme. You have to install pycrypto (section prerequisites) and then add a symbolic link to it in the plugin directory (section known issue).

For the 104, Connection reset by peer it may come from two different factors:
- tuya app open
- device not reachable (if you turn off the physical switch of your bulb)
vrega
Posts: 15
Joined: Wednesday 13 December 2017 10:12
Target OS: Linux
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by vrega »

tixi7 wrote: Tuesday 20 November 2018 20:33 For the import error, check the readme. You have to install pycrypto (section prerequisites) and then add a symbolic link to it in the plugin directory (section known issue).

For the 104, Connection reset by peer it may come from two different factors:
- tuya app open
- device not reachable (if you turn off the physical switch of your bulb)
Hi Tixi,

Regarding the import error with TurnOn.py script, i've alredy installed the pycrypto and added the symbolic link in the plugin directory (in fact the smartbulb plugin start without this error). I've copied the TurnOn.py in the same directory but when i start it,i've the error. it's very strange.

Regarding the 104 connection error, i've removed the app from my phone but the error is still present.

Domoticz version is 4.9701

Thank you.
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: [REQUEST] Plugin for Tuya

Post by freijn »

Hi,
i've tried to use the turnON.py script but i've this error:
Spoiler: show
Traceback (most recent call last):
File "turnON.py", line 31, in <module>
import pytuya
File "/opt/domoticz/plugins/Domoticz-Tuya-SmartBulb-Plugin/pytuya/__init__.py", line 27, in <module>
import pyaes # https://github.com/ricmoo/pyaes
ImportError: No module named pyaes

Can you help me to solve this problem?
Have you tried to install pyaes already ?
vrega
Posts: 15
Joined: Wednesday 13 December 2017 10:12
Target OS: Linux
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by vrega »

Hi,i managed to make it work.

Launching the get_dps.py script i've this result:

Plug State Information:
{u'devId': u'xxxxxxxxxxxxx', u'dps': {u'10': u'ffff0505ff000000ff00ffff00ff00ff0000ff000000', u'1': True, u'3': 255, u'2': u'colour', u'5': u'ff00fb012dffff', u'7': u'ffff500100ff00', u'6': u'00ff0000000000', u'9': u'ffff5001ff0000', u'8': u'ffff8003ff000000ff000000ff000000000000000000'}}

Plug DPS List:
1


Launching the script now i've the error:

Traceback (most recent call last):
File "turnON.py", line 51, in <module>
except (ConnectionResetError, socket.timeout, OSError) as e:
NameError: name 'ConnectionResetError' is not defined
tixi7
Posts: 52
Joined: Monday 10 September 2018 23:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by tixi7 »

What is your python version when you launch the script turnON.py?
vrega
Posts: 15
Joined: Wednesday 13 December 2017 10:12
Target OS: Linux
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by vrega »

if i digit:
python -V
it gives me 2.7.12
i've installed 3.5 but using the command
python -V it gives me the 2.7.12
The two versions are installed in directory /usr/local/lib

Then i've tried to launche the script with command python3 and the result is:

A problem occur please retry...

I've tried using command
python3 -V
and it gives me 3.5.2

I've Ubuntu 16.04
tixi7
Posts: 52
Joined: Monday 10 September 2018 23:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by tixi7 »

The script is for python3 because of ConnectionResetError.
When you use the script do have the plugin enable? or the tuya app?
vrega
Posts: 15
Joined: Wednesday 13 December 2017 10:12
Target OS: Linux
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by vrega »

Hi tixi,
i've no app installed on my phone and there is no plugin enabled on domoticz
jslegers
Posts: 34
Joined: Tuesday 23 February 2016 9:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: NL
Contact:

Re: [REQUEST] Plugin for Tuya

Post by jslegers »

Hi,

I have purchased this power powerplug.

https://www.aliexpress.com/item/Smart-P ... 4c4dt2kXMJ

Can the script also read out the Wattage usage ?

John
tixi7
Posts: 52
Joined: Monday 10 September 2018 23:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by tixi7 »

vrega, I do not see why you got that ConnectionResetError. Are you sure about your Local key, how did you get it?

jslegers, for sure the plugin is not define for it and i don't know if the library can get it or not. Can you send me the result of the script get_dps.py.
vrega
Posts: 15
Joined: Wednesday 13 December 2017 10:12
Target OS: Linux
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by vrega »

Hi,
I've captured traffic on My phone during the lamp registration with the app and after i've analyzed and
extracted the keys.
Is there a simplest way to re-check it?
tixi7
Posts: 52
Joined: Monday 10 September 2018 23:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by tixi7 »

https://github.com/codetheweb/tuyapi/bl ... s/SETUP.md

I use a previous version (check history if needed).
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: [REQUEST] Plugin for Tuya

Post by freijn »

vrega wrote: Friday 23 November 2018 21:24 Hi,
I've captured traffic on My phone during the lamp registration with the app and after i've analyzed and
extracted the keys.
Is there a simplest way to re-check it?
I have used this one to test. Not the simplest I am afraid :-(

https://github.com/clach04/python-tuya
vrega
Posts: 15
Joined: Wednesday 13 December 2017 10:12
Target OS: Linux
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by vrega »

tixi7 wrote: Friday 23 November 2018 21:45 https://github.com/codetheweb/tuyapi/bl ... s/SETUP.md

I use a previous version (check history if needed).
Hi Tixi,
you were right.
I've used your method to extract key and mine was wrong. Now it works. Thank you.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests