|
8 | 8 | from typing import Any |
9 | 9 |
|
10 | 10 | import paho.mqtt.client as mqtt |
| 11 | +from paho.mqtt.reasoncodes import ReasonCodes |
11 | 12 |
|
12 | 13 | from .api import KEEPALIVE, RoborockClient |
13 | 14 | from .containers import DeviceData, UserData |
14 | | -from .exceptions import RoborockException, VacuumError |
| 15 | +from .exceptions import RoborockException, RoborockInvalidUserData, VacuumError |
15 | 16 | from .protocol import ( |
16 | 17 | Decoder, |
17 | 18 | Encoder, |
@@ -78,21 +79,27 @@ def __init__(self, user_data: UserData, device_info: DeviceData) -> None: |
78 | 79 | self._encoder: Encoder = create_mqtt_encoder(device_info.device.local_key) |
79 | 80 |
|
80 | 81 | def _mqtt_on_connect(self, *args, **kwargs): |
81 | | - _, __, ___, rc, ____ = args |
| 82 | + rc: ReasonCodes = args[3] |
82 | 83 | 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)})" |
85 | 86 | self._logger.error(message) |
86 | 87 | 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)) |
88 | 95 | else: |
89 | 96 | self._logger.debug("Failed to notify connect future, not in queue") |
90 | 97 | return |
91 | 98 | self._logger.info(f"Connected to mqtt {self._mqtt_host}:{self._mqtt_port}") |
92 | 99 | topic = f"rr/m/o/{self._mqtt_user}/{self._hashed_user}/{self.device_info.device.duid}" |
93 | 100 | (result, mid) = self._mqtt_client.subscribe(topic) |
94 | 101 | if result != 0: |
95 | | - message = f"Failed to subscribe ({mqtt.error_string(rc)})" |
| 102 | + message = f"Failed to subscribe ({str(rc)})" |
96 | 103 | self._logger.error(message) |
97 | 104 | if connection_queue: |
98 | 105 | connection_queue.set_exception(VacuumError(message)) |
|
0 commit comments