[REQUEST] Plugin for Tuya

Python and python framework

Moderator: leecollings

merlot
Posts: 33
Joined: Saturday 13 April 2019 16:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by merlot »

For your referencie, activating the DEBUG (uncommenting the line and commentig de loggin ERROR level one in the ___init__.py) this is what I get from get_dps.py for a dual socket with version 3.3 of plug protocol:
Spoiler: show
INFO:pytuya:pytuya version 7.0.4
INFO:pytuya:Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
INFO:pytuya:Using PyCrypto (2, 6, 1, 'final', 0)
INFO:pytuya:Using PyCrypto from '/home/pi/.local/lib/python3.5/site-packages/Crypto/__init__.py'
DEBUG:pytuya:status() entry
DEBUG:pytuya:json_payload=b'{"devId":"836742153c71bf3xxxxx","gwId":"836742153c71bf3xxxxx"}'
DEBUG:pytuya:socket.error=<class 'OSError'>
DEBUG:pytuya:address='192.168.1.66'
DEBUG:pytuya:payload_sent=b'\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00H\x8d\xeb\x00\x98\x07X~\xe8H\xb9\rx\xfeu\xa9\'\xdb\xa1\x89\xde\x01\xcb9\x89\x1c"\xa9\x82N\xf3}\x81,+z\xf3\xec\xca\xffj\x1a\x96Z\xeeu\xc2\xf9f=\xf2V\x86#\xc5m\xd3-\x98MU\x84\x90{\x8e\xc7M\x19)\x00\x00\xaaU'
DEBUG:pytuya:data_rcv:b'\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\xac\x00\x00\x00\x00\x8d\xeb\x00\x98\x07X~\xe8H\xb9\rx\xfeu\xa9\'\xdb\xa1\x89\xde\x01\xcb9\x89\x1c"\xa9\x82N\xf3}\x81O\x1f\x9c\'\x97P\x16\xf4\xc6\x8cH\x9a4\x9d\xd0hW\x1d\x8f\x1c\xc4\xc7\xd6\xfe\xc0\x9a\xaa)\xf0\xf0l\xa2N\xd2\xe4\xf0\x0c\xc7\xe9\xc2\xd1\x87\x93\xf9\x9d\x02\x19\xd9}\xdf\x9eX\xbb\x9f:~\xe5\xd8\x08 \'\x0bq\xb8\x17R\x1b\xfa\x83`\xed`\x05\x80\xa2\xa8C\xf7kzT\xd9\xf7\xc6Z~\xe3%\xac\x0e\n\xeaU\xc7+\xb9q\xd9\xf1#\x0b\xb2\x01.\xa6\xb4\x83\xe6\x0f\xe0\x1e\x93\xe0\x04\xabcG\x01\xc2\t\r\xac\xb1&\xe6\xc8\xb8fakr\x16\x00\x00\xaaU'
DEBUG:pytuya:close
DEBUG:pytuya:status received data=b'\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\xac\x00\x00\x00\x00\x8d\xeb\x00\x98\x07X~\xe8H\xb9\rx\xfeu\xa9\'\xdb\xa1\x89\xde\x01\xcb9\x89\x1c"\xa9\x82N\xf3}\x81O\x1f\x9c\'\x97P\x16\xf4\xc6\x8cH\x9a4\x9d\xd0hW\x1d\x8f\x1c\xc4\xc7\xd6\xfe\xc0\x9a\xaa)\xf0\xf0l\xa2N\xd2\xe4\xf0\x0c\xc7\xe9\xc2\xd1\x87\x93\xf9\x9d\x02\x19\xd9}\xdf\x9eX\xbb\x9f:~\xe5\xd8\x08 \'\x0bq\xb8\x17R\x1b\xfa\x83`\xed`\x05\x80\xa2\xa8C\xf7kzT\xd9\xf7\xc6Z~\xe3%\xac\x0e\n\xeaU\xc7+\xb9q\xd9\xf1#\x0b\xb2\x01.\xa6\xb4\x83\xe6\x0f\xe0\x1e\x93\xe0\x04\xabcG\x01\xc2\t\r\xac\xb1&\xe6\xc8\xb8fakr\x16\x00\x00\xaaU'
DEBUG:pytuya:result=b'\x8d\xeb\x00\x98\x07X~\xe8H\xb9\rx\xfeu\xa9\'\xdb\xa1\x89\xde\x01\xcb9\x89\x1c"\xa9\x82N\xf3}\x81O\x1f\x9c\'\x97P\x16\xf4\xc6\x8cH\x9a4\x9d\xd0hW\x1d\x8f\x1c\xc4\xc7\xd6\xfe\xc0\x9a\xaa)\xf0\xf0l\xa2N\xd2\xe4\xf0\x0c\xc7\xe9\xc2\xd1\x87\x93\xf9\x9d\x02\x19\xd9}\xdf\x9eX\xbb\x9f:~\xe5\xd8\x08 \'\x0bq\xb8\x17R\x1b\xfa\x83`\xed`\x05\x80\xa2\xa8C\xf7kzT\xd9\xf7\xc6Z~\xe3%\xac\x0e\n\xeaU\xc7+\xb9q\xd9\xf1#\x0b\xb2\x01.\xa6\xb4\x83\xe6\x0f\xe0\x1e\x93\xe0\x04\xabcG\x01\xc2\t\r\xac\xb1&\xe6\xc8\xb8f'
DEBUG:pytuya:decrypted result='{"devId":"836742153c71bf3xxxxx","dps":{"1":false,"2":false,"9":0,"10":0,"18":0,"19":0,"20":2335,"21":1,"22":602,"23":28250,"24":16000,"25":1300}}'

