Skip to content

Commit 2968d72

Browse files
fix: add more routes for trying to find the tuya region
1 parent cdcad83 commit 2968d72

File tree

3 files changed

+274
-15
lines changed

3 files changed

+274
-15
lines changed

custom_components/robovac/config_flow.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,17 @@
3939
CONF_IP_ADDRESS,
4040
CONF_DESCRIPTION,
4141
CONF_MAC,
42-
CONF_LOCATION,
4342
CONF_CLIENT_ID,
4443
CONF_REGION,
4544
CONF_TIME_ZONE,
45+
CONF_COUNTRY_CODE,
46+
)
47+
48+
from .countries import (
49+
get_phone_code_by_country_code,
50+
get_phone_code_by_region,
51+
get_region_by_country_code,
52+
get_region_by_phone_code,
4653
)
4754

4855
from .const import CONF_AUTODISCOVERY, DOMAIN, CONF_VACS
@@ -88,15 +95,41 @@ def get_eufy_vacuums(self):
8895
settings_response = response.json()
8996

9097
self[CONF_CLIENT_ID] = user_response["user_info"]["id"]
91-
self[CONF_REGION] = settings_response["setting"]["home_setting"]["tuya_home"][
92-
"tuya_region_code"
93-
]
98+
if (
99+
"tuya_home" in settings_response["setting"]["home_setting"]
100+
and "tuya_region_code"
101+
in settings_response["setting"]["home_setting"]["tuya_home"]
102+
):
103+
self[CONF_REGION] = settings_response["setting"]["home_setting"]["tuya_home"][
104+
"tuya_region_code"
105+
]
106+
if user_response["user_info"]["phone_code"]:
107+
self[CONF_COUNTRY_CODE] = user_response["user_info"]["phone_code"]
108+
else:
109+
self[CONF_COUNTRY_CODE] = get_phone_code_by_region(self[CONF_REGION])
110+
elif user_response["user_info"]["phone_code"]:
111+
self[CONF_REGION] = get_region_by_phone_code(
112+
user_response["user_info"]["phone_code"]
113+
)
114+
self[CONF_COUNTRY_CODE] = user_response["user_info"]["phone_code"]
115+
elif user_response["user_info"]["country"]:
116+
self[CONF_REGION] = get_region_by_country_code(
117+
user_response["user_info"]["country"]
118+
)
119+
self[CONF_COUNTRY_CODE] = get_phone_code_by_country_code(
120+
user_response["user_info"]["country"]
121+
)
122+
else:
123+
self[CONF_REGION] = "EU"
124+
self[CONF_COUNTRY_CODE] = "44"
125+
94126
self[CONF_TIME_ZONE] = user_response["user_info"]["timezone"]
95127

96128
tuya_client = TuyaAPISession(
97129
username="eh-" + self[CONF_CLIENT_ID],
98130
region=self[CONF_REGION],
99131
timezone=self[CONF_TIME_ZONE],
132+
phone_code=self[CONF_COUNTRY_CODE],
100133
)
101134

