[REQUEST] Plugin for Tuya

Python and python framework

Moderator: leecollings

User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: [REQUEST] Plugin for Tuya

Post by sincze »

Looks promissing ;-)
tuya-beta1.JPG
tuya-beta1.JPG (55.76 KiB) Viewed 3073 times
At least the data is updated :lol:

Due to "https://www.domoticz.com/wiki/Developin ... hon_plugin" in the first beta of the plugin I only have Mode1-Mode6 for variables.
With de debug option as shown in my earlier post I had 7 variables -> as a result the plugin did not work.

At the moment I solved by cheating a bit

Code: Select all

Devices[key+10].Update(0,str(state['4']/1000))	# TypeName="Current (Single)
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: [REQUEST] Plugin for Tuya

Post by sincze »

I added a separate Watt meter.
As I don't know yet how to deal with kwH ;-) or if I created the correct meter in Domoticz :lol:

i experiment with

Code: Select all

Domoticz.Device(Name="Tuya SmartPlug #" + str(val+11), Unit=val+11, TypeName="kWh").Create()
Domoticz.Device(Name="Tuya SmartPlug #" + str(val+12), Unit=val+12, Type=243, Subtype=29, Switchtype=0, Image=0, Options="").Create()

Python is not my language at all.
Tuya Beta 1.01_Watt.jpeg
Tuya Beta 1.01_Watt.jpeg (76.37 KiB) Viewed 3068 times
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
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 »

Looks great. Thanks again for the effort. Domoticz man ;)
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: [REQUEST] Plugin for Tuya

Post by sincze »

jslegers wrote: Wednesday 27 February 2019 8:48 Looks great. Thanks again for the effort. Domoticz man ;)
:lol: Maybe we can do a small test with your plugs?? Before public release ??
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: [REQUEST] Plugin for Tuya

Post by sincze »

This is interesting. The plug is switched off, but sometimes it still uses different amounts of Watts somehow.
tuya plug off.JPG
tuya plug off.JPG (43.45 KiB) Viewed 3055 times
Keep in mind the data represented comes from the plug itself.

Code: Select all

Key 1: False Plug State
Key 2: 0 UNKNOWN
Key 4: 0.122 Ampere Key is:11
Key 5: 3.9 Watt Key is:12
Key 6:: 241.7 Voltage Key is:13
I guess the electronics are as cheap as the plug itself?
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
dorenberg
Posts: 111
Joined: Monday 22 June 2015 20:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10982
Location: Veghel, The Netherlands
Contact:

Re: [REQUEST] Plugin for Tuya

Post by dorenberg »

I can volunteer too!!!
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: [REQUEST] Plugin for Tuya

Post by sincze »

dorenberg wrote: Wednesday 27 February 2019 16:06 I can volunteer too!!!
A 2 volonteers. Many thanks.

@dorenberg -> keep an eye on those dps-keys (you might need to adjust them in plugin.py -> see instructions).
I believe yours were a bit different from @jslegers and my powerplug.
Forum_Post.JPG
Forum_Post.JPG (49.16 KiB) Viewed 3032 times
I've added the plugin to https://www.domoticz.com/wiki/Plugins for future reference.

Please let me know your findings.
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
maczek
Posts: 4
Joined: Wednesday 27 February 2019 21:58
Target OS: NAS (Synology & others)
Domoticz version:
Location: Wroclaw / Poland
Contact:

Re: [REQUEST] Plugin for Tuya

Post by maczek »

Hi guys,

I've installed your plugin sincze and I'm getting error:

Code: Select all

2019-02-27 21:56:05.243 Status: (SmartPlug) Initialized version 1.0.0, author 'tixi_sincze'
2019-02-27 21:56:05.453 Error: (SmartPlug) 'onMessage' failed 'KeyError'.
2019-02-27 21:56:05.453 Error: (SmartPlug) ----> Line 530 in /home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/plugin.py, function onMessage
2019-02-27 21:56:05.453 Error: (SmartPlug) ----> Line 436 in /home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/plugin.py, function onMessage
Do you have idea what may be wrong?

I previously installed original tixi plugin and it worked fine.

The plugin does not work in Domoticz. However when I run get_dps.py I get correct response.