Plug State Information:
{'devId': '836742153c71bf3xxxxx', 'dps': {'22': 602, '18': 0, '10': 0, '20': 2335, '23': 28250, '19': 0, '24': 16000, '2': False, '9': 0, '25': 1300, '1': False, '21': 1}}

Plug DPS List:
2;1
Loky31
Posts: 40
Joined: Monday 11 March 2019 13:46
Target OS: -
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by Loky31 »

Many thanks for the reply!!

Just a detail, to activate protocol V3.3 do I only need to edit the tuyapi/index.js file (by replacing 3.1 by 3.3) ?

is that enough?
Loky31
Posts: 40
Joined: Monday 11 March 2019 13:46
Target OS: -
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by Loky31 »

I've tried to update the index.js with 3.3 instead of 3.1 but din't change anything...

I still have an OSError when launching the get_dps.py

I don't know where I'm wrong then....
merlot
Posts: 33
Joined: Saturday 13 April 2019 16:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by merlot »

Loky31 wrote:Many thanks for the reply!!

Just a detail, to activate protocol V3.3 do I only need to edit the tuyapi/index.js file (by replacing 3.1 by 3.3) ?

is that enough?
As I've mentioned before

"Then you can add device.set_version(3.3) in the get_dps.py after creating the device and the get_dps.py should work"

But you need pytuya 7.0.4

Enviado desde mi Redmi Note 4 mediante Tapatalk

Loky31
Posts: 40
Joined: Monday 11 March 2019 13:46
Target OS: -
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by Loky31 »

Hello,
Thanks for the help!

So here is the situation :
I have modified the __init__.py in the pytuya directory to change 3.1 to 3.3 in the " class XenonDevice(object): " which results in "self.version = 3.3".

The get_dps.py was still crashing as follow:

Code: Select all

Traceback (most recent call last):
  File "./get_dps.py", line 46, in <module>
    data = device.status()
  File "/home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/pytuya/__init__.py", line 279, in status
    payload = self.generate_payload('status')
  File "/home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/pytuya/__init__.py", line 219, in generate_payload
    json_payload = self.cipher.encrypt(json_payload, False)
  File "/home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/pytuya/__init__.py", line 64, in encrypt
    cipher = AES.new(self.key, mode=AES.MODE_ECB)
  File "/usr/lib/python3/dist-packages/Crypto/Cipher/AES.py", line 94, in new
    return AESCipher(key, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/Crypto/Cipher/AES.py", line 59, in __init__
    blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/Crypto/Cipher/blockalgo.py", line 141, in __init__
    self._cipher = factory.new(key, *args, **kwargs)
ValueError: Key cannot be the null string
So I hard coded the local key in :

Code: Select all

class Device(XenonDevice):
    def __init__(self, dev_id, address, local_key=None, dev_type=None):
        super(Device, self).__init__(dev_id, address, 'xxxxxxxxxxxxx', dev_type)
Which succed to give me the DPS of both my plugs and the one of the curtain switch.
Socket : 'dps': {'1': False, '2': 0}
Curtain switch : 'dps': {'1': '3'}

I put it back again to "super(Device, self).__init__(dev_id, address, local_key, dev_type)" and my socket are more or les working on domoticz :
The switches created by the pluggin does not update to reflect socket state, so I created a dummy switch to pilot it.
All mesures are out of order (but I don't really care about that).

So I'm pretty happy to be able to pilot the plug even in degraded situation...

Still not able to deal with the curtain switch as I don't knoàw how to handle the " 'dps': {'1': '3'} " through the pluggin ( if it works) or if some modifications are needed...

Any further idea on all this epic tuya story? :)
Loky31
Posts: 40
Joined: Monday 11 March 2019 13:46
Target OS: -
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by Loky31 »

I finally handled the switch to work :)

