core: Region selection for Tuya integration fails for non US region accounts. And getting "Invalid authentication"
The problem
Tuya integration doesn’t select REGION for uri based on country code, This causes “Invalid authentication” for non US accounts.
Environment
- Home Assistant Core release with the issue: homeassistant/raspberrypi3-homeassistant:2021.1.0
- Last working Home Assistant Core release (if known): homeassistant/raspberrypi3-homeassistant:2021.1.0
- Operating environment (OS/Container/Supervised/Core): raspberrypi3
- Integration causing this issue: Tuya
- Link to integration documentation on our website: https://www.home-assistant.io/integrations/tuya/
Traceback/Error logs
ipdb> s
> /srv/homeassistant/lib/python3.7/site-packages/tuyaha/tuyaapi.py(103)get_access_token()
102 try:
--> 103 response = self._requestSession.post(
104 (TUYACLOUDURL + "/homeassistant/auth.do").format(SESSION.region),
ipdb> l 103
98 self.discover_devices()
99 return SESSION.devices
100
101 def get_access_token(self):
102 try:
--> 103 response = self._requestSession.post(
104 (TUYACLOUDURL + "/homeassistant/auth.do").format(SESSION.region),
105 data={
106 "userName": SESSION.username,
107 "password": SESSION.password,
108 "countryCode": SESSION.countryCode,
ipdb> p SESSION.region
'us'
This causes to hit url https://px1.tuyaus.com/homeassistant/auth.do (US region url) instead of the right url https://px1.tuyaeu.com/homeassistant/auth.do (EU region url)
Additional information
The patch requires some way to manage region at tuyaha API, I have created an issue at their end as well. tuyaha/issues/68
In order to reproduce.
Smartlife App
- Install Smartlife app
- Signup and use the country as “United Arab Emirates”
- Lets say you signed up using “xyzab@mailinator.com” and password “Xyz123”
- Once you entered the verification code that you received at email account you will be signed in.
- Try signing out from the Smartlife app and Sign in again. It works very well.
Home Assistant
- Goto Configuration > Integrations > Add integration > Tuya
- Enter below configuration
username: xyzab@mailinator.com
password: Xyz123
countrycode: 971
app: smart life
- This will always show as “Invalid authentication” even though your password is correct.
Underlying problem
Ideally the URL for tuya should be intelligently selected based on country code. Or we should have a new column for REGION in the input.
Lack of region selection causes to hit url https://px1.tuyaus.com/homeassistant/auth.do (US region url) instead of the right url https://px1.tuyaeu.com/homeassistant/auth.do (EU region url)
Work around
If you are desperately trying to connect your devices and you don’t have time to wait for the patch, Delete existing account in another country, create an account in country “United States”
Please note that If you have an account with apple integration / google integration, it might not work very well with home assistant integration
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 8
- Comments: 15 (2 by maintainers)
I implemented this additional setup option in tuya_custom so you can try this using this custom integration in place of default to see if this solve the issue. I also created a PR in TuyaHA library to implement same change in default integration, but this will require more times to be completed.
Had the same problem with country code 420. Tried setting up US account and invite the other account to my home but found out that you cannot home together accounts from different regions. Didn’t want to repair the devices with the US account so I went with a temporary solution to hardcode the eu region to tuyaha API.
Mine setup is installed RPi3 in docker container but the general path to the API is something like this: /usr/local/lib/python3.8/site-packages/tuyaha/tuyaapi.py
To find the tuyaha API I ran: sudo find / -name “tuyaapi.py”
Then edited the file with: sudo nano /usr/local/lib/python3.8/site-packages/tuyaha/tuyaapi.py
and changed row ~14 from DEFAULTREGION = “us” to DEFAULTREGION = “eu”