Thanks!
maczek
Server: RPi 2B + Domoticz | Mikrotik RB3011UiAS-RM
Gateway: Xiaomi Aqara
Devices: Xiaomi Motion Sensor | Xiaomi Temp. Sensor | Aqara Wall Switch x3 | Tuya SmartPlug
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: [REQUEST] Plugin for Tuya

Post by sincze »

maczek wrote: Wednesday 27 February 2019 22:03 Hi guys,

I've installed your plugin sincze and I'm getting error:

Code: Select all

2019-02-27 21:56:05.243 Status: (SmartPlug) Initialized version 1.0.0, author 'tixi_sincze'
2019-02-27 21:56:05.453 Error: (SmartPlug) 'onMessage' failed 'KeyError'.
2019-02-27 21:56:05.453 Error: (SmartPlug) ----> Line 530 in /home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/plugin.py, function onMessage
2019-02-27 21:56:05.453 Error: (SmartPlug) ----> Line 436 in /home/pi/domoticz/plugins/Domoticz-Tuya-SmartPlug-Plugin/plugin.py, function onMessage
Do you have idea what may be wrong?

I previously installed original tixi plugin and it worked fine.

The plugin does not work in Domoticz. However when I run get_dps.py I get correct response.

Thanks!
maczek
Can you also share outcome of get_dps.py? Please
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
dorenberg
Posts: 111
Joined: Monday 22 June 2015 20:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10982
Location: Veghel, The Netherlands
Contact:

Re: [REQUEST] Plugin for Tuya

Post by dorenberg »

I had the same. This is my version of the script. Spot the differences in DPS values of 104 105 and 106. These parts I have changed.


Domoticz.Device(Name="Tuya SmartPlug (Switch)", Unit=val, TypeName="Switch").Create()
Domoticz.Log("Tuya SmartPlug Device (Switch) #" + str(val) +" created.")
Domoticz.Device(Name="Tuya SmartPlug (A)" , Unit=val*1+10, TypeName="Current (Single)").Create()
Domoticz.Log("Tuya SmartPlug Device (A) #" + str(val*1+10) +" created.")
Domoticz.Device(Name="Tuya SmartPlug (kWh)", Unit=val*1+11, TypeName="kWh").Create()
Domoticz.Log("Tuya SmartPlug Device kWh #" + str(val*1+11) +" created.")
Domoticz.Device(Name="Tuya SmartPlug (V)", Unit=val*1+12, TypeName="Voltage").Create()
Domoticz.Log("Tuya SmartPlug Device (V) #" + str(val*1+12) +" created.")
Domoticz.Device(Name="Tuya SmartPlug (W)", Unit=val*1+13, TypeName="Usage").Create()
Domoticz.Log("Tuya SmartPlug Device (W) #" + str(val*1+14) +" created.")

Devices[key*1+10].Update(0,str(state[str(self.__ampere)]/1000)) # TypeName="Current (Single)
Devices[key*1+11].Update(0,str(state[str(self.__watt)]/10) + ";0") # kWh / Calculated
Devices[key*1+12].Update(0,str(state[str(self.__voltage)]/10)) # TypeName="Voltage"
Devices[key*1+13].Update(0,str(state[str(self.__watt)]/10)) # TypeName="Usage"

Domoticz.Debug("created. Key 101: " + str(state['1']) + " Plug State")
Domoticz.Debug("created. Key 104: " + str(state[str(self.__ampere)]/1000) + " Ampere Key is:" + str(key+10))
Domoticz.Debug("created. Key 105: " + str(state[str(self.__watt)]/10) + " Watt Key is:" + str(key+11))
Domoticz.Debug("created. Key 106:: " + str(state[str(self.__voltage)]/10) + " Voltage Key is:" + str(key+12))

Code: Select all

########################################################################################
# 	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.                                                                          #
#                                                                                      #
########################################################################################