But still the pluggin does not handle well status (the domoticz switch is stuck on OFF state)...

Anyway, it works with a dummy that activate the switch created by the pluggin. Fair enough to be functionnal :)
xavierlp
Posts: 70
Joined: Tuesday 01 November 2016 16:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: France
Contact:

Re: [REQUEST] Plugin for Tuya

Post by xavierlp »

Hello to all and thanks again tixi for the plugin

I could setup the plggin with the guide to catch device id and key

I've got some error in the log and when i run get_dps.py the message "A problem occur please retry..."

In the plugin setting I changes the value of debug to 1 and can read this :
2019-09-20 16:40:24.186 Error: (Tuya) 'onMessage' failed 'KeyError'.
2019-09-20 16:40:24.186 Error: (Tuya) ----> Line 534 in '/home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/plugin.py', function onMessage
2019-09-20 16:40:24.186 Error: (Tuya) ----> Line 442 in '/home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/plugin.py', function onMessage

Is it an issue with python, script ?

Thanks for your help

More details




2019-09-20 16:40:24.031 (Tuya) Debug logging mask set to: PYTHON PLUGIN QUEUE IMAGE DEVICE CONNECTION MESSAGE ALL
2019-09-20 16:40:24.031 (Tuya) onStart called
2019-09-20 16:40:24.031 (Tuya) Pushing 'ConnectDirective' on to queue
2019-09-20 16:40:24.031 (Tuya) Processing 'ConnectDirective' message
2019-09-20 16:40:24.031 (Tuya) Protocol for 'Tuya' not specified, 'None' assumed.
2019-09-20 16:40:24.031 (Tuya) Transport set to: 'TCP/IP', 192.168.1.23:6668.
2019-09-20 16:40:24.032 (Tuya) Connect directive received, action initiated successfully.
2019-09-20 16:40:24.116 (Tuya) Pushing 'onConnectCallback' on to queue
2019-09-20 16:40:24.132 (Tuya) Processing 'onConnectCallback' message
2019-09-20 16:40:24.132 (Tuya) Calling message handler 'onConnect'.
2019-09-20 16:40:24.132 (Tuya) Connected successfully to: 192.168.1.23:6668
2019-09-20 16:40:24.133 (Tuya) Pushing 'WriteDirective' on to queue
2019-09-20 16:40:24.134 (Tuya) Processing 'WriteDirective' message
2019-09-20 16:40:24.134 (Tuya) Sending 86 bytes of data
2019-09-20 16:40:24.134 (Tuya) 00 00 55 aa 00 00 00 00 00 00 00 0a 00 00 00 46 7b 22 64 65 ..U�...........F{"de
2019-09-20 16:40:24.134 (Tuya) 76 49 64 22 3a 22 35 35 31 34 32 37 30 37 62 63 64 64 63 32 vId":"55142707bcddc2
2019-09-20 16:40:24.134 (Tuya) 39 35 66 39 36 61 22 2c 22 67 77 49 64 22 3a 22 35 35 31 34 95f96a","gwId":"5514
2019-09-20 16:40:24.134 (Tuya) 32 37 30 37 62 63 64 64 63 32 39 35 66 39 36 61 22 7d 49 51 2707bcddc295f96a"}IQ
2019-09-20 16:40:24.134 (Tuya) d9 16 00 00 aa 55 .. .. .. .. .. .. .. .. .. .. .. .. .. .. �...�U
2019-09-20 16:40:24.145 (Tuya) Pushing 'ReadEvent' on to queue
2019-09-20 16:40:24.184 (Tuya) Processing 'ReadEvent' message
2019-09-20 16:40:24.184 (Tuya) Received 173 bytes of data
2019-09-20 16:40:24.184 (Tuya) 00 00 55 aa 00 00 00 00 00 00 00 0a 00 00 00 9d 00 00 00 00 ..U�...........�....
2019-09-20 16:40:24.184 (Tuya) 7b 22 64 65 76 49 64 22 3a 22 35 35 31 34 32 37 30 37 62 63 {"devId":"55142707bc
2019-09-20 16:40:24.185 (Tuya) 64 64 63 32 39 35 66 39 36 61 22 2c 22 64 70 73 22 3a 7b 22 ddc295f96a","dps":{"
2019-09-20 16:40:24.185 (Tuya) 31 22 3a 66 61 6c 73 65 2c 22 32 22 3a 66 61 6c 73 65 2c 22 1":false,"2":false,"
2019-09-20 16:40:24.185 (Tuya) 39 22 3a 30 2c 22 31 30 22 3a 30 2c 22 31 38 22 3a 30 2c 22 9":0,"10":0,"18":0,"
2019-09-20 16:40:24.185 (Tuya) 31 39 22 3a 30 2c 22 32 30 22 3a 32 32 38 32 2c 22 32 31 22 19":0,"20":2282,"21"
2019-09-20 16:40:24.185 (Tuya) 3a 31 2c 22 32 32 22 3a 36 30 39 2c 22 32 33 22 3a 32 37 32 :1,"22":609,"23":272
2019-09-20 16:40:24.185 (Tuya) 39 37 2c 22 32 34 22 3a 31 35 35 31 39 2c 22 32 35 22 3a 31 97,"24":15519,"25":1
2019-09-20 16:40:24.185 (Tuya) 33 34 30 7d 7d 53 83 91 98 00 00 aa 55 .. .. .. .. .. .. .. 340}}S���..�U
2019-09-20 16:40:24.185 (Tuya) Pushing 'onMessageCallback' on to queue
2019-09-20 16:40:24.185 (Tuya) Processing 'onMessageCallback' message
2019-09-20 16:40:24.185 (Tuya) Calling message handler 'onMessage'.
2019-09-20 16:40:24.185 (Tuya) onMessage called: 192.168.1.23:6668 b'\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x9d\x00\x00\x00\x00{"devId":"55142707bcddc295f96a","dps":{"1":false,"2":false,"9":0,"10":0,"18":0,"19":0,"20":2282,"21":1,"22":609,"23":27297,"24":15519,"25":1340}}S\x83\x91\x98\x00\x00\xaaU'
2019-09-20 16:40:24.024 Status: (Tuya) Entering work loop.
2019-09-20 16:40:24.025 Status: (Tuya) Initialized version 1.0.0, author 'tixi_sincze'
2019-09-20 16:40:24.186 Error: (Tuya) 'onMessage' failed 'KeyError'.
2019-09-20 16:40:24.186 Error: (Tuya) ----> Line 534 in '/home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/plugin.py', function onMessage
2019-09-20 16:40:24.186 Error: (Tuya) ----> Line 442 in '/home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/plugin.py', function onMessage
xavierlp
Posts: 70
Joined: Tuesday 01 November 2016 16:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: France
Contact:

