Like many of you, I am using hassio to set up as much local control of my devices as I can. Because of that, I didn’t want to use the built in Tuya integration. Figuring out how to get this to work has been a challenge, partly because of the conflicting information between self-installed HA, Hassio, and Haspbian. Thanks to a bunch of folks on these forums, I finally got my pair of Tuya switches working locally as a custom device. I figured I should post what I did to get them working in case my route can help someone else. Thanks to everyone who has shared information on this topic. Apologies for the lack of links, I can’t currently post more than 2 links. I’ll update this post should that ever change.
This guide describes how I got a pair of TanTan Smart Sockets (B071LLWLLP at Amazon) working with local control on a Pi based Hassio install.
Getting the device_id and local_key values
Note, you’ll need to match your id/key pair to your device’s IP address. If you haven’t already set your devices up in an app (eg: Smart Life), set them up and perform the following steps one at a time to keep the data organized. Otherwise you will have to use trial-and-error to match the IP up to the IDs.
I used the Bluestacks android emulator to set up my switches in the Smart Life app. This made it easy to root and get the values. I highly recommend following HolgiHab’s guide for this (note, that links to the openhab community forum). To summarize the guide, you will:
- Install Bluestacks Tweaker
- Install Bluestacks (find a link on the Bluestacks Tweaker page)
- Root Bluestacks: BmB8I63ea-o at YouTube
- Install SmartLife version 3.6.1 (newer versions seem to be hiding the key and localKey data we need)
- Install ES File Explorer
- Navigate to the app’s data directory (in my case, /data/data/com.tuya.smartlife/shared_prefs
- Open preferences_global_key<numbers and characters>.xml
- Search for your ids and keys
- Write these down along with each device’s IP
Set up the custom device type
You’ll need both tuya.py and it’s dependency, pytuya. I wasn’t able to get pytuya installed via pip, so ended up dropping it in locally and modifying tuya.py to look for it there. I’ve created a zip containing the appropriate custom component files and directory structure. Be smart and scan the zip file before you use it. You can do the following via ssh, samba, or Configurator.
- Download the zip file
The structure of the zip is:
switch/
pytuya/
__init__.py
mytuya.py
- Extract the contents of the zip to the /config/custom_components directory
- You may need to create the custom_components directory
- The resulting directory structure should look like this:
config/
custom_components/
switch/
pytuya/
__init__.py
mytuya.py
Configure your switches
In /config/configuration.yaml, configure your switches
switch:
- platform: mytuya
host: 192.168.1.100
local_key: 321df149e4ff79bf
device_id: 1212d408ba1e7dc0bc7f
name: tuya_01
id: 1
optimistic: 1
- platform: mytuya
host: 192.168.1.101
local_key: 57f9c957db5909d7
device_id: 75d262abb4366d5521ef
name: tuya_02
id: 1
optimistic: 1
Restart
Finally, perform a config check, and then restart hassio. With any luck, your newly defined switches should show up and be functional!
Back-up!
Now that you’ve finally got this working (hopefully), don’t forget to back up your config at hassio.local/hassio/snapshots!
Edit 2019-02-27:
Release 0.88 broke the custom tuya integration. It appears that the custom component (“tuya” in this case) can no longer conflict with built in components (“tuya” in this case) even if they are unused. To solve this, you just need to rename config/custom_components/switch/tuya.py to config/custom_components/switch/mytuya.py, and update the “platform” key to “mytuya” in your configuration.yaml. I have updated the tutorial to reflect the change.
Also, the Tuya SmartLife app no longer stores the key and localKey data in the preferences_global_key* file. I wasn’t able to find a new location. I recommend you use an apk mirror to download an older version. I found that SmartLife 3.6.1 works fine.
The zip file has been updated with the new filename.