Tuya Device Help

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
1 Like

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.

1 Like

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).

1 Like

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?

1 Like

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

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

1 Like

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…?

has anyone managed to get Tuya switch working yet?

I just got it working. 
My first wall socket plug from [email protected]
But it does not update status when changed locally

1) Install efamilycloud version 1.0.7 (found on google search - not the play store one)  
2) Setup adding the socket
3) Install the TuyaKey Grab from https://github.com/bobalob/TuyaKeyGrab
4) Get the key and id.
5) Config Hass as bellow:

platform: tuya
local_key: xxxxxxxxxxxxxxxxx
device_id: xxxxxxxxxxxxxxxxxxx
    friendly_name: Tuya01
    id: 1

Do you get status update when manual change switch status?
I get updates almost 1 minutes after changing…

Today It stopped working…
And I got a message from Google Play Store telling the EFamilyCloud was updated to the last version…

I can not say that was the cause, but now I installed again the 1.0.7, got another key and it is working again. Blocked Play Store updates for that app and lets see what happen!!!

I followed your instruction and instruction on sean6541’s github page

it didn’t work. the trace back is as follows

2018-07-09 00:00:00 ERROR (MainThread) [homeassistant.core] Error executing service <ServiceCall switch.turn_on: entity_id=['switch.hlight']>
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/core.py", line 1021, in _event_to_service_call
    await service_handler.func(service_call)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/switch/__init__.py", line 109, in async_handle_switch_service
    await switch.async_turn_on()
  File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/switch/tuya.py", line 132, in turn_on
    self._device.set_status(True, self._switchid)
  File "/home/homeassistant/.homeassistant/custom_components/switch/tuya.py", line 96, in set_status
    return self._device.set_status(state, switchid)
  File "/srv/homeassistant/lib/python3.5/site-packages/pytuya/__init__.py", line 294, in set_status
    data = self._send_receive(payload)
  File "/srv/homeassistant/lib/python3.5/site-packages/pytuya/__init__.py", line 167, in _send_receive
    data = s.recv(1024)
socket.timeout: timed out

Try this mate, I have tuya sockets and wrote a mini guide. Happy to help where I can