Re: [REQUEST] Plugin for Tuya

Post by xavierlp »

More information with DEBUG and get_dps

pi@raspberrypi:~/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin $ sudo python3.5 get_dps.py '192.168.1.23' '55142707bcddc295f96a'
INFO:pytuya:pytuya version 7.0.4
INFO:pytuya:Python 3.5.7 (default, Aug 17 2019, 22:31:31)
[GCC 4.9.2] on linux
INFO:pytuya:Using PyCrypto (2, 6, 1, 'final', 0)
INFO:pytuya:Using PyCrypto from '/usr/local/lib/python3.5/site-packages/Crypto/__init__.py'
DEBUG:pytuya:status() entry
DEBUG:pytuya:json_payload=b'{"gwId":"55142707bcddc295f96a","devId":"55142707bcddc295f96a"}'
A problem occur please retry...
deennoo
Posts: 784
Joined: Wednesday 10 December 2014 13:06
Target OS: Linux
Domoticz version: beta
Location: Bordeaux France
Contact:

Re: [REQUEST] Plugin for Tuya

Post by deennoo »

@xavierlp

Which kind of device did you use ? Action One ?

I get a Smart-Plug and a Led Strip from Action and they use v3.3 tuya version (MCUFW 1.1.5 for the Plug and 1.1.6 for the Led Strip)


@merlot
Did you think that it can e possible to choose which version to use on the plugin ? (3.1, 3.2, 3.3)
Can you be more explicit about how to choose version 3.3 using Pytuya 7.0.4 ?
Domoticz stable 3.5877 for real & Domoticz beta for test
Rfxtrxe / RFLink / Milight / Yeelight / Tasmota / MQTT / BLE / Zigate
http://domo-attitude.fr
xavierlp
Posts: 70
Joined: Tuesday 01 November 2016 16:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: France
Contact:

Re: [REQUEST] Plugin for Tuya

Post by xavierlp »

Hello @deennoo

I am using OxaOxe
https://www.amazon.fr/gp/product/B07G2J ... le_o02_s00

I could red your post on github. For my side I don't know how to check the version of tuya.

thanks
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Re: [REQUEST] Plugin for Tuya

Post by FearNaBoinne »

Adding Set_Version(3.3) to the get_dps, turnOn and turnOFF scripts made it work*

*) Also had to modify the device generator to contain the key, and add a 3rd argument (key) to get_dps...

My get_dps.py is now:

Code: Select all

#!/usr/bin/python3

########################################################################################
#       Domoticz Tuya Smart Plug Python Plugin                                             #
#                                                                                      #
#       MIT License                                                                        #
#                                                                                      #
#       Copyright (c) 2018 tixi                                                            #
#                                                                                      #
#       Permission is hereby granted, free of charge, to any person obtaining a copy       #
#       of this software and associated documentation files (the "Software"), to deal      #
#       in the Software without restriction, including without limitation the rights       #
#       to use, copy, modify, merge, publish, distribute, sublicense, and/or sell          #
#       copies of the Software, and to permit persons to whom the Software is              #
#       furnished to do so, subject to the following conditions:                           #
#                                                                                      #
#       The above copyright notice and this permission notice shall be included in all     #
#       copies or substantial portions of the Software.                                    #
#                                                                                      #
#       THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR         #
#       IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,           #
#       FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE        #
#       AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER             #
#       LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,      #
#       OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE      #
#       SOFTWARE.                                                                          #
#                                                                                      #
########################################################################################

import sys
import pytuya
import socket #needed for socket.timeout exception


if(len(sys.argv)!=4):
        print("usage: " + sys.argv[0] + " <IP> <DevID> <DevKey>")
        exit(1)

ip       = sys.argv[1]
devid    = sys.argv[2]
devkey   = sys.argv[3]

device   = pytuya.OutletDevice(devid,ip,devkey)

data = 0 #stub for the try except
try:
        device.set_version(3.3)
        data = device.status()
except (ConnectionResetError, socket.timeout, OSError)  as e:
        print("A problem occur please retry...")
        exit(1)

print("\nPlug State Information:")
print(data)

print("\nPlug DPS List:")

dps_list = ""
first=True
for key in data['dps'].keys():

        if(type (data['dps'][key]) is bool):
                if(not first):
                        dps_list += ";"
                dps_list += str(int(key))
                first=False

print(dps_list)
My other 2 scripts:

turnON.py

Code: Select all

#!/usr/bin/python3

########################################################################################
#       Domoticz Tuya Smart Plug Python Plugin                                             #
#                                                                                      #
#       MIT License                                                                        #
#                                                                                      #
#       Copyright (c) 2018 tixi                                                            #
#                                                                                      #
#       Permission is hereby granted, free of charge, to any person obtaining a copy       #
#       of this software and associated documentation files (the "Software"), to deal      #
#       in the Software without restriction, including without limitation the rights       #
#       to use, copy, modify, merge, publish, distribute, sublicense, and/or sell          #
#       copies of the Software, and to permit persons to whom the Software is              #
#       furnished to do so, subject to the following conditions:                           #
#                                                                                      #
#       The above copyright notice and this permission notice shall be included in all     #
#       copies or substantial portions of the Software.                                    #
#                                                                                      #
#       THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR         #
#       IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,           #
#       FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE        #
#       AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER             #
#       LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,      #
#       OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE      #
#       SOFTWARE.                                                                          #
#                                                                                      #
########################################################################################

