Python Plugin: TUYA

Python and python framework

Moderator: leecollings

Post Reply
itguy78
Posts: 12
Joined: Friday 10 May 2019 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 12184
Contact:

Python Plugin: TUYA

Post 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
Attachments
TuyaPlugin.png
TuyaPlugin.png (299.24 KiB) Viewed 33857 times
nico4sen
Posts: 6
Joined: Saturday 15 February 2020 13:41
Target OS: -
Domoticz version:
Contact:

Re: Python Plugin: TUYA

Post by nico4sen »

Will this also work when using the SmartLife app?
User avatar
sincze
Posts: 1299
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Python Plugin: TUYA

Post 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
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.
itguy78
Posts: 12
Joined: Friday 10 May 2019 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 12184
Contact:

Re: Python Plugin: TUYA

Post 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)
itguy78
Posts: 12
Joined: Friday 10 May 2019 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 12184
Contact:

Re: Python Plugin: TUYA

Post 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.
itguy78
Posts: 12
Joined: Friday 10 May 2019 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 12184
Contact:

Re: Python Plugin: TUYA

Post 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.
nico4sen
Posts: 6
Joined: Saturday 15 February 2020 13:41
Target OS: -
Domoticz version:
Contact:

Re: Python Plugin: TUYA

Post by nico4sen »

It works. Nice, thx
DarkG
Posts: 89
Joined: Friday 15 September 2017 18:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10076
Location: Germany
Contact:

Re: Python Plugin: TUYA

Post by DarkG »

Is there a way to change temperature for my AC? I can only turn on and off.
RPi4 Shelly1 Shelly2.5 ESPEasy Tuya Domoticz Beta Dashticz 3.6
itguy78
Posts: 12
Joined: Friday 10 May 2019 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 12184
Contact:

Re: Python Plugin: TUYA

Post 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.
DarkG
Posts: 89
Joined: Friday 15 September 2017 18:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10076
Location: Germany
Contact:

Re: Python Plugin: TUYA

Post 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
RPi4 Shelly1 Shelly2.5 ESPEasy Tuya Domoticz Beta Dashticz 3.6
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Python Plugin: TUYA

Post 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.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
Xenomes
Posts: 379
Joined: Tuesday 27 November 2018 19:05
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Python Plugin: TUYA

Post 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
HP T630 (32GB SSD/8GB Mem) - Ubuntu 22.04.4 LTS (64Bit) - Domoticz 2024.7 with Machinon theme - RFLink - KaKu - Sonoff - Tasmota - Shelly - MQTT2Zigbee - OpenTherm Gateway - Tinytuya - IR Blaster - P1 Smart Meter - NPN Watermeter - Google Assistant
itguy78
Posts: 12
Joined: Friday 10 May 2019 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 12184
Contact:

Re: Python Plugin: TUYA

Post 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 33276 times
User avatar
Xenomes
Posts: 379
Joined: Tuesday 27 November 2018 19:05
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Python Plugin: TUYA

Post 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 33248 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
HP T630 (32GB SSD/8GB Mem) - Ubuntu 22.04.4 LTS (64Bit) - Domoticz 2024.7 with Machinon theme - RFLink - KaKu - Sonoff - Tasmota - Shelly - MQTT2Zigbee - OpenTherm Gateway - Tinytuya - IR Blaster - P1 Smart Meter - NPN Watermeter - Google Assistant
itguy78
Posts: 12
Joined: Friday 10 May 2019 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 12184
Contact:

Re: Python Plugin: TUYA

Post 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!
User avatar
Xenomes
Posts: 379
Joined: Tuesday 27 November 2018 19:05
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Python Plugin: TUYA

Post 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!")
HP T630 (32GB SSD/8GB Mem) - Ubuntu 22.04.4 LTS (64Bit) - Domoticz 2024.7 with Machinon theme - RFLink - KaKu - Sonoff - Tasmota - Shelly - MQTT2Zigbee - OpenTherm Gateway - Tinytuya - IR Blaster - P1 Smart Meter - NPN Watermeter - Google Assistant
cobra045
Posts: 46
Joined: Friday 12 February 2016 20:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Contact:

Re: Python Plugin: TUYA

Post 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
User avatar
Xenomes
Posts: 379
Joined: Tuesday 27 November 2018 19:05
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Python Plugin: TUYA

Post 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
HP T630 (32GB SSD/8GB Mem) - Ubuntu 22.04.4 LTS (64Bit) - Domoticz 2024.7 with Machinon theme - RFLink - KaKu - Sonoff - Tasmota - Shelly - MQTT2Zigbee - OpenTherm Gateway - Tinytuya - IR Blaster - P1 Smart Meter - NPN Watermeter - Google Assistant
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Python Plugin: TUYA

Post 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?
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
User avatar
Xenomes
Posts: 379
Joined: Tuesday 27 November 2018 19:05
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Python Plugin: TUYA

Post by Xenomes »

You need to Install a python Library 'requests'.

Code: Select all

pip3 install requests
HP T630 (32GB SSD/8GB Mem) - Ubuntu 22.04.4 LTS (64Bit) - Domoticz 2024.7 with Machinon theme - RFLink - KaKu - Sonoff - Tasmota - Shelly - MQTT2Zigbee - OpenTherm Gateway - Tinytuya - IR Blaster - P1 Smart Meter - NPN Watermeter - Google Assistant
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest