Tuya switch

I was able to add a generic switch that I operate with the tuya app using https://github.com/sean6541/tuya-homeassistant. Using services in the developer tools I’m able to turn the switch on and off, but when I use the toggle in the main overview in hassio the switch will turn on, but the state doesn’t change to on. So, I can’t turn it off.

Is there a way to resolve this? Perhaps a way to change the switch toggle in the overview to on/off buttons or something I can add to the config? I’m a newbie :slight_smile: Thanks for any suggestions.

switch:

  • platform: tuya
    name: test
    host: 192.168.1.111
    local_key: 11e88a2087b9058d
    device_id: 022003915ccf7f75d70b
2 Likes

any luck with this? i was able (i think) to find my local key & device id, but haven’t been able to get HA to find it. i created a “home assistant\custom_components\switch” folder and added the tuya.py. but no luck. should have just got the actual Sonoff ones so that i could flash them.

The .py file goes in:

/home/homeassistant/.homeassistant/custom_components/switch/

I was able to extract the key using the Jinvoo smart app with a rooted tablet. There is a python script to extract the data posted here:

Other ways of getting key can be found here:

Got it working, but update status (when changed manually) is taking more than 1 minutes sometimes…

Got it working in HASS using this configuration:

platform: tuya
host: 192.168.1.172
local_key: xxxxxxxxxxxxxxxxx
device_id: xxxxxxxxxxxxxxxxxxx
switches:
  switch1:
    friendly_name: Tuya01
    id: 1

With the guidance above, I got the tuya switch working on a “Greatever Mini Wireless smart socket”, https://www.amazon.com/gp/product/B077ZTQGBX/ref=oh_aui_detailpage_o02_s00

The tuya switch only has 1 outlet; in configuration.yaml, I had to use

switch:
  - platform: tuya
    host: xxx.xxx.xxx.xxx
    local_key: xxxxxxxxxxxxx
    device_id: xxxxxxxxxxxxx
    name: Tuya01

A couple other items that might be of help:

  • to find the local_key, I used the traffic monitor of my router. If your router supports traffic monitoring, assign the tuya switch a static IP address based on its MAC address (the Jinvoo Smart app lists it under device info; you should also be able to find it in the DHCP client list). Enable monitoring of all traffic from that IP address.
  • the step in other instructions to “add your Tuya device…” is important, as the local_key appears to be only transmitted in the initial registration. If needed, remove the tuya switch from your app, reset it, so it has to be added again while the monitor is running.
  • As this was the first custom component I ever added, I was a tad overwhelmed with the instructions. /home/homeassistant/.homeassistant/custom_components/switch/tuya.py was the correct location for my setup.

A big thank you to the community to help enable these devices.

@agmerk
To get a local key, I used Bluestack and installed SmartLife app on it. Then, we can follow Android method to retrieve keys, which is much easier than other methods :slight_smile:
The location you mentions is correct place to put tuya.py

Cheers

If you are root on you android, you can look inside the file

dev_data_storage.xml

located in:
/data/data/com.tuya.smartlife/shared_prefs/

All data are written here.

Hey

I just want to say THANK YOU for such helpful tip.

This is something much easier for me. All other method require me to install software, register everything to a particular version of an app, etc.

I can get most updated info here.

One silly question. how do you copy the file from android machine to local PC?

adb pull

doesn’t work for protected file. So far, I painstakingly copied them by hand. I just wish there is a better way to do this.

thanks in advance

I simply mount the fs of the mobile phone on my PC, then copy/paste!
Of course, I am root on the phone.

would you spoon feed me on this one?

I used to use ftpfs and mount it on my linux.

how do you mount your mobile phone?

I simply plug the phone to USB, selecting MTP protocol on the phone and my linux mint mounts it.

1 Like

Have you seen the new HA component?

just found out that today (prior to your post). thank you.

Dear all:

After 6 months of struggling (literally), I FINALLY got tuya-based switch to work.

The following is instruction to get it work:

1) For early adapter, or those who tried so hard to trying to get tuya-based switch to work in the past, the very first step is to make sure you REMOVE the custom tuya components you at one time downloaded from

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

As of Home Assistant 0.74.0, tuya component is built in. You absolutely need to remove the custom component or entire HA is going to shorten your life span.