import sys
import pytuya
import socket #needed for socket.timeout exception


if(len(sys.argv)!=5):
        print("usage: " + sys.argv[0] + " <IP> <DevID> <Local key> <DPS value>")
        exit(1)

ip        = sys.argv[1]
devid     = sys.argv[2]
localkey  = sys.argv[3]
dps_value = sys.argv[4]

device    = pytuya.OutletDevice(devid,ip,localkey)
device.set_version(3.3)

try:

        payload = device.generate_payload('set', {str(dps_value):True})
        device._send_receive(payload)

except (ConnectionResetError, socket.timeout, OSError)  as e:
        print("A problem occur please retry...")
        exit(1)
turnOFF.py

Code: Select all

#!/usr/bin/python3

########################################################################################
#       Domoticz Tuya Smart Plug Python Plugin                                             #
#                                                                                      #
#       MIT License                                                                        #
#                                                                                      #
#       Copyright (c) 2018 tixi                                                            #
#                                                                                      #
#       Permission is hereby granted, free of charge, to any person obtaining a copy       #
#       of this software and associated documentation files (the "Software"), to deal      #
#       in the Software without restriction, including without limitation the rights       #
#       to use, copy, modify, merge, publish, distribute, sublicense, and/or sell          #
#       copies of the Software, and to permit persons to whom the Software is              #
#       furnished to do so, subject to the following conditions:                           #
#                                                                                      #
#       The above copyright notice and this permission notice shall be included in all     #
#       copies or substantial portions of the Software.                                    #
#                                                                                      #
#       THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR         #
#       IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,           #
#       FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE        #
#       AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER             #
#       LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,      #
#       OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE      #
#       SOFTWARE.                                                                          #
#                                                                                      #
########################################################################################

import sys
import pytuya
import socket #needed for socket.timeout exception


if(len(sys.argv)!=5):
        print("usage: " + sys.argv[0] + " <IP> <DevID> <Local key> <DPS value>")
        exit(1)

ip        = sys.argv[1]
devid     = sys.argv[2]
localkey  = sys.argv[3]
dps_value = sys.argv[4]

device    = pytuya.OutletDevice(devid,ip,localkey)
device.set_version(3.3)

try:

        payload = device.generate_payload('set', {str(dps_value):False})
        device._send_receive(payload)

except (ConnectionResetError, socket.timeout, OSError)  as e:
        print("A problem occur please retry...")
        exit(1)
Now to get the plugin working... ;)
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Re: [REQUEST] Plugin for Tuya

Post by FearNaBoinne »

xavierlp wrote: Friday 20 September 2019 16:42 In the plugin setting I changes the value of debug to 1 and can read this :
2019-09-20 16:40:24.186 Error: (Tuya) 'onMessage' failed 'KeyError'.
2019-09-20 16:40:24.186 Error: (Tuya) ----> Line 534 in '/home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/plugin.py', function onMessage
2019-09-20 16:40:24.186 Error: (Tuya) ----> Line 442 in '/home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/plugin.py', function onMessage
It's hard to say without having the actual code as you're running it in your environment, but 'KeyError' often refers to missing dictionary items. For instance, the Parameters dictionary only has 6 entries (Mode1 - Mode6), trying to use Mode7 will not work, because it doesn't exist and referring to it (ie Parameters["Mode7"]) would generate a key error.
You'd have to go in to plugin.py and look at line 534 to see what is happening there...
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
xavierlp
Posts: 70
Joined: Tuesday 01 November 2016 16:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: France
Contact:

Re: [REQUEST] Plugin for Tuya

Post by xavierlp »

Hello FearNaBoine

The line 534 is
_plugin.onMessage(Connection, Data)
from the
def onMessage(Connection, Data):
global _plugin
_plugin.onMessage(Connection, Data)

So nothning very clear.

so I read your previous message and I updated the get_dps.py

I've got now this message ValueError: Input strings must be a multiple of 16 in length