102135
items = device_response["items"]
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
COUNTRIES = [
2+
{"country_code": "AF", "phone_code": "93", "tuya_region": "EU"},
3+
{"country_code": "AL", "phone_code": "355", "tuya_region": "EU"},
4+
{"country_code": "DZ", "phone_code": "213", "tuya_region": "EU"},
5+
{"country_code": "AO", "phone_code": "244", "tuya_region": "EU"},
6+
{"country_code": "AR", "phone_code": "54", "tuya_region": "AZ"},
7+
{"country_code": "AM", "phone_code": "374", "tuya_region": "EU"},
8+
{"country_code": "AU", "phone_code": "61", "tuya_region": "AZ"},
9+
{"country_code": "AT", "phone_code": "43", "tuya_region": "EU"},
10+
{"country_code": "AZ", "phone_code": "994", "tuya_region": "EU"},
11+
{"country_code": "BH", "phone_code": "973", "tuya_region": "EU"},
12+
{"country_code": "BD", "phone_code": "880", "tuya_region": "EU"},
13+
{"country_code": "BY", "phone_code": "375", "tuya_region": "EU"},
14+
{"country_code": "BE", "phone_code": "32", "tuya_region": "EU"},
15+
{"country_code": "BZ", "phone_code": "501", "tuya_region": "EU"},
16+
{"country_code": "BJ", "phone_code": "229", "tuya_region": "EU"},
17+
{"country_code": "BT", "phone_code": "975", "tuya_region": "EU"},
18+
{"country_code": "BO", "phone_code": "591", "tuya_region": "AZ"},
19+
{"country_code": "BA", "phone_code": "387", "tuya_region": "EU"},
20+
{"country_code": "BW", "phone_code": "267", "tuya_region": "EU"},
21+
{"country_code": "BR", "phone_code": "55", "tuya_region": "AZ"},
22+
{"country_code": "VG", "phone_code": "1284", "tuya_region": "EU"},
23+
{"country_code": "BN", "phone_code": "673", "tuya_region": "EU"},
24+
{"country_code": "BG", "phone_code": "359", "tuya_region": "EU"},
25+
{"country_code": "BF", "phone_code": "226", "tuya_region": "EU"},
26+
{"country_code": "BI", "phone_code": "257", "tuya_region": "EU"},
27+
{"country_code": "KH", "phone_code": "855", "tuya_region": "EU"},
28+
{"country_code": "CM", "phone_code": "237", "tuya_region": "EU"},
29+
{"country_code": "US", "phone_code": "1", "tuya_region": "AZ"},
30+
{"country_code": "CA", "phone_code": "1", "tuya_region": "AZ"},
31+
{"country_code": "CV", "phone_code": "238", "tuya_region": "EU"},
32+
{"country_code": "KY", "phone_code": "1345", "tuya_region": "EU"},
33+
{"country_code": "CF", "phone_code": "236", "tuya_region": "EU"},
34+
{"country_code": "TD", "phone_code": "235", "tuya_region": "EU"},
35+
{"country_code": "CL", "phone_code": "56", "tuya_region": "AZ"},
36+
{"country_code": "CN", "phone_code": "86", "tuya_region": "AY"},
37+
{"country_code": "CO", "phone_code": "57", "tuya_region": "AZ"},
38+
{"country_code": "KM", "phone_code": "269", "tuya_region": "EU"},
39+
{"country_code": "CG", "phone_code": "242", "tuya_region": "EU"},
40+
{"country_code": "CD", "phone_code": "243", "tuya_region": "EU"},
41+
{"country_code": "CR", "phone_code": "506", "tuya_region": "EU"},
42+
{"country_code": "HR", "phone_code": "385", "tuya_region": "EU"},
43+
{"country_code": "CY", "phone_code": "357", "tuya_region": "EU"},
44+
{"country_code": "CZ", "phone_code": "420", "tuya_region": "EU"},
45+
{"country_code": "DK", "phone_code": "45", "tuya_region": "EU"},
46+
{"country_code": "DJ", "phone_code": "253", "tuya_region": "EU"},
47+
{"country_code": "DO", "phone_code": "1809", "tuya_region": "EU"},
48+
{"country_code": "DO", "phone_code": "1829", "tuya_region": "EU"},
49+
{"country_code": "DO", "phone_code": "1849", "tuya_region": "EU"},
50+
{"country_code": "EC", "phone_code": "593", "tuya_region": "AZ"},
51+
{"country_code": "EG", "phone_code": "20", "tuya_region": "EU"},
52+
{"country_code": "SV", "phone_code": "503", "tuya_region": "EU"},
53+
{"country_code": "GQ", "phone_code": "240", "tuya_region": "EU"},
54+
{"country_code": "ER", "phone_code": "291", "tuya_region": "EU"},
55+
{"country_code": "EE", "phone_code": "372", "tuya_region": "EU"},
56+
{"country_code": "ET", "phone_code": "251", "tuya_region": "EU"},
57+
{"country_code": "FJ", "phone_code": "679", "tuya_region": "EU"},
58+
{"country_code": "FI", "phone_code": "358", "tuya_region": "EU"},
59+
{"country_code": "FR", "phone_code": "33", "tuya_region": "EU"},
60+
{"country_code": "GA", "phone_code": "241", "tuya_region": "EU"},
61+
{"country_code": "GM", "phone_code": "220", "tuya_region": "EU"},
62+
{"country_code": "GE", "phone_code": "995", "tuya_region": "EU"},
63+
{"country_code": "DE", "phone_code": "49", "tuya_region": "EU"},
64+
{"country_code": "GH", "phone_code": "233", "tuya_region": "EU"},
65+
{"country_code": "GR", "phone_code": "30", "tuya_region": "EU"},
66+
{"country_code": "GL", "phone_code": "299", "tuya_region": "EU"},
67+
{"country_code": "GT", "phone_code": "502", "tuya_region": "AZ"},
68+
{"country_code": "GN", "phone_code": "224", "tuya_region": "EU"},
69+
{"country_code": "GY", "phone_code": "592", "tuya_region": "EU"},
70+
{"country_code": "HT", "phone_code": "509", "tuya_region": "EU"},
71+
{"country_code": "HN", "phone_code": "504", "tuya_region": "EU"},
72+
{"country_code": "HK", "phone_code": "852", "tuya_region": "AZ"},
73+
{"country_code": "HU", "phone_code": "36", "tuya_region": "EU"},
74+
{"country_code": "IS", "phone_code": "354", "tuya_region": "EU"},
75+
{"country_code": "IN", "phone_code": "91", "tuya_region": "IN"},
76+
{"country_code": "ID", "phone_code": "62", "tuya_region": "AZ"},
77+
{"country_code": "IR", "phone_code": "98", "tuya_region": "EU"},
78+
{"country_code": "IQ", "phone_code": "964", "tuya_region": "EU"},
79+
{"country_code": "IE", "phone_code": "353", "tuya_region": "EU"},
80+
{"country_code": "IM", "phone_code": "44", "tuya_region": "EU"},
81+
{"country_code": "IL", "phone_code": "972", "tuya_region": "EU"},
82+
{"country_code": "IT", "phone_code": "39", "tuya_region": "AZ"},
83+
{"country_code": "CI", "phone_code": "225", "tuya_region": "EU"},
84+
{"country_code": "JM", "phone_code": "1876", "tuya_region": "EU"},
85+
{"country_code": "JP", "phone_code": "81", "tuya_region": "AZ"},
86+
{"country_code": "JO", "phone_code": "962", "tuya_region": "EU"},
87+
{"country_code": "KZ", "phone_code": "7", "tuya_region": "EU"},
88+
{"country_code": "KE", "phone_code": "254", "tuya_region": "EU"},
89+
{"country_code": "KR", "phone_code": "82", "tuya_region": "AZ"},
90+
{"country_code": "KW", "phone_code": "965", "tuya_region": "EU"},
91+
{"country_code": "KG", "phone_code": "996", "tuya_region": "EU"},
92+
{"country_code": "LA", "phone_code": "856", "tuya_region": "EU"},
93+
{"country_code": "LV", "phone_code": "371", "tuya_region": "EU"},
94+
{"country_code": "LB", "phone_code": "961", "tuya_region": "EU"},
95+
{"country_code": "LS", "phone_code": "266", "tuya_region": "EU"},
96+
{"country_code": "LR", "phone_code": "231", "tuya_region": "EU"},
97+
{"country_code": "LY", "phone_code": "218", "tuya_region": "EU"},
98+
{"country_code": "LT", "phone_code": "370", "tuya_region": "EU"},
99+
{"country_code": "LU", "phone_code": "352", "tuya_region": "EU"},
100+
{"country_code": "MO", "phone_code": "853", "tuya_region": "AZ"},
101+
{"country_code": "MK", "phone_code": "389", "tuya_region": "EU"},
102+
{"country_code": "MG", "phone_code": "261", "tuya_region": "EU"},
103+
{"country_code": "MW", "phone_code": "265", "tuya_region": "EU"},
104+
{"country_code": "MY", "phone_code": "60", "tuya_region": "AZ"},
105+
{"country_code": "MV", "phone_code": "960", "tuya_region": "EU"},
106+
{"country_code": "ML", "phone_code": "223", "tuya_region": "EU"},
107+
{"country_code": "MT", "phone_code": "356", "tuya_region": "EU"},
108+
{"country_code": "MR", "phone_code": "222", "tuya_region": "EU"},
109+
{"country_code": "MU", "phone_code": "230", "tuya_region": "EU"},
110+
{"country_code": "MX", "phone_code": "52", "tuya_region": "AZ"},
111+
{"country_code": "MD", "phone_code": "373", "tuya_region": "EU"},
112+
{"country_code": "MC", "phone_code": "377", "tuya_region": "EU"},
113+
{"country_code": "MN", "phone_code": "976", "tuya_region": "EU"},
114+
{"country_code": "ME", "phone_code": "382", "tuya_region": "EU"},
115+
{"country_code": "MA", "phone_code": "212", "tuya_region": "EU"},
116+
{"country_code": "MZ", "phone_code": "258", "tuya_region": "EU"},
117+
{"country_code": "MM", "phone_code": "95", "tuya_region": "AZ"},
118+
{"country_code": "NA", "phone_code": "264", "tuya_region": "EU"},
119+
{"country_code": "NP", "phone_code": "977", "tuya_region": "EU"},
120+
{"country_code": "NL", "phone_code": "31", "tuya_region": "EU"},
121+
{"country_code": "NZ", "phone_code": "64", "tuya_region": "AZ"},
122+
{"country_code": "NI", "phone_code": "505", "tuya_region": "AZ"},
123+
{"country_code": "NE", "phone_code": "227", "tuya_region": "EU"},
124+
{"country_code": "NG", "phone_code": "234", "tuya_region": "EU"},
125+
{"country_code": "KP", "phone_code": "850", "tuya_region": "EU"},
126+
{"country_code": "NO", "phone_code": "47", "tuya_region": "EU"},
127+
{"country_code": "OM", "phone_code": "968", "tuya_region": "EU"},
128+
{"country_code": "PK", "phone_code": "92", "tuya_region": "EU"},
129+
{"country_code": "PA", "phone_code": "507", "tuya_region": "EU"},
130+
{"country_code": "PY", "phone_code": "595", "tuya_region": "AZ"},
131+
{"country_code": "PE", "phone_code": "51", "tuya_region": "AZ"},
132+
{"country_code": "PH", "phone_code": "63", "tuya_region": "AZ"},
133+
{"country_code": "PL", "phone_code": "48", "tuya_region": "EU"},
134+
{"country_code": "PF", "phone_code": "689", "tuya_region": "EU"},
135+
{"country_code": "PT", "phone_code": "351", "tuya_region": "EU"},
136+
{"country_code": "PR", "phone_code": "1787", "tuya_region": "AZ"},
137+
{"country_code": "QA", "phone_code": "974", "tuya_region": "EU"},
138+
{"country_code": "RE", "phone_code": "262", "tuya_region": "EU"},
139+
{"country_code": "RO", "phone_code": "40", "tuya_region": "EU"},
140+
{"country_code": "RU", "phone_code": "7", "tuya_region": "EU"},
141+
{"country_code": "RW", "phone_code": "250", "tuya_region": "EU"},
142+
{"country_code": "SM", "phone_code": "378", "tuya_region": "EU"},
143+
{"country_code": "SA", "phone_code": "966", "tuya_region": "EU"},
144+
{"country_code": "SN", "phone_code": "221", "tuya_region": "EU"},
145+
{"country_code": "RS", "phone_code": "381", "tuya_region": "EU"},
146+
{"country_code": "SL", "phone_code": "232", "tuya_region": "EU"},
147+
{"country_code": "SG", "phone_code": "65", "tuya_region": "EU"},
148+
{"country_code": "SK", "phone_code": "421", "tuya_region": "EU"},
149+
{"country_code": "SI", "phone_code": "386", "tuya_region": "EU"},
150+
{"country_code": "SO", "phone_code": "252", "tuya_region": "EU"},
151+
{"country_code": "ZA", "phone_code": "27", "tuya_region": "EU"},
152+
{"country_code": "ES", "phone_code": "34", "tuya_region": "EU"},
153+
{"country_code": "LK", "phone_code": "94", "tuya_region": "EU"},
154+
{"country_code": "SD", "phone_code": "249", "tuya_region": "EU"},
155+
{"country_code": "SR", "phone_code": "597", "tuya_region": "AZ"},
156+
{"country_code": "SZ", "phone_code": "268", "tuya_region": "EU"},
157+
{"country_code": "SE", "phone_code": "46", "tuya_region": "EU"},
158+
{"country_code": "CH", "phone_code": "41", "tuya_region": "EU"},
159+
{"country_code": "SY", "phone_code": "963", "tuya_region": "EU"},
160+
{"country_code": "TW", "phone_code": "886", "tuya_region": "AZ"},
161+
{"country_code": "TJ", "phone_code": "992", "tuya_region": "EU"},
162+
{"country_code": "TZ", "phone_code": "255", "tuya_region": "EU"},
163+
{"country_code": "TH", "phone_code": "66", "tuya_region": "AZ"},
164+
{"country_code": "TG", "phone_code": "228", "tuya_region": "EU"},
165+
{"country_code": "TO", "phone_code": "676", "tuya_region": "EU"},
166+
{"country_code": "TT", "phone_code": "1868", "tuya_region": "EU"},
167+
{"country_code": "TN", "phone_code": "216", "tuya_region": "EU"},
168+
{"country_code": "TR", "phone_code": "90", "tuya_region": "EU"},
169+
{"country_code": "TM", "phone_code": "993", "tuya_region": "EU"},
170+
{"country_code": "VI", "phone_code": "1340", "tuya_region": "EU"},
171+
{"country_code": "UG", "phone_code": "256", "tuya_region": "EU"},
172+
{"country_code": "UA", "phone_code": "380", "tuya_region": "EU"},
173+
{"country_code": "AE", "phone_code": "971", "tuya_region": "EU"},
174+
{"country_code": "GB", "phone_code": "44", "tuya_region": "EU"},
175+
{"country_code": "UY", "phone_code": "598", "tuya_region": "AZ"},
176+
{"country_code": "UZ", "phone_code": "998", "tuya_region": "EU"},
177+
{"country_code": "VA", "phone_code": "379", "tuya_region": "EU"},
178+
{"country_code": "VE", "phone_code": "58", "tuya_region": "AZ"},
179+
{"country_code": "VN", "phone_code": "84", "tuya_region": "AZ"},
180+
{"country_code": "YE", "phone_code": "967", "tuya_region": "EU"},
181+
{"country_code": "ZR", "phone_code": "243", "tuya_region": "EU"},
182+
{"country_code": "ZM", "phone_code": "260", "tuya_region": "EU"},
183+
{"country_code": "ZW", "phone_code": "263", "tuya_region": "EU"},
184+
{"country_code": "NCL", "phone_code": "687", "tuya_region": "EU"},
185+
{"country_code": "MQ", "phone_code": "596", "tuya_region": "EU"},
186+
]
187+
188+
189+
def get_region_by_country_code(country_code):
190+
country = next(
191+
(item for item in COUNTRIES if item["country_code"] == country_code), None
192+
)
193+
194+
if country is None:
195+
return "EU"
196+
197+
return country["tuya_region"]
198+
199+
200+
def get_region_by_phone_code(phone_code):
201+
country = next(
202+
(item for item in COUNTRIES if item["phone_code"] == phone_code), None
203+
)
204+
205+
if country is None:
206+
return "EU"
207+
208+
return country["tuya_region"]
209+
210+
211+
def get_phone_code_by_region(region):
212+
country = next((item for item in COUNTRIES if item["region"] == region), None)
213+
214+
if country is None:
215+
return "44"
216+
217+
return country["phone_code"]
218+
219+
220+
def get_phone_code_by_country_code(country_code):
221+
country = next(
222+
(item for item in COUNTRIES if item["country_code"] == country_code), None
223+
)
224+
225+
if country is None:
226+
return "44"
227+
228+
return country["phone_code"]

