Page 1 of 26

Python Plugin: TUYA

Posted: Wednesday 24 June 2020 3:26
by itguy78
Now that TUYA and IFTTT are no longer cooperating I could no longer use IFTTT to control my TUYA devices, so I wrote a python plugin for my own use to control TUYA devices around the house.

There is a large number of brands that are manufactured by Tuya including Merkury, Geenie, etc (names may vary/change around the world) but with the features and price-range they are likely to be around for a long time.

This is working well for me, so anyone is welcome to use/try it and provide any feedback.
I only have a few LED lights (white/color) and wallplug switches but I am pretty sure this will work with most TUYA devices for on/off status and control. It is also able to activate 'scenes' created/configured in the TUYA app.
The main thing for me was a solution that does not require setting up IP addresses/port settings for each device (and have to manually update them when anything changed) and worse trying to find out device IDs and Keys, so this plugin automatically detects/redetects TUYA devices on your account and adds them with the name given in the TUYA app.

Fair warning: there are draw-backs from using the Cloud:
-Not being able to control the devices locally if your internet is down (I have fast/reliable internet and there's always the TUYA app as last resort)
-it can take up to 1 minute for the status of the device to be updated in the cloud (should not be an issue if you always use Domoticz to control it)

For Downloads/Install steps/Source see this link:
https://github.com/guino/tuyaha

Re: Python Plugin: TUYA

Posted: Friday 26 June 2020 21:05
by nico4sen
Will this also work when using the SmartLife app?

Re: Python Plugin: TUYA

Posted: Saturday 27 June 2020 11:38
by sincze
Tnx.
Nice addition for people that have their Tuya devices in the cloud and not reflashed by Tasmota.
Don't forget to add the plugin to: https://www.domoticz.com/wiki/Plugins

Re: Python Plugin: TUYA

Posted: Wednesday 01 July 2020 5:52
by itguy78
nico4sen wrote: Friday 26 June 2020 21:05 Will this also work when using the SmartLife app?
I have never tried but looking at some info I believe the only change required would be to change line 170 of plugin.py to say "smart_life" instead of "tuya" -- I don't have anything enrolled in the smart life app but if someone tries and it works I can make a setting for the plugin.

EDIT: I enrolled a device in the smart life app and made the change I suggested above and it appears to work -- I'll make the change to the plugin so you can configure the app type in the settings (when I have time)

Re: Python Plugin: TUYA

Posted: Wednesday 01 July 2020 22:19
by itguy78
sincze wrote: Saturday 27 June 2020 11:38 Tnx.
Nice addition for people that have their Tuya devices in the cloud and not reflashed by Tasmota.
Don't forget to add the plugin to: https://www.domoticz.com/wiki/Plugins
I honestly don't know how to edit this page -- I believe I need to login but it says forum user/pass won't work (and I tried) and there's no 'create account' anywhere that I can see as mentioned in the help login page, so no luck for me.

Re: Python Plugin: TUYA

Posted: Friday 03 July 2020 18:35
by itguy78
I have made the changes that allow the plugin to be configured for SmartLife or Jinvoo Smart app (as well as Tuya). Simply select the app from the drop down in the hardware settings (Options are: Tuya, Smart Life or Jinvoo Smart) and it should automatically detect the devices from the app in question. The link on the original post has been updated with the latest version of the plugin.

Re: Python Plugin: TUYA

Posted: Friday 03 July 2020 20:37
by nico4sen
It works. Nice, thx

Re: Python Plugin: TUYA

Posted: Tuesday 14 July 2020 22:06
by DarkG
Is there a way to change temperature for my AC? I can only turn on and off.

Re: Python Plugin: TUYA

Posted: Wednesday 15 July 2020 19:51
by itguy78
DarkG wrote: Tuesday 14 July 2020 22:06 Is there a way to change temperature for my AC? I can only turn on and off.
I wrote the plugin with support for on/off (any device with on/off switch) and for setting brightness/color for LED lights. These are the only devices I have to test with.

The API has functions to read and set the 'target' temperature and to read the 'current' temperature, but I have no way to test these features. Someone with access to the device should be able to add in a new device that displays current temperature and allows changing the target temperature. Only way I could work on this is if you sent me your app account information (private message) so I could configure it and add it to the plugin -- obviously you'd have to not mind me messing with the settings for development.

EDIT: You should be able to create a 'scene' in the app to set the temperature of the AC to some fixed values you commonly use and then add these scene buttons to domoticz so it changes the temperature to these preset values.

Re: Python Plugin: TUYA

Posted: Friday 17 July 2020 21:59
by DarkG
itguy78 wrote: Wednesday 15 July 2020 19:51
DarkG wrote: Tuesday 14 July 2020 22:06 Is there a way to change temperature for my AC? I can only turn on and off.
I wrote the plugin with support for on/off (any device with on/off switch) and for setting brightness/color for LED lights. These are the only devices I have to test with.

The API has functions to read and set the 'target' temperature and to read the 'current' temperature, but I have no way to test these features. Someone with access to the device should be able to add in a new device that displays current temperature and allows changing the target temperature. Only way I could work on this is if you sent me your app account information (private message) so I could configure it and add it to the plugin -- obviously you'd have to not mind me messing with the settings for development.

EDIT: You should be able to create a 'scene' in the app to set the temperature of the AC to some fixed values you commonly use and then add these scene buttons to domoticz so it changes the temperature to these preset values.

I wrote you a pm

Re: Python Plugin: TUYA

Posted: Wednesday 05 August 2020 15:19
by EdwinK
Thanks for this plugin. I now can use my LSC/Tuya LED-strip with domoticz.

Now I need to create some scenes for it.

Re: Python Plugin: TUYA

Posted: Sunday 30 August 2020 18:46
by Xenomes
Hi itguy78,

Is your (plugin.py) script still under development? The plugin works perfectly, I miss some warm and cold contols for my RBGWW LED light (Realtek chip). I tried to edit / change your script by adding the CW and WW channel. But my coding skills are insufficient. Hopefully you see possibilities to add it. Of Course I want to test things for you and provide info to help your progress.

Ps. You still need to add it to the https://www.domoticz.com/wiki/Plugins 😉

Greeting Xenomes

Re: Python Plugin: TUYA

Posted: Monday 31 August 2020 3:04
by itguy78
Xenomes wrote: Sunday 30 August 2020 18:46 Hi itguy78,

Is your (plugin.py) script still under development? The plugin works perfectly, I miss some warm and cold contols for my RBGWW LED light (Realtek chip). I tried to edit / change your script by adding the CW and WW channel. But my coding skills are insufficient. Hopefully you see possibilities to add it. Of Course I want to test things for you and provide info to help your progress.

Ps. You still need to add it to the https://www.domoticz.com/wiki/Plugins 😉

Greeting Xenomes
I would need to know of a plugin that has the warm/cold controls to know what kind of properties are supposed to be set in Domoticz to display these controls. I would also need access to one of these lights so I can see the properties available and provide the controls for them. I only have some basic RGB lights and while lights to test with, so I am not sure what to look for but I would expect the current RGB control works for your lights too?

I haven't added it to the wiki because my domoticz forum account doesn't work on it and the 'create account' function isn't working for me, so unless someone has a working account and can add it I don't have a way to do it myself. From what I looked at the 'Login' page should have a 'create account' link but it is not anywhere to be found:
Screenshot_2020-08-30_21-05-59.png
Screenshot_2020-08-30_21-05-59.png (102.97 KiB) Viewed 35861 times

Re: Python Plugin: TUYA

Posted: Monday 31 August 2020 13:19
by Xenomes
itguy78 wrote: Monday 31 August 2020 3:04 I haven't added it to the wiki because my domoticz forum account doesn't work on it and the 'create account' function isn't working for me, so unless someone has a working account and can add it I don't have a way to do it myself. From what I looked at the 'Login' page should have a 'create account' link but it is not anywhere to be found:
Screenshot_2020-08-30_21-05-59.png
Maybe one of the most active users can help:
2020-08-31 12_23_30-Active users list - Domoticz.png
2020-08-31 12_23_30-Active users list - Domoticz.png (10.13 KiB) Viewed 35833 times
itguy78 wrote: Monday 31 August 2020 3:04 I would need to know of a plugin that has the warm/cold controls to know what kind of properties are supposed to be set in Domoticz to display these controls. I would also need access to one of these lights so I can see the properties available and provide the controls for them. I only have some basic RGB lights and while lights to test with, so I am not sure what to look for but I would expect the current RGB control works for your lights too?
These plugin uses the WW and WC for Tasmota flashed tuya bulbs, https://github.com/emontnemery/domoticz_mqtt_discovery and the https://github.com/mario-peters/ShellyCloudPlugin in the Bulb section, the device settings are Type 241, subType is 4 and SwitchType 7. If i set the rgb trigger in the middle then there is a strange blueish light.

I am running your script with the latest version of tuyaha (0.0.7)

Edit: I have added a warm/cold white channel support.
Line 205

Code: Select all

                            Domoticz.Device(Name=dev.name(), Unit=unit, Type=241, Subtype=4, Switchtype=7, DeviceID=dev.object_id()).Create()
Line 136-152

Code: Select all

        elif Command == 'Set Color':
            # Convert RGB to Hue+Saturation
            rgb = json.loads(Hue)
            h, s = rgb_to_hs(rgb.get("r"), rgb.get("g"), rgb.get("b"))
            mode = rgb.get("m")
            t = rgb.get("t")
            Domoticz.Debug("color="+str(rgb)+" h="+str(h)+" s="+str(s))
            # If color changed
            if Devices[Unit].Color != Hue:
                if mode == 3:
                      dev.set_color( [ h*360, s*100 ] )
                      Domoticz.Debug("Set color called")
                if mode == 2:
                      temp = round(1000+(9000/255*(255-t)))
                      Domoticz.Debug("temp = " + str(temp))
                      dev.set_color_temp( temp )
                      Domoticz.Debug("Set white called")
            # If level changed

Re: Python Plugin: TUYA

Posted: Monday 31 August 2020 21:28
by itguy78
The changes look simple enough -- I'll test it with my regular color lights to make sure it works then I can merge them in. Thanks!

Re: Python Plugin: TUYA

Posted: Monday 31 August 2020 21:58
by Xenomes
Here a other modification for detect on/off, brightness, WW, RGB and RGBWW lights.

Code: Select all

                    if dev_type == "light":
                        if dev.data.get("color_mode") is not None and dev.data.get("color_temp") is not None and dev.data.get("brightness") is not None:
                            # Light Color and White temperature contol
                            Domoticz.Device(Name=dev.name(), Unit=unit, Type=241, Subtype=4, Switchtype=7, DeviceID=dev.object_id()).Create()
                        elif dev.data.get("color_mode") is not None and dev.data.get("color_temp") is None and dev.data.get("brightness") is not None:
                            # Light Color control
                            Domoticz.Device(Name=dev.name(), Unit=unit, Type=241, Subtype=2, Switchtype=7, DeviceID=dev.object_id()).Create()
                        elif dev.data.get("color_mode") is None and dev.data.get("color_temp") is not None and dev.data.get("brightness") is not None:
                            # Light White temperature control
                            Domoticz.Device(Name=dev.name(), Unit=unit, Type=241, Subtype=8, Switchtype=7, DeviceID=dev.object_id()).Create()
                        elif dev.data.get("color_mode") is None and dev.data.get("color_temp") is None and dev.data.get("brightness") is not None:
                            # Light Brightness control
                            Domoticz.Device(Name=dev.name(), Unit=unit, Type=241, Subtype=3, Switchtype=7, DeviceID=dev.object_id()).Create()
                        elif dev.data.get("color_mode") is None and dev.data.get("color_temp") is None and dev.data.get("brightness") is None:
                            # Light On/Off control
                            Domoticz.Device(Name=dev.name(), Unit=unit, Type=244, Subtype=73, Switchtype=7, Image=0, DeviceID=dev.object_id()).Create()
                        else:
                            Domoticz.Debug("No controls found for your light device!")

Re: Python Plugin: TUYA

Posted: Monday 31 August 2020 23:58
by cobra045
itguy78 wrote: Monday 31 August 2020 3:04
I haven't added it to the wiki because my domoticz forum account doesn't work on it and the 'create account' function isn't working for me, so unless someone has a working account and can add it I don't have a way to do it myself. From what I looked at the 'Login' page should have a 'create account' link but it is not anywhere to be found:
Screenshot_2020-08-30_21-05-59.png
I found it, here you can create the requested account

https://www.mediawiki.org/w/index.php?t ... n=loginCTA

Cobra045

Re: Python Plugin: TUYA

Posted: Wednesday 02 September 2020 5:50
by Xenomes
Hi itguy78,

Is it possible that you change your github clone in a standalone project? called 'Domoticz-TUYA-Plugin' (as in the file) your script is only depending on tuyaha and not based on. I am running it now without the original files in the directory but installed sudo pip3 install tuyaha . An other benefit is that users can make issues/device support request and you can make a beta branch for developing. Others can clone and modify and make improvements. Think about it.

Greeting Xenomes

Re: Python Plugin: TUYA

Posted: Thursday 03 September 2020 15:16
by Chris12
Hi, I ran into this topic because I just received a wifi smartplug via AliExpress which I want to use to control my garden lights based on the daylight/sunrise times.

The smartplug works fine via the smartplug app. I can see that the plug has firmware version 1.4.2 (Main/MCU)

My domoticz (beta 2020.2) runs on a Synology NAS, and I added the TUYA plugin so it is visable in teh hwardware list.

When adding the TUYA hardware, that goes fine, I cannot see any devices created after that?
In the logging I can see this:

2020-09-03 15:13:05.892 Error: (tuya) failed to load 'plugin.py', Python Path used was '/usr/local/domoticz/var/plugins/tuyaha-master/:/volume1/@appstore/py3k/usr/local/lib/python35.zip:/volume1/@appstore/py3k/usr/local/lib/python3.5/:/volume1/@appstore/py3k/usr/local/lib/python3.5/plat-linux:/volume1/@appstore/py3k/usr/local/lib/python3.5/lib-dynload:/volume1/@appstore/py3k/usr/local/lib/python3.5/site-packages'.
2020-09-03 15:13:05.892 Error: (SmartPlug-1) Module Import failed, exception: 'ImportError'
2020-09-03 15:13:05.892 Error: (SmartPlug-1) Module Import failed: ' Name: requests'
2020-09-03 15:13:05.892 Error: (SmartPlug-1) Error Line details not available.

Am I missing a step here? Of can someone point me in the right direction to get this solved?

Re: Python Plugin: TUYA

Posted: Thursday 03 September 2020 15:36
by Xenomes
You need to Install a python Library 'requests'.

Code: Select all

pip3 install requests