sudo python3.5 get_dps.py 192.168.1.23 55142707bcddc295f96a c6e4fd6a1f14fe20
INFO:pytuya:pytuya version 7.0.4
INFO:pytuya:Python 3.5.7 (default, Aug 17 2019, 22:31:31)
[GCC 4.9.2] on linux
INFO:pytuya:Using PyCrypto (2, 6, 1, 'final', 0)
INFO:pytuya:Using PyCrypto from '/usr/local/lib/python3.5/site-packages/Crypto/__init__.py'
DEBUG:pytuya:status() entry
DEBUG:pytuya:json_payload=b'{"devId":"55142707bcddc295f96a","gwId":"55142707bcddc295f96a"}'
DEBUG:pytuya:status received data=b'\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x1d\x00\x00\x00\x01data format errorM\xdf\x82\xac\x00\x00\xaaU'
DEBUG:pytuya:result=b'data format error'
Traceback (most recent call last):
File "get_dps.py", line 48, in <module>
data = device.status()
File "/home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/pytuya/__init__.py", line 308, in status
result = cipher.decrypt(result, False)
File "/home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/pytuya/__init__.py", line 87, in decrypt
raw = cipher.decrypt(enc)
File "/usr/local/lib/python3.5/site-packages/Crypto/Cipher/blockalgo.py", line 295, in decrypt
return self._cipher.decrypt(ciphertext)
ValueError: Input strings must be a multiple of 16 in length

SO I removed the plugin from smartdevice app on my android and rerun the process but I have the same key and same error message.

Any ideas ?

thanks a lot

Xavier
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Re: [REQUEST] Plugin for Tuya

Post by FearNaBoinne »

What about line 442?

When I run the script I do not get debug output, so I may be doing something wrong there, but I *do* get useful response data:"

Code: Select all

Plug State Information:
{'devId': '70116356840d8e5c3382', 'dps': {'1': False, '9': 0}}

Plug DPS List:
1
(Of course, your dual plug may just be incompatible with the default plugin)
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
xavierlp
Posts: 70
Joined: Tuesday 01 November 2016 16:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: France
Contact:

Re: [REQUEST] Plugin for Tuya

Post by xavierlp »

On the line 442

I can read Devices[key+1].Update(0,str(state[str(self.__ampere)]/1000)) # TypeName="Current (Single)

for key in self.__plugs:
error = error or self.__plugs[key].update_state(state[str(key)])
442-->>>>> Devices[key+1].Update(0,str(state[str(self.__ampere)]/1000)) # TypeName="Current (Single)
Devices[key+2].Update(0,str(state[str(self.__watt)]/10) + ";0") # kWh / Calculated
Devices[key+3].Update(0,str(state[str(self.__voltage)]/10)) # TypeName="Voltage"
Devices[key+4].Update(0,str(state[str(self.__watt)]/10)) # TypeName="Usage"
xavierlp
Posts: 70
Joined: Tuesday 01 November 2016 16:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: France
Contact:

Re: [REQUEST] Plugin for Tuya

Post by xavierlp »

For debugging I uncomment theses lines in __ini__.py

log = logging.getLogger(__name__)
logging.basicConfig() # TODO include function name/line numbers in log
log.setLevel(level=logging.DEBUG) # Debug hack!
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Re: [REQUEST] Plugin for Tuya

Post by FearNaBoinne »

My output shows the devID and gwID in reversed order compared to yours:

Code: Select all

INFO:pytuya:pytuya version 7.0.4
INFO:pytuya:Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.2.0] on linux
INFO:pytuya:Using PyCrypto (2, 6, 1, 'final', 0)
INFO:pytuya:Using PyCrypto from '/usr/lib/python3/dist-packages/Crypto/__init__.py'
DEBUG:pytuya:status() entry
DEBUG:pytuya:json_payload=b'{"gwId":"70116356840d8e5c3382","devId":"70116356840d8e5c3382"}'
DEBUG:pytuya:status received data=b'\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00L\x00\x00\x00\x00#H7\x00\x03\xe9\xb5p\x18\xdf`?\xa5+\x9f \x7f#\xf4\xe14)\x0f_5\xa1\x93\xb92f\xf1\xecp(\xff\xda\x9dT\xbeZ\xc4\xca\xbaSi@\x892\x14\xc3\x89\xbb2\xe6\x82\x02\xabI\xe0\x9c\xf9\x18\x7fQ`\x02\xba-\x00\x00\xaaU'
DEBUG:pytuya:result=b'#H7\x00\x03\xe9\xb5p\x18\xdf`?\xa5+\x9f \x7f#\xf4\xe14)\x0f_5\xa1\x93\xb92f\xf1\xecp(\xff\xda\x9dT\xbeZ\xc4\xca\xbaSi@\x892\x14\xc3\x89\xbb2\xe6\x82\x02\xabI\xe0\x9c\xf9\x18\x7fQ'
DEBUG:pytuya:decrypted result='{"devId":"70116356840d8e5c3382","dps":{"1":false,"9":0}}'

