core: Tuya Integration apparently fails to renew cloud access token
The problem
This is an attempt to pull together information from a number of duplicate reports: #61820, #61854, #61903, #61907, #62229 and #62318
Tuya devices are not updating their status from the cloud. Control of devices through automations and scripts still works. Regular error reports of disconnection appear in the logs (approximately at 2 hourly intervals). Reloading the integration solves the issue until the next disconnection.
@simie7 has shown failure reported in debug logs to renew token is causing integration to regularly disconnect from the cloud here
@bpfoster has observed same issue while logging the openmqtt method
@matfra has traced the issue through packet capture between HA device and Tuya ELB endpoint in us-west-2
@sharoltd and @eduspina have shown that problems started on the Tuya cloud side here and here. and that the problems are therefore only coincidental with recent HA updates
Reporting the issue directly to Tuya has been unsuccessful, in as far as the advice is ‘reboot and reconnect. Keep the network open’ (whatever that means)
In the meantime, @cvroque has proposed a workaround automation to periodically reload the integration, with slight variations suggested by @ResteNarquois and another by @simie7
What version of Home Assistant Core has the issue?
All versions since 2021.11.5 (but this is not the problem)
What was the last working version of Home Assistant Core?
No response
What type of installation are you running?
Home Assistant OS
Integration causing the issue
Tuya
Link to integration documentation on our website
https://www.home-assistant.io/integrations/tuya
Example YAML snippet
## Using this automation to periodically reload the integration acts as a workaround
## Add this to configuration.yaml
system_log:
fire_event: true
## Automation
- id: 'automation_id'
alias: 'Check and Reload Tuya Integration'
trigger:
- platform: event
event_type: system_log_event
event_data:
level: ERROR
name: tuya_iot
condition:
- condition: template
value_template: >-
{{ "error while get mqtt config" in trigger.event.data.message[0] }}
action:
- service: homeassistant.reload_config_entry
data:
entity_id:
- [tuya.entity_id]
Anything in the logs that might be useful for us?
Per @simie7 in https://github.com/home-assistant/core/issues/61854#issuecomment-997939164:
2021-12-20 15:33:19 DEBUG (Thread-6) [tuya_iot] Request: method = POST, url = https://openapi.tuyaeu.com/v1.0/iot-03/users/token/*************************, params = None,
body = None, t = 1640007199566
2021-12-20 15:33:19 DEBUG (Thread-6) [tuya_iot] Response: {
"code": 1011,
"msg": "token invalid",
"success": false,
"t": 1640007199773
}
2021-12-20 15:33:19 DEBUG (Thread-6) [tuya_iot] Request: method = POST, url = https://openapi.tuyaeu.com/v1.0/iot-03/open-hub/access-config, params = None, body = {'
uid': '***', 'link_id': '***', 'link_type': 'mqtt', 'topics': 'device', 'msg_encrypted_version': '1.0'}, t = 1640007199788
2021-12-20 15:33:19 DEBUG (Thread-6) [tuya_iot] Response: {
"code": 1010,
"msg": "token invalid",
"success": false,
"t": 1640007199809
}
2021-12-20 15:33:19 DEBUG (Thread-6) [tuya_iot] Request: method = POST, url = https://openapi.tuyaeu.com/v1.0/iot-01/associated-users/actions/authorized-login, params = None,
body = {'username': 'login@gmail.com', 'password': '***', 'country_code': '3xx', 'schema': 'tuyaSmart'}, t = 1640007199826
2021-12-20 15:33:19 DEBUG (Thread-6) [tuya_iot] Response: {
"result": {
"access_token": "***",
"expire_time": 7200,
"platform_url": "https://openapi.tuyaeu.com",
"refresh_token": "***",
"uid": "***"
},
"success": true,
"t": 1640007199922
}
2021-12-20 15:33:19 ERROR (Thread-6) [tuya_iot] error while get mqtt config
2021-12-20 15:33:19 INFO (MainThread) [homeassistant.components.automation.new_automation] Tuya: reload integration on error: Running automation actions
Additional information
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 25
- Comments: 37 (4 by maintainers)
The PR I submitted to fix this in the tuya SDK has not been merged, so this would not be fixed yet. I have no idea whether this is the “right” fix, but I’ve been running this patched code for 24 hours now with no issues and the problems described by these tickets have gone away.
I’ve tried to engage with Tuya support but that has been less than fruitful.
Hi guys, sorry for the inconvenience! We have addressed the issue and will release a new version of tuya-iot-python-sdk soon. Thanks @bpfoster for creating the PR, we are double checking and testing the new version. Will inform you once it’s released.
Path in the container: /usr/local/lib/python3.9/site-packages/tuya_iot/openapi.py
Changes: https://github.com/tuya/tuya-iot-python-sdk/pull/51/files
This probably isn’t the best place, but real quick assuming you are running standard raspberrypi install
Apologies if I’ve not followed procedure, but it was a sincere attempt to maintain clarity within what is becoming a very scattered discussion - can you not close all the others and keep this one open?
You know best of course - I’m guessing this integration is causing you more grief than you would care for, and we understand that it is not really an HA issue…
Had me stumped for a while. I’m running the standard HA OS on a Pi. Had the ‘Terminal & SSH’ add-on. Realised you actually need the ‘SSH & Web Terminal’ which is a more enhanced community add-on allowing some access to host stuff. Using that (and disabling protection mode), I was able to access the files needed and make the tweak to the file within the SSH & Web Terminal add-on.
Sorry noob here. Im running the Home Assistant OS, and I couldnt find the file “tuya_iot/openapi.py” or “tuya_iot/openmq.py” . I have the Tuya integration installed, and the error I’m getting is “Source: /usr/local/lib/python3.9/site-packages/tuya_iot/openmq.py:161”. Somehow I cant find the file in terminal. Thank you.
Thanks so much for doing this. I’ve been running with your patched code a well and so far it works great.
Nope still not working properly for me.
I think it’s already fixed,
but the developer referenced every ticket except this one 😃https://github.com/tuya/tuya-iot-python-sdk/pull/51
I am experiencing the same issue again on HA 2021.12.10 Light bulbs’ status get out of sync in HA but the issue gets fixed after HA/TUYA integration reboot/reload.
I think the fix was merged in 2021.12.6 - seems to be OK for me
Once applied the patch, the problem is resolved.
Thank you so much!
A bit of an effort (for me) to apply the patch but works a treat and was totally worth it, particularly as I don’t really trust Tuya to action anything quickly - if at all. Thanks folks for the really useful guidance!
Was able to apply the patch after everyone’s help. Looks like its working for me. Thank you all !!
Cheers, this really helped
Thank you for this !!
Similar issue, I have the terminal installed and the file editor, but they connect to a shell that cant access the files mentioned here. Can anyone link or explain how to connect to the “session” where these files live ? is this the supervisor layer? so I cant edit this file whilst waiting for this to rollout. I’ve googled and googled, but have you any idea how many articles there are on HA 😃
Quick question @bpfoster, I am running HA on a docker, so dropped into a shell on the container and edited the /usr/local/lib/python3.9/site-packages/tuya_iot/openapi.py, I guess this change won’t persist a container restart, right? And do you think it would work without restarting it? btw, thanks for this patch!!!
I had restarted HA from the GUI and will find out if it worked in the next 20 minutes:
UPDATE: I guess it worked!!!
Thanks a lot!!!
So the solution to resolving all the issue is to open another issue? 🤷♂️
I’m kinda tempted to close it as a duplicate.