|
38 | 38 | "https://ruiot.roborock.com", |
39 | 39 | ] |
40 | 40 |
|
| 41 | +# Fallback user agreement version, used only if the latest version cannot be fetched. |
| 42 | +DEFAULT_AGREEMENT_MAJOR_VERSION = 14 |
| 43 | +DEFAULT_AGREEMENT_MINOR_VERSION = 0 |
| 44 | + |
41 | 45 |
|
42 | 46 | @dataclass |
43 | 47 | class IotLoginInfo: |
@@ -292,6 +296,34 @@ async def _sign_key_v3(self, s: str) -> str: |
292 | 296 |
|
293 | 297 | return code_response["data"]["k"] |
294 | 298 |
|
| 299 | + async def _get_agreement_version(self, country: str) -> dict[str, int]: |
| 300 | + """Get the latest user agreement version for the given country. |
| 301 | +
|
| 302 | + The login endpoint rejects a stale agreement version with code 3006. The |
| 303 | + current version differs per server and per country, so it must be looked |
| 304 | + up rather than hardcoded. Falls back to the previously hardcoded values |
| 305 | + if the lookup fails so that login is never blocked by this request. |
| 306 | + """ |
| 307 | + try: |
| 308 | + base_url = await self.base_url |
| 309 | + agreement_request = PreparedRequest(base_url, self.session, {"header_clientlang": "en"}) |
| 310 | + response = await agreement_request.request( |
| 311 | + "get", |
| 312 | + "/api/v3/app/agreement/latest", |
| 313 | + params={"country": country}, |
| 314 | + ) |
| 315 | + if response is not None and response.get("code") == 200: |
| 316 | + data = response.get("data") or {} |
| 317 | + major = data.get("majorVersion") |
| 318 | + minor = data.get("minorVersion") |
| 319 | + if isinstance(major, int) and isinstance(minor, int): |
| 320 | + _LOGGER.debug("Using user agreement version %s.%s for %s", major, minor, country) |
| 321 | + return {"majorVersion": major, "minorVersion": minor} |
| 322 | + _LOGGER.debug("Unexpected agreement version response: %s", response) |
| 323 | + except RoborockException as err: |
| 324 | + _LOGGER.debug("Could not fetch latest user agreement version: %s", err) |
| 325 | + return {"majorVersion": DEFAULT_AGREEMENT_MAJOR_VERSION, "minorVersion": DEFAULT_AGREEMENT_MINOR_VERSION} |
| 326 | + |
295 | 327 | async def code_login_v4( |
296 | 328 | self, code: int | str, country: str | None = None, country_code: int | None = None |
297 | 329 | ) -> UserData: |
@@ -334,10 +366,7 @@ async def code_login_v4( |
334 | 366 | "countryCode": country_code, |
335 | 367 | "email": self._username, |
336 | 368 | "code": code, |
337 | | - # Major and minor version are the user agreement version, we will need to see if this needs to be |
338 | | - # dynamic https://usiot.roborock.com/api/v3/app/agreement/latest?country=US |
339 | | - "majorVersion": 14, |
340 | | - "minorVersion": 0, |
| 369 | + **await self._get_agreement_version(country), |
341 | 370 | }, |
342 | 371 | ) |
343 | 372 | if login_response is None: |
|
0 commit comments