custom_components/robovac/tuyawebapi.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,20 @@ class TuyaAPISession:
9090
country_code = None
9191
session_id = None
9292

93-
def __init__(self, username, region, timezone):
93+
def __init__(self, username, region, timezone, phone_code):
9494
self.session = requests.session()
9595
self.session.headers = DEFAULT_TUYA_HEADERS.copy()
9696
self.default_query_params = DEFAULT_TUYA_QUERY_PARAMS.copy()
9797
self.default_query_params["deviceId"] = self.generate_new_device_id()
9898
self.username = username
99-
self.country_code = self.getCountryCode(region)
99+
self.country_code = phone_code
100100
self.base_url = {
101-
"EU": "https://a1.tuyaeu.com",
101+
"AZ": "https://a1.tuyaus.com",
102102
"AY": "https://a1.tuyacn.com",
103-
}.get(region, "https://a1.tuyaus.com")
103+
"IN": "https://a1.tuyain.com",
104+
"EU": "https://a1.tuyaeu.com",
105+
}.get(region, "https://a1.tuyaeu.com")
106+
104107
DEFAULT_TUYA_QUERY_PARAMS["timeZoneId"] = timezone
105108

106109
@staticmethod
@@ -232,13 +235,8 @@ def acquire_session(self):
232235

233236
def list_homes(self):
234237
return self._request(action="tuya.m.location.list", version="2.1")
235-
238+
236239
def get_device(self, devId):
237240
return self._request(
238-
action="tuya.m.device.get",
239-
version="1.0",
240-
data={"devId": devId}
241+
action="tuya.m.device.get", version="1.0", data={"devId": devId}
241242
)
242-
243-
def getCountryCode(self, region_code):
244-
return {"EU": "44", "AY": "86"}.get(region_code, "1")

0 commit comments

Comments
 (0)