Skip to content
This repository has been archived by the owner on Oct 7, 2021. It is now read-only.

Tuya switch component works, but the toggle does not keep its state #3

Closed
niladam opened this issue Aug 16, 2019 · 26 comments · Fixed by #4
Closed

Tuya switch component works, but the toggle does not keep its state #3

niladam opened this issue Aug 16, 2019 · 26 comments · Fixed by #4

Comments

@niladam
Copy link
Contributor

niladam commented Aug 16, 2019

Hello. I'm merely mirroring the issue #25992 that i posted on the home-assistant's issues as i think linking between them might catch more attraction to this.

Also, it appears that i'm not the only one affected by this according to this post on HA community.

EDIT: Ooops. I totally forgot to thank you for your work! -- what a jackass i am!

--- BEGIN COPY ---

Home Assistant release with the issue:
0.97.2

Last working Home Assistant release (if known):
Unknown.

Operating environment (Hass.io/Docker/Windows/etc.):
Ubuntu, 18.04.

Component/platform:
https://www.home-assistant.io/components/tuya/

Description of problem:
The switching on/off works, but the state of the toggle gets reset (eg: I turn on the light, the light gets turned on, but the toggle reverts to the off state)

Problem-relevant configuration.yaml entries and (fill out even if it seems unimportant):

tuya:
  username: phone_number
  password: password
  country_code: countrycode
  platform: smart_life

Traceback (if applicable):
none.

Additional information:

No errors in any logs.

@chetcuti
Copy link

chetcuti commented Aug 16, 2019

I'm having the exact same issue with Tuya switches (Tuya bulbs seem to be working fine) which started about 30 hours ago. When I start HA everything displays properly in the UI, but then slowly every Tuya switch toggle turns off, and the states section of the UI shows them as off, even though the actual switches themselves stay on. If I check the Tuya app the states are correct. Here's a screencast of what happens after a reboot: https://www.screencast.com/t/c0pC9CnUmxx

I first started experiencing this on Hass.io v0.95.4 running in a Proxmox VM (which had previously been working fine for over a month and a half). I then upgraded to v0.97.2 and the problem persisted. I then tried installing v0.97.2 on a completely new Proxmox VM and only set up two Tuya switches for testing purposes, and the problem remained. I also tried using the VM file from the recommended installation steps and used that on my local computer in VirtualBox, with the same two switches as my previous test, and yet again this odd problem persisted.

As a test I turned on debug logging and restarted HA, and after everything started up, about 25 seconds later the two Tuya switch toggles shut themselves off, and these entries were recorded in the log.

2019-08-16 11:30:30 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=switch.03487135cc50e3cb9eaa_1, old_state=<state switch.03487135cc50e3cb9eaa_1=on; friendly_name=Switch 1 @ 2019-08-16T11:29:59.264659-07:00>, new_state=<state switch.03487135cc50e3cb9eaa_1=off; friendly_name=Switch 1 @ 2019-08-16T11:30:30.686297-07:00>>

2019-08-16 11:30:30 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.140484409570768] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=switch.03487135cc50e3cb9eaa_1, old_state=<state switch.03487135cc50e3cb9eaa_1=on; friendly_name=Switch 1 @ 2019-08-16T11:29:59.264659-07:00>, new_state=<state switch.03487135cc50e3cb9eaa_1=off; friendly_name=Switch 1 @ 2019-08-16T11:30:30.686297-07:00>>}

2019-08-16 11:30:31 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=switch.03487135cc50e3cb9eaa_2, old_state=<state switch.03487135cc50e3cb9eaa_2=on; friendly_name=Switch 2 @ 2019-08-16T11:29:59.265471-07:00>, new_state=<state switch.03487135cc50e3cb9eaa_2=off; friendly_name=Switch 2 @ 2019-08-16T11:30:31.370631-07:00>>

2019-08-16 11:30:31 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=group.all_switches, old_state=<state group.all_switches=on; entity_id=('switch.03487135cc50e3cb9eaa_1', 'switch.03487135cc50e3cb9eaa_2'), order=0, auto=True, friendly_name=all switches, hidden=True @ 2019-08-16T11:29:59.268424-07:00>, new_state=<state group.all_switches=off; entity_id=('switch.03487135cc50e3cb9eaa_1', 'switch.03487135cc50e3cb9eaa_2'), order=0, auto=True, friendly_name=all switches, hidden=True @ 2019-08-16T11:30:31.371626-07:00>>

2019-08-16 11:30:31 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.140484409570768] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=switch.03487135cc50e3cb9eaa_2, old_state=<state switch.03487135cc50e3cb9eaa_2=on; friendly_name=Switch 2 @ 2019-08-16T11:29:59.265471-07:00>, new_state=<state switch.03487135cc50e3cb9eaa_2=off; friendly_name=Switch 2 @ 2019-08-16T11:30:31.370631-07:00>>}

2019-08-16 11:30:31 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.140484409570768] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=group.all_switches, old_state=<state group.all_switches=on; entity_id=('switch.03487135cc50e3cb9eaa_1', 'switch.03487135cc50e3cb9eaa_2'), order=0, auto=True, friendly_name=all switches, hidden=True @ 2019-08-16T11:29:59.268424-07:00>, new_state=<state group.all_switches=off; entity_id=('switch.03487135cc50e3cb9eaa_1', 'switch.03487135cc50e3cb9eaa_2'), order=0, auto=True, friendly_name=all switches, hidden=True @ 2019-08-16T11:30:31.371626-07:00>>}

Additional Notes:

  • External triggers such as dash buttons, API calls from custom scripts, etc. all trigger the switches properly, however the state in HA still says off, and the toggle stays off
  • Someone on Discord had me run the Tuya force update service but it didn't change anything
  • Someone on Discord suggested I try updating the Tuya firmware but no updates were available
  • I've tried using a completely separate Tuya account, in case it was an issue with my specific Tuya account (corruption, etc.), with nothing but these two switches added, and the results were the same

@niladam
Copy link
Contributor Author

niladam commented Aug 16, 2019

@chetcuti - thank you for your really good report! I'm hoping someone picks this up and we'll be able to sort it out.

IMHO, to me it sounds like this is an API change and/or modification..

@slim0287
Copy link

slim0287 commented Aug 18, 2019

I have this same issue, but only with Tuya smart plugs with multiple sockets. When I change an outlet to "on," the outlet turns on but a few seconds later the Home Assistant representation of the switch state turns back to off. The outlet remains on. I'm using version 0.97.2 and I suspect it has something to do with a firmware update to the smart plug itself rather than some change in Home Assistant. The single socket smart plugs work fine. I really do hope the keepers of the tuya component in home assistant can address the issue.

@niladam
Copy link
Contributor Author

niladam commented Aug 18, 2019

@slim0287 - actually, the discussion is waaay longer and it appears that it's not just the dual socket switches but other type of appliances as well. The discussion (#25992) is longer if you want to have a look on the main home assistant issues tracker.

@slim0287
Copy link

slim0287 commented Aug 18, 2019

@niladam Thanks, I left basically the same comment over on thread #25992 as well.

@niladam
Copy link
Contributor Author

niladam commented Aug 18, 2019

@slim0287 - yeah. I just noticed that after i posted my reply. Please ignore it :)

@RXM307
Copy link

RXM307 commented Aug 21, 2019

Looks like the API has changed looking at https://docs.tuya.com/en/cloudapi/device_access.html the Cloudurl in the code points to https://px1.tuyaus.com/homeassistant/auth.do (access.do & skill) Which appear to be getting HTTP 404 instead of https://a1.tuyacn.com/api.json) @polyedre over at polyedre/tuya-lan looks to be having some success accessing the new API's

@PaulAnnekov
Copy link
Owner

I will explain you what happened. TL;DR tuya has broken their Home Assistant API, but I know to make a workaround. Estimation: next week.

When you toggle your tuya switch via HA UI, this lib sends a turnOnOff command to https://px1.tuya[country code].com/homeassistant/skill. After that HA requests state update (https://github.com/PaulAnnekov/tuyaha/blob/master/tuyaha/devices/base.py#L39) and tuyaha sends a QueryDevice command to the same url. In response it gets: {..., "data": {"online": true, "state": true}, ...}. But several days ago it started to return {..., "data": {"online": true}, ...}. As you can see, state has disappered. Now UI can't determine the state and shows it as off (https://github.com/PaulAnnekov/tuyaha/blob/master/tuyaha/devices/switch.py#L8).

As Tuya ignores our emails, it's nearly impossible to make them fix this bug. As a workaround, on update I will ask for Discovery (https://github.com/PaulAnnekov/tuyaha/blob/master/tuyaha/tuyaapi.py#L108). This command returns a list of all devices with the same data field, but it still contains state 🎊 .

@RXM307 tuya created a special API for Home Assistant long time ago. It's still working, but they broke it a bit recently. https://a1.tuyacn.com/api.json is an API for OEMs and it's hard to use it by end-user. Just check how to get credentials to authorize https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md 😱.

@slim0287
Copy link

Paul,
First off, I greatly appreciate you taking the time to sort out a fix. Please excuse the uninformed question, when you write "As a workaround, on update I will..." does this mean we'll have it fixed in HA 0.97.3?

@tbkon3
Copy link

tbkon3 commented Aug 22, 2019

Hassio 0.97.2

Same for me,
i do a lot of test and i see that:
if i remove component,
reboot hassio,
reconfigure component,
reboot hassio,
all devices takes the correct states for about 10/15 seconds and then they come back to be "off"

@aidbish
Copy link

aidbish commented Aug 22, 2019

Ha beta 0.98 has just been released and will go into production 1week later. The Dev needs to get the pr in soon

@niladam
Copy link
Contributor Author

niladam commented Aug 22, 2019

@PaulAnnekov - thank you for your work buddy! I managed to find a really quick and dirty fix for this and i've created a pull request for it.

The solution has been posted onto the initial HA issue.

@OptimusGREEN
Copy link

OptimusGREEN commented Aug 24, 2019

For anyone using hassio the easiest solution is to download the new version from here and extract it. Inside there is a tuya folder.

in your config directory, create a custom_components directory if you don't already have one and copy the extracted tuya folder over to custom_components and restart. that is it.

@BreBar07
Copy link

@OptimusGREEN I have tried your solution for hassio and doesn’t work for me...

@OptimusGREEN
Copy link

OptimusGREEN commented Aug 24, 2019

@OptimusGREEN I have tried your solution for hassio and doesn’t work for me...

image

this is how it looks? make sure its the tuyaha folder and not the tuyaha-0.0.3 one

@chetcuti
Copy link

I'm on Hass.io v0.97.2 and setting this up as a custom component doesn't work for me either. So far the only fix that's worked is @niladam's code here that you replace the contents of /usr/local/lib/python3.7/site-packages/tuyaha/devices/switch.py with.

Unfortunately the above fix doesn't seem to be permanent though, at least not for me. I can restart Hass.io without issue (hassio ha restart), but if I completely shutdown the Proxmox VM that Hass.io runs on and then restart it, I need to redo the fix. I rarely have to shut the VM down though so it's not really a big deal, especially since the next version of Hass.io will have the permanent fix implemented.

@OptimusGREEN
Copy link

sorry, I had posted the wrong link.
the correct link is here

extract the tuya folder and copy it into custom_components

@BreBar07
Copy link

@OptimusGREEN champion mine is now working on hassio! Thanks alot!

@slim0287
Copy link

@OptimusGREEN
Your solution works great for hassio on my Pi. Let me ask, when they release HA 0.98, should I delete this custom component before I upgrade? I'm presuming Tuyha will be fixed in HA 0.98.

@OptimusGREEN
Copy link

@OptimusGREEN
Your solution works great for hassio on my Pi. Let me ask, when they release HA 0.98, should I delete this custom component before I upgrade? I'm presuming Tuyha will be fixed in HA 0.98.

it'll use the custom over the standard so you might as well delete it as both will be the same.

@chetcuti
Copy link

Thanks @OptimusGREEN! The new link you posted works perfectly when setup as a custom component.

PaulAnnekov added a commit that referenced this issue Aug 30, 2019
@jasonalsing
Copy link

I was able to get this fixed with the custom component and the official fix in .98 but the "Better Fix" seems to have re-broken my installation on .98.2

Reverted to .98.1 and all is ok.

@slim0287
Copy link

slim0287 commented Sep 4, 2019

I had the same issue with 0.98.2, the tuya component stopped working whereas it worked in 0.98 and 0.98.1. So, I kept 0.98.2 but I put a custom_component back in to fix tuya.

@Martin781
Copy link

Same here. It doesn't retain the actual state always shows off with 0.98.2. The switch itself toggles though.

@PaulAnnekov
Copy link
Owner

Reason: home-assistant/core#26373.

@zolabus
Copy link

zolabus commented Sep 21, 2019

where can i find the folder to modify? I cannot in the path specified

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.