Plug State Information:
{'devId': '70116356840d8e5c3382', 'dps': {'1': False, '9': 0}}

Plug DPS List:
1
Not sure whether that is the 'data format error' referenced in your output...
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
tete
Posts: 8
Joined: Tuesday 11 September 2018 13:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [REQUEST] Plugin for Tuya

Post by tete »

FearNaBoinne wrote: Sunday 22 September 2019 22:27 # MIT License #
# #
# Copyright (c) 2018 tixi #

I've made these changes and now I can get the plugin working.

Plug State Information:
{'devId': '60417563840d8e68fe98', 'dps': {'22': 626, '19': 1334, '23': 34433, '9': 0, '20': 2358, '18': 634, '1': True, '24': 19774, '21': 1, '25': 1045}}

Plug DPS List:
1

However, when I add the device to domoticz, the values ​​are 0 and neither does work the on / off function.
But it works the on / off by command line (turnON.py, turnOFF.py).

What could be wrong?
xavierlp
Posts: 70
Joined: Tuesday 01 November 2016 16:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: France
Contact:

Re: [REQUEST] Plugin for Tuya

Post by xavierlp »

FearNaBoinne wrote: Monday 23 September 2019 15:04 My output shows the devID and gwID in reversed order compared to yours:

Code: Select all

INFO:pytuya:pytuya version 7.0.4
INFO:pytuya:Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.2.0] on linux
INFO:pytuya:Using PyCrypto (2, 6, 1, 'final', 0)
INFO:pytuya:Using PyCrypto from '/usr/lib/python3/dist-packages/Crypto/__init__.py'
DEBUG:pytuya:status() entry
DEBUG:pytuya:json_payload=b'{"gwId":"70116356840d8e5c3382","devId":"70116356840d8e5c3382"}'
DEBUG:pytuya:status received data=b'\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00L\x00\x00\x00\x00#H7\x00\x03\xe9\xb5p\x18\xdf`?\xa5+\x9f \x7f#\xf4\xe14)\x0f_5\xa1\x93\xb92f\xf1\xecp(\xff\xda\x9dT\xbeZ\xc4\xca\xbaSi@\x892\x14\xc3\x89\xbb2\xe6\x82\x02\xabI\xe0\x9c\xf9\x18\x7fQ`\x02\xba-\x00\x00\xaaU'
DEBUG:pytuya:result=b'#H7\x00\x03\xe9\xb5p\x18\xdf`?\xa5+\x9f \x7f#\xf4\xe14)\x0f_5\xa1\x93\xb92f\xf1\xecp(\xff\xda\x9dT\xbeZ\xc4\xca\xbaSi@\x892\x14\xc3\x89\xbb2\xe6\x82\x02\xabI\xe0\x9c\xf9\x18\x7fQ'
DEBUG:pytuya:decrypted result='{"devId":"70116356840d8e5c3382","dps":{"1":false,"9":0}}'

Plug State Information:
{'devId': '70116356840d8e5c3382', 'dps': {'1': False, '9': 0}}

Plug DPS List:
1
Not sure whether that is the 'data format error' referenced in your output...
What is strange for my side is that key and device id are the same !!!!
User avatar
FearNaBoinne
Posts: 144
Joined: Tuesday 30 April 2019 10:08
Target OS: Linux
Domoticz version: 2021.1
Location: Sector 0
Contact:

Re: [REQUEST] Plugin for Tuya

Post by FearNaBoinne »

It's not key and devID, it's devID and gwID... They key is different, but not shown in the debug info...
RasPi, Raspbian , Domoticz v2021.1, Z-Wave Stick, RFLink, RFXtrx433e, Hue, Tuya (Tasmota/Mosquitto with Discovery), ESP(easy), MySensors.org, OTGW
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests