Tuya Device Help


Just getting started with Home Assistant & HassIO.
I got my TP-Link plug set up.
Now I’m attempting to get my Tuya plug working.

It doesn’t look like HA supports Tuya but I found someone on GitHub that has something that may work…https://github.com/sean6541/tuya-homeassistant.

In order to set this up you need the LocalKey. I followed the instruction to find it with WireShark but got no results (sorry for the vague description). Has anybody set up a Tuya plug and if so, how did you go about doing it? Can Tuya device be used with HA?


Add Support for Smart Life Components

look for the samething.

Tuya platform is an absolute god send. it allow me to buy plugs from various brand and still using one single app for it.

I end up having 6 of those plugs, but I didn’t know that Home Assistant is not supported.



There is an Android app that gets teh local_key from Tuya devices after connecting them to an app call eFamily Cloud. See https://github.com/bobalob/TuyaKeyGrab for details.


Thanks! Got the localkey and set up my platform but still no luck. None of my tuya device show up. Does the localkey change if you remove and re-add the device? I used my wifes android phone to configure with eFamily and get the localkey then I removed them and re-discovered them on my iPhone Tuya app. I suspect that the localkey may have changed.


thank you so much. I got it


how did you managed to install this tuya-homeassistant on hass.io? my silly question is, where do you put these python scripts?


From: https://github.com/sean6541/tuya-homeassistant

To use, copy tuya.py to “/custom_components/switch” and add config below to configuration.yaml

Config Fields:


  • platform: tuya
    name: //switch name
    host: //ip of device
    local_key: //localKey
    device_id: //devId
    id: //switch id. leave blank if only one switch


Here is my configuration:

  - platform: tuya
    name: harveyLight
    device_id: 00200465c020d62f2d26
    local_key: 261x54ac0z16871f
    id: 3

I still get KeyError: '3'

does it means my key is wrong?


Your local key looks a bit off. There should be no letter larger than ‘f’ because this is a hexadecimal number. Also, I think you need to enclose your data in single quotes. For example…

  • platform: tuya
    name: plug 213
    host: '’
    local_key: 'fec84f7791488588’
    device_id: '01200738ecfabc81f0b3’
    id: ‘1’

Note that I still have not gotten this to work but I do not get the key error.


I’m pretty sure keyError refers to “id”. Important: id is not the “id of the switch” (three switches: 1,2,3), but rather the number of switches per device. Ergo if you have three devices that are all a device with a single plug, they all get “id: 1”. That should resolve the keyErrors for all I know. Note: There are other threads around that pretty accurately describe the process of getting the key.


Yes, the local key changes every time the device is set up again.

Also, apparently the switches I have can only connect to one TCP client at a time and will refuse all other connection requests. That means that when intending to control them with HA, you cannot control them with the app anymore (I uninstalled the app, now it works almost flawlessly).


thank you.

my problem is with that “id” field.

I promptly changed to
id: 1


I got a different error.

But at least I am making progress!

Before I dig further into it, I have a very silly, most basic question to ask:

I notice that I have SOMETHING installed in home assistant directory:

  • /srv/homeassistant/lib/python3.5/site-packages/pytuya-5.0.dist-info
  • /srv/homeassistant/lib/python3.5/site-packages/__pytuya__
  1. what are these things?

  2. now, via GUI trying to turn on a switch, I got the following error:

    File “/home/homeassistant/.homeassistant/custom_components/switch/tuya.py”, line 83, in update
    raise ConnectionError(“Failed to update status.”)
    ConnectionError: Failed to update status.

any idea what to do next?


Were you able to get the “local_key” from the app?

When I install and run the app, it only displays devId: localkey


Same here. I manually searched the 1.abj file and don’t see ‘localKey’


I’ve been able to get HA to update when the light turns on or off both with and without the Tuya app controlling the switch but I cannot get HA to actually control the switch. I get an error in the Tuya.py

Here’s the error:

File “/home/homeassistant/.homeassistant/custom_components/switch/tuya.py”, line 77, in update
status = self._device.status()
File “/srv/homeassistant/lib/python3.5/site-packages/pytuya/init.py”, line 256, in status
data = self._send_receive(payload)
File “/srv/homeassistant/lib/python3.5/site-packages/pytuya/init.py”, line 165, in _send_receive
ConnectionResetError: [Errno 104] Connection reset by peer


you need to install “efamily cloud” app first


Were you able to get this up and running via the raspberry pi or a linux install?


@sebringsc I just recently got Tuya switches working in HA. If you have the Device ID (“devid” in Tuya protocol) and Key (“localkey” in Tuya protocol) then I’d suggest testing control with the pytuya python module (which is underlying this HA Device Add-On).

From memory, pip install pytuya then open a python shell and paste in this code. If successful your outlet should toggle on/off …

import pytuya
device = pytuya.OutletDevice('xxxxxxdevidxxxxxxxxx', '10.x.x.x', 'xxxxxkeyxxxxxxx'); 
dev.set_status(not  device.status()['dps']['1']) 

Note: I got devid/localkey via using Burpsuite Proxy on my PC then pointing iPhone wifi Proxy to PC then registering the Tuya outlet in question.

I have 8 Tuya outlets, configuration.yaml looks like:

- platform: tuya
  name: Tuya1
  local_key: xxxxxxxxxxxxxxxx
  device_id: xxxxxxxxxxxxxxxxxxxx
  id: 1

- platform: tuya
  name: Tuya2
  local_key: xxxxxxxxxxxxxxxx
  device_id: xxxxxxxxxxxxxxxxxxxx
  id: 1


Thanks for the pointers. I was unable to install pytuya via the pip3 command (I get a permissions error). I already have a pytuya.py file in my switch folder in homeassitant but I copy/pasted and created it myself. Coincidentally I seem to be stuck with Python 2.7. I have tried to update it but I get the same permissions error. I also have the most recent release of HA but my Python is still 2.7

As for the import pytuya, i am unable to figure out how to do that. When I SSH to my raspberry pi, I cannot run import pytuya as it gives me an error. I don’t know enough about python to know what I am doing wrong. I also do not know how to open a python shell via SSH. Any pointers on what to do?


can you run it as sudo…?