2) get the device ID and local keys. There are plenty of post show you how to do this, different methods can be used. The method I used is the one posted on this thread by boggiano simply because I rooted all my phones so this wasn’t a problem. All the info is in the following file in your android phone if you installed “Smart Life” app:

/data/data/com.tuya.smartlife/shared_prefs/dev_data_storage.xml

There are a couple pitfalls to watch out for. First, there are many information out there, the two absolutely needed are devId and localKey Let me repeat this, what you are looking for is devId, AND localKey . There are other attributes such as uuid and key. And for our purpose, they only exist to confuse the weak minds. Second pitfall is that this localKey do changes if you decided to switch apps to control tuya-based devices (exactly when the localKey changes is something I still don’t exactly know). Since there are plenty of apps (efamilyCloud, Smartlife, tuya, jimvoo, etc) that control tuya, I recommend you stick with ONE app to avoid localkey being changed without your knowledge.

3) Once you have all the deviceID and localKeys ready, you need to put these into your configuration.yaml file:

tuya: 
  username: XXX
  password: YYY
  country_code: ZZ

In the switch section, there are two formats which both work:

switch: 
  - platform: tuya
    host: your_local_IP_address
    local_key:  yourKey
    device_id:  yourDevID
    friendly_name:  switchName
    optimistic: 1  //  I have no idea what is this for
    id: 1

The following format would work too: I think this is for those tuya device which has multiple outlet/swtich in one device:

switch: 
  - platform: tuya
    host: yourLoacalIP
    device_id: deviceID
    local_key: localKey
    switches:
      switch1:
        friendly_name: nightLight
        id: 1

That is it.

Good luck

3 Likes

Hello
I’m trying to get the tuya switch working. I followed the directions of the first few posts at top,
I’m running 0.49.1 ver not sure if I can have the custom_components folder on my setup,
If I can is there anything different I have to do for my setup. As HA will fails to load.

follow my instruction. it is relatively completely other than the part about getting the device ID and local key.

Hey Everybody,

First of: A big thank you to all those contributing to this platform that I just discovered yesterday. I have a small issue with the Tuya switches (I use a version from Smart Life). I am using a pre-loaded component in Hassio. I see the switches in my Home Assistant and I can toggle them. The proper switch will change state. However, as soon as it does, the slider in Home Assistant automatically slides back to the previous state. After this the switch no longer responds to HA. It does however still react to Google Assistant and/or the Smart Life app.

Any hints on how to fix this behaviour?

Thanks in advance and all the best!

Update: I have removed these switches from every app (smart life, google home) so only Hass.io has the interface. This does seem to improve behaviour. However it remains unpredictable. Sometimes the switches don’t react and then as soon as I restart Hass.io all commands that hadn’t been executed up to that point are fired in rapid succesion…

Hi,
I was able to access all my Jinvoo Smart plugs by following this guide and adding optional platform as ‘jinvoo_smart’.

hi there!
im new to HA. Therefor i struggle a lot.
I have a tuya switchan i set it up as “kngharv” told.
In my configuration.yaml under “switch:” i have following

  - platform: tuya
    host: 192.168.178.xxx
    local_key: f3a83578e8931xxx
    device_id: 01530667b4e62d517xxx
    switches:
      switch1:
        friendly_name:  schalter
        id: 1

BUT on HASSIO Webinterface there ist no switch visible. What can I do to switch on and off my device from webinterface.

Sorry for my horrible englisch :slight_smile:

I was able to use my tuya power strip via the cloud service but I’d like to keep things local. I was able to grab my local_key and device_id but it looks like HA no longer supports local connectivity. I’m on 0.83.2 and with the following switch settings:

  - platform: tuya
    name: powerstrip
    host: 192.168.1.239
    local_key: 6555asdfasdff02
    device_id: 02200145asdfasdfasdf
    switches:
      switch1:
        friendly_name: 'powersocket 1'
        id: 1
      switch2:
        friendly_name: 'powersocket 2'
        id: 2
      switch3:
        friendly_name: 'powersocket 3'
        id: 3
      switch4:
        friendly_name: 'powersocket 4'
        id: 4

I get the following error:

2018-12-01 23:33:41 ERROR (MainThread) [homeassistant.setup] Error during setup of component tuya
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/setup.py", line 148, in _async_setup_component
    component.setup, hass, processed_config)  # type: ignore
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/tuya.py", line 59, in setup
    username = config[DOMAIN][CONF_USERNAME]
KeyError: 'tuya'