Skip to content

Commit 5634ff2

Browse files
committed
fix: handle auth expiring
1 parent d966b84 commit 5634ff2

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

roborock/cloud_api.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
from typing import Any
99

1010
import paho.mqtt.client as mqtt
11+
from paho.mqtt.reasoncodes import ReasonCodes
1112

1213
from .api import KEEPALIVE, RoborockClient
1314
from .containers import DeviceData, UserData
14-
from .exceptions import RoborockException, VacuumError
15+
from .exceptions import RoborockException, RoborockInvalidUserData, VacuumError
1516
from .protocol import (
1617
Decoder,
1718
Encoder,
@@ -78,21 +79,27 @@ def __init__(self, user_data: UserData, device_info: DeviceData) -> None:
7879
self._encoder: Encoder = create_mqtt_encoder(device_info.device.local_key)
7980

8081
def _mqtt_on_connect(self, *args, **kwargs):
81-
_, __, ___, rc, ____ = args
82+
rc: ReasonCodes = args[3]
8283
connection_queue = self._waiting_queue.get(CONNECT_REQUEST_ID)
83-
if rc != mqtt.MQTT_ERR_SUCCESS:
84-
message = f"Failed to connect ({mqtt.error_string(rc)})"
84+
if rc != 0:
85+
message = f"Failed to connect ({str(rc)})"
8586
self._logger.error(message)
8687
if connection_queue:
87-
connection_queue.set_exception(VacuumError(message))
88+
# These are the ReasonCodes relating to authorization issues.
89+
if rc.value in {24, 25, 133, 134, 135, 144}:
90+
connection_queue.set_exception(
91+
RoborockInvalidUserData("Failed to connect to mqtt. Invalid user data. Re-auth is needed.")
92+
)
93+
else:
94+
connection_queue.set_exception(VacuumError(message))
8895
else:
8996
self._logger.debug("Failed to notify connect future, not in queue")
9097
return
9198
self._logger.info(f"Connected to mqtt {self._mqtt_host}:{self._mqtt_port}")
9299
topic = f"rr/m/o/{self._mqtt_user}/{self._hashed_user}/{self.device_info.device.duid}"
93100
(result, mid) = self._mqtt_client.subscribe(topic)
94101
if result != 0:
95-
message = f"Failed to subscribe ({mqtt.error_string(rc)})"
102+
message = f"Failed to subscribe ({str(rc)})"
96103
self._logger.error(message)
97104
if connection_queue:
98105
connection_queue.set_exception(VacuumError(message))

roborock/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Roborock exceptions."""
2+
23
from __future__ import annotations
34

45

@@ -76,3 +77,7 @@ class RoborockTooManyRequest(RoborockException):
7677

7778
class RoborockRateLimit(RoborockException):
7879
"""Class for our rate limits exceptions."""
80+
81+
82+
class RoborockInvalidUserData(RoborockException):
83+
"""Class to state the user data is invalid (expired or manipulated)."""

roborock/roborock_future.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import async_timeout
77

8-
from .exceptions import VacuumError
8+
from .exceptions import RoborockInvalidUserData, VacuumError
99

1010

1111
class RoborockFuture:
@@ -21,11 +21,11 @@ def _set_result(self, item: Any) -> None:
2121
def set_result(self, item: Any) -> None:
2222
self.loop.call_soon_threadsafe(self._set_result, item)
2323

24-
def _set_exception(self, exc: VacuumError) -> None:
24+
def _set_exception(self, exc: VacuumError | RoborockInvalidUserData) -> None:
2525
if not self.fut.cancelled():
2626
self.fut.set_exception(exc)
2727

28-
def set_exception(self, exc: VacuumError) -> None:
28+
def set_exception(self, exc: VacuumError | RoborockInvalidUserData) -> None:
2929
self.loop.call_soon_threadsafe(self._set_exception, exc)
3030

3131
async def async_get(self, timeout: float | int) -> tuple[Any, VacuumError | None]:

0 commit comments

Comments
 (0)