AlexxIT / SonoffLAN

Control Sonoff Devices with eWeLink (original) firmware over LAN and/or Cloud from Home Assistant
https://github.com/AlexxIT/Blog
MIT License
2.64k stars 412 forks source link

"Error in getting user region from global!" while trying to login to cloud #1249

Closed sylvix closed 4 months ago

sylvix commented 10 months ago

Steps to reproduce:

  1. Download SonoffLAN integration v.3.5.3
  2. Restart Home Assistant
  3. Go to "Settings" -> "Devices and services" -> "Add integration" -> find "Sonoff"
  4. A dialog window appears which asks for your login credentials.

Now, my eWeLink account is registered in Kyrgyzstan, which is Asia, and it might be important.

  1. When I enter my credentials I get this error in the dialog window and cannot continue any further: Error in getting user region from global!

The debug logs show something similar without any more clarification. While I was debugging this issue the error code was 10005. This error does not show up every time. Actually I can't reproduce it right now while creating this issue, but the version of this integration is the same as when this error occured. It might be that API somehow now works for my account after one successful login with correct region API URL.

What I understood and temporary workaround

By default login method tries EU server which is hard-coded into XRegistryCloud class. Also for some reason the countryCode is hard-coded as "+86". As I understand in the login method of the same class there is a check which catches 10004 error and tries logging in using URL of another region which is actually returned in response.

But it wasn't working for me for some reason. I changed my password not long ago so it might be related.

You can change the following lines in the custom_components/sonoff/core/ewelink/cloud.py to make it work:

Line 88: region = "eu" - change it to another region from dictionary above (Line 22: "API", one of the "cn" - China, "as" - Asia, "us" - USA, "eu" - Europe)

Line 117: "countryCode": "+86", - change it to your country code that you registered your account with. Might not be needed, but after I changed both of these lines together I was able to log in.

Proposed fixes

I guess I might be the only person that encountered this error, because I could find neither that error code or message anywhere in Google or this repository issues. But it might be a good idea to make a fix or provide an explanation if this happens to anyone else.

So, the first option - the login procedure might check every login API URL one by one if that error code happens since it doesn't return a "region" key in its response. But if it also needs a correct countryCode phone prefix then it won't work.

So the next proposition - add "optional" parameters to login credential dialog window where you can select a region and country ("phone") code, so if login doesn't work for anyone with default settings, that person could fiddle with those settings and possibly get logged in.

AlexxIT commented 10 months ago

It's better to wait other users with same issue. This integration uses many years. You right, you first with this issue.

SoumyadebBasu commented 5 months ago

Hi, I would like to add that I also encountered this exact same issue. The solution above has worked for me as well. I am from India so my region is Asia as well. I am not sure if this issue is specific to the asia region.

Initially the plugin was working fine for me without issue, but after a few reboots I got a notification at home assistant asking me to reconfigure the add-on. That is when I encounted this issue. I did a reinstall of the add-on but that didn't help.

ivanfmartinez commented 5 months ago

I'm in Brazil and having same issue after upgrading my homeassistant to 2024.03.3

ivanfmartinez commented 5 months ago

Worked after changing only countryCode in cloud.py

  "countryCode": "+55",
AlexxIT commented 4 months ago

https://github.com/AlexxIT/SonoffLAN/releases/tag/v3.7.0