It wasn't easy and it isn't pretty, but I now finally have LOCAL network control of all my Tuya Smart Plug devices via Indigo. I thought I would share the details in case somebody finds this useful. Hopefully this will do until a Tuya device plug-in is developed.
1. Setup and register your Tuya smart plug using the Smart Life or Jinvoo Smart phone app. This step is required.
You will then have to obtain the Device ID, Local Key, and IP address for your Tuya device(s). The Device ID and IP address are easy to obtain, but obtaining the Local Key is more complicated.
2. To obtain the Device ID, open your Smart Life or Jinvoo Smart phone app, click on the device, click the edit icon at the top right (looks like a pencil), and then click Device Information. Your device ID can be found there (called Virtual ID).
3. Next you will need to find your Tuya device's local IP address. The IP address listed in the phone app is the WAN address. This is of no use. To obtain your local IP address, copy your device's Mac Address listed in the app, and then go to your router to find the local address. You will need to configure your router to use a
static IP address (NOT randomly assigned by your DHCP server) for your Tuya device(s).
4. Now for the hard part of obtaining your device Local Key. You will need these installed on your Mac: CLI, XCode, Homebrew, Node and NPM
CLI: You can install it using the package installer available here:
https://docs.cloudfoundry.org/cf-cli/install-go-cli.html.
Homebrew: You can install it by pasting this line in a Terminal window and pressing return:
- Code: Select all
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
XCode: You can get this from the Apple app store. Howevever, you probably won't need to because Homebrew will offer to install it for you if you don't already have it. Be patient as it runs. It will take a few minutes.
Node and NPM: After installing Homebrew, simply copy and paste this line in a Terminal window and press return:
- Code: Select all
brew install node
Next, go through all 9 steps described here:
https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md. The process involves making your phone communicate through a proxy server running on your Mac. You will access the Tuya cloud via this proxy server using your Smart Life or Jinvoo Smart app. The proxy server will then intercept and identify your Local Key(s)!
5. Once you have the Device ID, IP address, and Local Key for your Tuya device(s), you will then need to install pytuya (the Python Tuya code) on the computer running your Indigo server. Simply copy and paste this command in a Terminal window:
- Code: Select all
pip install pytuya
6. Now create an Indigo Action Group to control your Tuya device. It should set to run embedded Python code. Here is the code to turn on your Tuya device:
- Code: Select all
import pytuya
d = pytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE')
data = d.status() # NOTE this does NOT require a valid key
# print('Dictionary %r' % data)
# print('state (bool, true is ON) %r' % data['dps']['1']) # Show status of first controlled switch on device
# Turn on switch
data = d.set_status(True)
if data:
print('set_status() result %r' % data)
If you want to turn your device OFF, simply replace the "True" command with "False" at line 7.
You can also toggle your device using this code:
- Code: Select all
import pytuya
d = pytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE')
data = d.status() # NOTE this does NOT require a valid key
print('Dictionary %r' % data)
print('state (bool, true is ON) %r' % data['dps']['1']) # Show status of first controlled switch on device
# Toggle switch state
switch_state = data['dps']['1']
data = d.set_status(not switch_state) # This requires a valid key
if data:
print('set_status() result %r' % data)
# on a switch that has 4 controllable ports, turn the fourth OFF (1 is the first)
data = d.set_status(False, 4)
if data:
print('set_status() result %r' % data)
print('set_status() extrat %r' % data[20:-8])