"""
<plugin key="sincze_tuya_smartplug_plugin" name="Tuya SmartPlug" author="tixi_sincze" version="1.0.0" externallink=" https://github.com/sincze/Domoticz-Tuya-SmartPlug-Plugin">
	<params>
		<param field="Address" label="IP address" width="200px" required="true"/>
		<param field="Mode1" label="DevID" width="200px" required="true"/>
		<param field="Mode2" label="Local Key" width="200px" required="true"/>
		<param field="Mode3" label="DPS" width="200px" required="true" default="1"/>
		<param field="Mode4" label="DPS group" width="200px" required="true" default="None"/>
		<param field="Mode5" label="DPS always ON" width="200px" required="true" default="None"/>
		<param field="Mode6" label="Debug" width="75px">
			<options>
				<option label="0"   value="0" default="true"/>
				<option label="1"   value="1"/>
				<option label="2"   value="2"/>
				<option label="4"   value="4"/>
				<option label="8"   value="8"/>
				<option label="16"  value="16"/>
				<option label="32"  value="32"/>
				<option label="64"  value="64"/>
				<option label="128" value="128"/>
			</options>
		</param>
	</params>
</plugin>
"""

# https://wiki.domoticz.com/wiki/Developing_a_Python_plugin
# Debugging
# Value 	Meaning
# 0 		None. All Python and framework debugging is disabled.
# 1 		All. Very verbose log from plugin framework and plugin debug messages.
# 2 		Mask value. Shows messages from Plugin Domoticz.Debug() calls only.
# 4 		Mask Value. Shows high level framework messages only about major the plugin.
# 8 		Mask Value. Shows plugin framework debug messages related to Devices objects.
# 16 		Mask Value. Shows plugin framework debug messages related to Connections objects.
# 32 		Mask Value. Shows plugin framework debug messages related to Images objects.
# 64 		Mask Value. Dumps contents of inbound and outbound data from Connection objects.
# 128 		Mask Value. Shows plugin framework debug messages related to the message queue. 

import Domoticz
import pytuya
import json

########################################################################################
#
# plug object (represents a socket of the Tuya device)
#
########################################################################################
class Plug:

	#######################################################################
	#
	# constructor
	#
	#######################################################################
	def __init__(self,unit):
		self.__dps_id   = unit		# dps id
		self.__command  = None		# command ('On'/'Off'/None)
		self.__alwaysON = False		# True if the socket should be always ON, False otherwise
		return
	
	#######################################################################
	# update_state function
	#		update the domoticz device 
	#		and checks if the last command is equal to the current state
	#
	# parameters:
	#		state: True <=> On ; False <=> Off
	#
	# returns: 
	#		True in case of an error (the state does not correspond to the command)
	#		False otherwise
	#######################################################################
	def update_state(self,state): #state: True <=> On ; False <=> Off
		
		if(state):
			UpdateDevice(self.__dps_id, 1, "On")
			if(self.__command == 'Off'):
				return True
			else:
				self.__command = None
		
		elif(self.__alwaysON): #if not state: need to change the state for always_on devices
			self.__command = 'On'
			return True
			
		else:
			UpdateDevice(self.__dps_id, 0, "Off")
			if(self.__command == 'On'):
				return True
			else:
				self.__command = None

		return False
	
	#######################################################################
	#
	# set_command function
	# 		set the command for the next request
	#
	#######################################################################
	def set_command(self,cmd):
		if(self.__alwaysON):
			self.__command = 'On'
		else:
			self.__command = cmd
	
	#######################################################################
	#
	# set_alwaysON function
	# 		set __alwaysON to True
	#
	#######################################################################
	def set_alwaysON(self):
		self.__alwaysON = True
		self.__command  = 'On'
	
	#######################################################################
	#
	# put_payload function
	#		add to dict_payload the command to be sent to the device
	#
	#######################################################################
	def put_payload(self,dict_payload):
		
		if(self.__command == None):
			return
		
		if(self.__command =="On"):
			dict_payload[str(self.__dps_id)] = True
		else:
			dict_payload[str(self.__dps_id)] = False

########################################################################################
	
########################################################################################
#
# plugin object
#
########################################################################################
class BasePlugin:
	
	#######################################################################
	#
	# constant definition
	#
	#######################################################################
	__HB_BASE_FREQ = 2			  #heartbeat frequency (val x 10 seconds)
	__VALID_CMD    = ('On','Off') #list of valid command

	#######################################################################
	#
	# private functions definition
	#	__extract_status
	#	__is_encoded
	#	__command_to_execute
	#
	#######################################################################
	
	
	#######################################################################
	#
	# __extract_status
	#
	# Parameter
	#	Data: a received payload from the tuya smart plug
	#
	# Returns a tuple (bool,dict)
	#	first:  set to True if an error occur and False otherwise
	#	second: dict of the dps (irrelevant if first is True )
	#
	#######################################################################
	def __extract_status(self, Data):

		start=Data.find(b'{"devId')
		
		if(start==-1):
			return (True,"")
			
		result = Data[start:] #in 2 steps to deal with the case where '}}' is present before {"devId'
			
		end=result.find(b'}}')
		
		if(end==-1):
			return (True,"")
		
		end=end+2
		result = result[:end]
		if not isinstance(result, str):
			result = result.decode()
			
		try:
			result = json.loads(result)
			return (False,result['dps'])
		except (JSONError, KeyError) as e:
			return (True,"")


	#######################################################################
	#
	# __is_encoded
	#
	# Parameter
	#	Data: a received payload from the tuya smart plug
	#
	# Returns 
	#	True if Data is encoded
	#	False otherwise
	#
	# Remark: for debugging purpose
	#
	#######################################################################
	#~ def __is_encoded(self, Data):

		#~ tmp = Data[20:-8]  # hard coded offsets
		#~ if(tmp.startswith(b'3.1')):#PROTOCOL_VERSION_BYTES
			#~ return True
		#~ else:
			#~ return False
			
	#######################################################################
	#
	# __command_to_execute
	#	send a command (set or status) to the tuya device
	#
	#
	#######################################################################
	def __command_to_execute(self):
		
		self.__runAgain = self.__HB_BASE_FREQ
				
		if(self.__connection.Connected()):
					
			dict_payload = {}
			
			for key in self.__plugs:
				self.__plugs[key].put_payload(dict_payload)
			
			if(len(dict_payload) != 0):
				self.__state_machine = 1
				payload = self.__device.generate_payload('set', dict_payload)
				self.__connection.Send(payload)
			
			else:
				self.__state_machine = 2
				payload=self.__device.generate_payload('status')
				self.__connection.Send(payload)	
			
		else:
			if(not self.__connection.Connecting()):
				self.__connection.Connect()	
	
	#######################################################################
	#
	# constructor
	#
	#######################################################################
	def __init__(self):
		self.__address          = None          		#IP address of the smartplug
		self.__devID            = None          		#devID of the smartplug
		self.__localKey         = None          		#localKey of the smartplug
		self.__device           = None          		#pytuya object of the smartplug
		self.__ampere	        = 104						#key for Ampere
		self.__watt             = 105						#key for Watt
		self.__voltage          = 106						#key for Voltage
		self.__runAgain         = self.__HB_BASE_FREQ	#heartbeat frequency
		self.__connection       = None					#connection to the tuya plug
		self.__unit2dps_id_list = None					#mapping between Unit and list of dps id
		self.__plugs	        = None					#mapping between dps id and a plug object
		self.__state_machine    = 0						#state_machine: 0 -> no waiting msg ; 1 -> set command sent ; 2 -> status command sent
		return
		
	#######################################################################
	#		
	# onStart Domoticz function
	#
	#######################################################################
	def onStart(self):
		
		# Debug mode
		Domoticz.Debugging(int(Parameters["Mode6"]))
		Domoticz.Debug("onStart called")
			
		#get parameters
		self.__address  = Parameters["Address"]
		self.__devID    = Parameters["Mode1"]
		self.__localKey = Parameters["Mode2"]
		
		#set the next heartbeat
		self.__runAgain = self.__HB_BASE_FREQ
		
		#build internal maps (__unit2dps_id_list and __plugs)
		self.__unit2dps_id_list = {}
		self.__plugs            = {}
		max_unit                = 0
		max_dps					= 0
		for val in sorted(Parameters["Mode3"].split(";")):
			
			self.__unit2dps_id_list[int(val)]=[int(val),]
			
			self.__plugs[int(val)]=Plug(int(val))
			
			if(int(val)>max_unit):
				max_unit=int(val)
	
		max_dps = max_unit
		
		#groups management: #syntax: 1;2 : 3;4
		max_unit = max_unit + 1
		if(Parameters["Mode4"]!="None"):
			groups = Parameters["Mode4"].split(":")
			for group in groups:
				self.__unit2dps_id_list[max_unit]=[]
				for val in sorted(group.split(";")):
					self.__unit2dps_id_list[max_unit].append(int(val))
				max_unit = max_unit + 1
				
		#create domoticz devices			
		if(len(Devices) == 0):
			for val in self.__unit2dps_id_list:
				
				if(val <= max_dps): #single socket dps
					Domoticz.Device(Name="Tuya SmartPlug (Switch)", Unit=val, TypeName="Switch").Create()
					Domoticz.Log("Tuya SmartPlug Device (Switch) #" + str(val) +" created.")
					Domoticz.Device(Name="Tuya SmartPlug (A)" , Unit=val*1+10, TypeName="Current (Single)").Create()
					Domoticz.Log("Tuya SmartPlug Device (A) #" + str(val*1+10) +" created.")
					Domoticz.Device(Name="Tuya SmartPlug (kWh)", Unit=val*1+11, TypeName="kWh").Create()
					Domoticz.Log("Tuya SmartPlug Device kWh #" + str(val*1+11) +" created.")
					Domoticz.Device(Name="Tuya SmartPlug (V)", Unit=val*1+12, TypeName="Voltage").Create()
					Domoticz.Log("Tuya SmartPlug Device (V) #" + str(val*1+12) +" created.")
					Domoticz.Device(Name="Tuya SmartPlug (W)", Unit=val*1+13, TypeName="Usage").Create()
					Domoticz.Log("Tuya SmartPlug Device (W) #" + str(val*1+14) +" created.")

				else: #group: selector switch
					Options = {"LevelActions": "|",
						"LevelNames": "Off|On",
						"LevelOffHidden": "false",
						"SelectorStyle": "0"}
					Domoticz.Device(Name="Tuya SmartPlug #" + str(val), Unit=val, TypeName="Selector Switch", Options=Options).Create()
					Domoticz.Log("Tuya SmartPlug Device #" + str(val) +" created.")
		
		#manage always on
		if(Parameters["Mode5"]!="None"):
			for val in sorted(Parameters["Mode5"].split(";")):
				self.__plugs[int(val)].set_alwaysON()
		
		#create the pytuya object
		self.__device = pytuya.OutletDevice(self.__devID, self.__address, self.__localKey)

		#state machine
		self.__state_machine = 0

		#start the connection
		self.__connection = Domoticz.Connection(Name="Tuya", Transport="TCP/IP", Address=self.__address, Port="6668")
		self.__connection.Connect()

	#######################################################################
	#		
	# onConnect Domoticz function
	#
	#######################################################################
	def onConnect(self, Connection, Status, Description):
		if (Connection == self.__connection):
			if (Status == 0):
				Domoticz.Debug("Connected successfully to: "+Connection.Address+":"+Connection.Port)
				self.__command_to_execute()
			else:
				Domoticz.Debug("OnConnect Error Status: " + str(Status))
				if(Status==113):#no route to host error (skip to avoid intempestive connect call)
					return
				if(self.__connection.Connected()):
					self.__connection.Disconnect()
				if(not self.__connection.Connecting()):
					self.__connection.Connect()
				


	#######################################################################
	#		
	# onMessage Domoticz function
	#
	#######################################################################
	def onMessage(self, Connection, Data):
		Domoticz.Debug("onMessage called: " + Connection.Address + ":" + Connection.Port +" "+ str(Data))
		
		if (Connection == self.__connection):
			
			if(self.__state_machine == 0):#skip nothing was waiting
				return
			
			if(self.__state_machine == 1):#after a set command: need to ask the status
				self.__state_machine = 2
				payload=self.__device.generate_payload('status')
				self.__connection.Send(payload)#TODO active connection check (it should be because we just get a message)
				return
				
			#now self.__state_machine == 2
			self.__state_machine = 0
			
			(error,state) = self.__extract_status(Data)
			if(error):
				self.__command_to_execute()
				return
						
			error = False
			for key in self.__plugs:				
				error = error or self.__plugs[key].update_state(state[str(key)])	
				Devices[key*1+10].Update(0,str(state[str(self.__ampere)]/1000))	# TypeName="Current (Single)
				Devices[key*1+11].Update(0,str(state[str(self.__watt)]/10) + ";0")	# kWh / Calculated
				Devices[key*1+12].Update(0,str(state[str(self.__voltage)]/10)) 	# TypeName="Voltage"
				Devices[key*1+13].Update(0,str(state[str(self.__watt)]/10)) 	# TypeName="Usage"

				Domoticz.Debug("created. Key 101: " + str(state['1']) + " Plug State")
				Domoticz.Debug("created. Key 104: " + str(state[str(self.__ampere)]/1000) + " Ampere Key is:" + str(key+10))
				Domoticz.Debug("created. Key 105: " + str(state[str(self.__watt)]/10) + " Watt Key is:" + str(key+11))
				Domoticz.Debug("created. Key 106:: " + str(state[str(self.__voltage)]/10) + " Voltage Key is:" + str(key+12))

				
			if(error):
				self.__command_to_execute()

	#######################################################################
	#		
	# onCommand Domoticz function
	#
	#######################################################################
	def onCommand(self, Unit, Command, Level, Hue):
		Domoticz.Debug("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + " Level: " + str(Level))
		
		if(Command=="Set Level"): #group (selector switch): convert level to command (0 <=> 'Off' ; 10 <=> 'On')
			if(Level==0):
				Command = 'Off'
			elif(Level==10):
				Command = 'On'
			else:
				Domoticz.Error("Undefined Level: " + str(Level))
				return
			
		if(Command not in self.__VALID_CMD):
			Domoticz.Error("Undefined command: " + Command)
			return
		
		for val in self.__unit2dps_id_list[Unit]:
			self.__plugs[val].set_command(Command)
		
		self.__command_to_execute()		

	#######################################################################
	#		
	# onDisconnect Domoticz function
	#
	#######################################################################
	def onDisconnect(self, Connection):
		Domoticz.Debug("Disconnected from: "+Connection.Address+":"+Connection.Port)

	#######################################################################
	#		
	# onHeartbeat Domoticz function
	#
	#######################################################################
	def onHeartbeat(self):
		self.__runAgain -= 1
		if(self.__runAgain == 0):
			self.__command_to_execute()
	
	#######################################################################
	#		
	# onStop Domoticz function
	#
	#######################################################################
	def onStop(self):
		self.__device           = None
		self.__plugs	        = None
		self.__unit2dps_id_list = None
		if(self.__connection.Connected() or self.__connection.Connecting()):
			self.__connection.Disconnect()
		self.__connection       = None
		self.__state_machine    = 0

########################################################################################
#
# Domoticz plugin management
#
########################################################################################
global _plugin
_plugin = BasePlugin()

def onStart():
	global _plugin
	_plugin.onStart()

def onStop():
	global _plugin
	_plugin.onStop()

def onConnect(Connection, Status, Description):
	global _plugin
	_plugin.onConnect(Connection, Status, Description)

def onMessage(Connection, Data):
	global _plugin
	_plugin.onMessage(Connection, Data)

def onCommand(Unit, Command, Level, Hue):
	global _plugin
	_plugin.onCommand(Unit, Command, Level, Hue)

def onDisconnect(Connection):
	global _plugin
	_plugin.onDisconnect(Connection)

def onHeartbeat():
	global _plugin
	_plugin.onHeartbeat()

################################################################################
# Generic helper functions
################################################################################

def UpdateDevice(Unit, nValue, sValue, TimedOut=0, AlwaysUpdate=False):
	# Make sure that the Domoticz device still exists (they can be deleted) before updating it
	if Unit in Devices:
		if Devices[Unit].nValue != nValue or Devices[Unit].sValue != sValue or Devices[Unit].TimedOut != TimedOut or AlwaysUpdate:
			Devices[Unit].Update(nValue=nValue, sValue=str(sValue), TimedOut=TimedOut)
			Domoticz.Debug("Update " + Devices[Unit].Name + ": " + str(nValue) + " - '" + str(sValue) + "'")
maczek
Posts: 4
Joined: Wednesday 27 February 2019 21:58
Target OS: NAS (Synology & others)
Domoticz version:
Location: Wroclaw / Poland
Contact:

Re: [REQUEST] Plugin for Tuya

Post by maczek »

The output of get_dps.py:

Code: Select all

Plug State Information:
{'dps': {'23': 30147, '25': 1205, '21': 1, '1': True, '22': 623, '19': 891, '18': 432, '20': 2346, '24': 17293, '9': 0}, 'devId': '46268374840d8e953b36'}

Plug DPS List:
1
Server: RPi 2B + Domoticz | Mikrotik RB3011UiAS-RM
Gateway: Xiaomi Aqara
Devices: Xiaomi Motion Sensor | Xiaomi Temp. Sensor | Aqara Wall Switch x3 | Tuya SmartPlug
dorenberg
Posts: 111
Joined: Monday 22 June 2015 20:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10982
Location: Veghel, The Netherlands
Contact:

Re: [REQUEST] Plugin for Tuya

Post by dorenberg »

You have to check in the smart life app which values corresponding to the parameters in the list.
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: [REQUEST] Plugin for Tuya

Post by sincze »

dorenberg wrote: Thursday 28 February 2019 6:34 You have to check in the smart life app which values corresponding to the parameters in the list.
@dorenberg. Tnx for helping me test the plugin with different types of plugs. Interesting to see what is going wrong with variables "val & key" in the plugin. We could agree upon making the plug single socket only. :lol: and use the spare parameter in the hardware tab to fill in your device id's instead of hacking them into the plugin.py

Just for the fun In meantime
Your:

Code: Select all

Domoticz.Device(Name="Tuya SmartPlug (A)" , Unit=val*1+10, TypeName="Current (Single)").Create()
Domoticz.Log("Tuya SmartPlug Device (A) #" + str(val*1+10) +" created."
Could as well be:

Code: Select all

Domoticz.Device(Name="Tuya SmartPlug (A)" , Unit=val+10, TypeName="Current (Single)").Create()
Domoticz.Log("Tuya SmartPlug Device (A) #" + str(val+10) +" created."
As the multiplier was chosen (and because I don't have such a multi plug... guessed by me) to avoid overlapping device-ids. :shock:
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
maczek
Posts: 4
Joined: Wednesday 27 February 2019 21:58
Target OS: NAS (Synology & others)
Domoticz version:
Location: Wroclaw / Poland
Contact:

Re: [REQUEST] Plugin for Tuya

Post by maczek »

Hi guys,

I found solution to my problem:

Code: Select all

Plug State Information:
{'devId': '46268374840d8e953b36', 'dps': {'1': True, '23': 30147, '18': 175, '25': 1205, '9': 0, '22': 623, '19': 260, '24': 17293, '20': 2334, '21': 1}}

Plug DPS List:
1
Then I had to adjust values in constructor:

Code: Select all

        self.__ampere = 18  # key for Ampere
        self.__watt = 19  # key for Watt
        self.__voltage = 20  # key for Voltage
My unit IDs in Domoticz start from 14 so I edited this:

Code: Select all

                Devices[key * 1 + 13].Update(0, str(state[str(self.__ampere)] / 1000))  # TypeName="Current (Single)
                Devices[key * 1 + 14].Update(0, str(state[str(self.__watt)] / 10) + ";0")  # kWh / Calculated
                Devices[key * 1 + 15].Update(0, str(state[str(self.__voltage)] / 10))  # TypeName="Voltage"
                Devices[key * 1 + 16].Update(0, str(state[str(self.__watt)] / 10))  # TypeName="Usage"
I thought it would work if I set the DPS to 4 in device settings but it didn't work so I had to change 10,11,12,13 to 13,14,15,16.

Works fine, no error :) I wonder how the total KWh usage is collected - is it calculated by the device or by the plugin? Is it the average of all reads?

Cheers,
maczek
Server: RPi 2B + Domoticz | Mikrotik RB3011UiAS-RM
Gateway: Xiaomi Aqara
Devices: Xiaomi Motion Sensor | Xiaomi Temp. Sensor | Aqara Wall Switch x3 | Tuya SmartPlug
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: [REQUEST] Plugin for Tuya

Post by sincze »

maczek wrote: Thursday 28 February 2019 11:13 Hi guys,

I found solution to my problem:

Code: Select all

Plug State Information:
{'devId': '46268374840d8e953b36', 'dps': {'1': True, '23': 30147, '18': 175, '25': 1205, '9': 0, '22': 623, '19': 260, '24': 17293, '20': 2334, '21': 1}}

Plug DPS List:
1
Then I had to adjust values in constructor:

Code: Select all

        self.__ampere = 18  # key for Ampere
        self.__watt = 19  # key for Watt
        self.__voltage = 20  # key for Voltage
Tnx for your feedback.
That is 2 happy testers.
This gives me some python programming skill confidence. :lol:
Works fine, no error :) I wonder how the total KWh usage is collected - is it calculated by the device or by the plugin? Is it the average of all reads?
That is a tricky one indeed.
I had to modify (edit the created device) and put in on 'calculated'.
As by default it is set to a 'device' thing.
But that data is not retrieved from the plug as far as I know so the plugin will send '0' all the time. Just do a ctrl-f for ';0'
I could not find a different plugin that showed me how to do it straight out of the box.
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
maczek
Posts: 4
Joined: Wednesday 27 February 2019 21:58
Target OS: NAS (Synology & others)
Domoticz version:
Location: Wroclaw / Poland
Contact:

Re: [REQUEST] Plugin for Tuya

Post by maczek »

Reading and debugging this code gave me also some (a little) python skills confidence - I have 12+ years of commercial background in PHP but started learning Python recently ;)

Funny thing is that I bought this plug yesterday and then started googling to find a way to setup it in Domoticz. I wasn't aware that this plugin was so young :D
Server: RPi 2B + Domoticz | Mikrotik RB3011UiAS-RM
Gateway: Xiaomi Aqara
Devices: Xiaomi Motion Sensor | Xiaomi Temp. Sensor | Aqara Wall Switch x3 | Tuya SmartPlug
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: [REQUEST] Plugin for Tuya

Post by sincze »

maczek wrote: Thursday 28 February 2019 12:02 Reading and debugging this code gave me also some (a little) python skills confidence - I have 12+ years of commercial background in PHP but started learning Python recently ;)
offtopic: If PHP is your thing.. Why not speed up your Domoticz with Pass2PHP
https://www.domoticz.com/forum/viewtopic.php?t=12343

I modified the plugin to help somebody else, as I use Pass2PHP for my setup. :lol:
Even had to borrow a plug.

Fun part with the plugins is that they create the devices for you ;-) With pass2php I had to create virtual devices all the time.
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
dorenberg
Posts: 111
Joined: Monday 22 June 2015 20:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10982
Location: Veghel, The Netherlands
Contact:

Re: [REQUEST] Plugin for Tuya

Post by dorenberg »

The counting seems not to work over here. Keeps 0.000 kwh. So maybe I did something wrong.

all info is available in the plug. Just in another json block.....

0.02 is in kWh of today. 0.43 is total.

{
"result": {
"thisDay": "0.02",
"sum": "0.43",
"years": {
"2019": {
"02": "0.43"
}
}
},
"t": 1549792047291,
"success": true,
"status": "ok"
}
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: [REQUEST] Plugin for Tuya

Post by sincze »

dorenberg wrote: Thursday 28 February 2019 12:41 The counting seems not to work over here. Keeps 0.000 kwh. So maybe I did something wrong.

all info is available in the plug. Just in another json block.....

0.02 is in kWh of today. 0.43 is total.

{
"result": {
"thisDay": "0.02",
"sum": "0.43",
"years": {
"2019": {
"02": "0.43"
}
}
},
"t": 1549792047291,
"success": true,
"status": "ok"
}
Ok you have a more expensive plug that is for sure :lol:
I have it solved like this:
computed.JPG
computed.JPG (38.82 KiB) Viewed 2991 times
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: [REQUEST] Plugin for Tuya

Post by sincze »

I modified my local plugin so now you can add the DPS values of Amp;Watt;Volt in the hardware config screen.
tuya-divider.png
tuya-divider.png (16.75 KiB) Viewed 2986 times
And it does work.. ;-)
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest