44import math
55import secrets
66import time
7+ from urllib .parse import urlparse
78
89from . import RoborockCommand
910from .containers import DeviceData , ModelStatus , S7MaxVStatus , Status , UserData
1011from .device_trait import ConsumableTrait , DeviceTrait , DndTrait
11- from .mqtt_manager import RoborockMqttManager
12- from .protocol import MessageParser , Utils
12+ from .mqtt . roborock_session import MqttParams , RoborockMqttSession
13+ from .protocol import MessageParser , Utils , md5hex
1314from .roborock_message import RoborockMessage , RoborockMessageProtocol
1415from .util import RoborockLoggerAdapter , get_next_int
1516
1617_LOGGER = logging .getLogger (__name__ )
1718
1819
1920class RoborockDevice :
21+ _mqtt_sessions : dict [str , RoborockMqttSession ] = {}
22+
2023 def __init__ (self , user_data : UserData , device_info : DeviceData ):
2124 self .user_data = user_data
2225 self .device_info = device_info
2326 self .data = None
2427 self ._logger = RoborockLoggerAdapter (device_info .device .name , _LOGGER )
2528 self ._mqtt_endpoint = base64 .b64encode (Utils .md5 (user_data .rriot .k .encode ())[8 :14 ]).decode ()
29+ rriot = user_data .rriot
30+ self ._mqtt_user = rriot .u
31+ self ._hashed_user = md5hex (self ._mqtt_user + ":" + rriot .k )[2 :10 ]
32+ url = urlparse (rriot .r .m )
33+ self ._mqtt_host = str (url )
34+ self ._mqtt_port = url .port
35+ mqtt_password = rriot .s
36+ self ._hashed_password = md5hex (mqtt_password + ":" + rriot .k )[16 :]
37+
2638 self ._local_endpoint = "abc"
2739 self ._nonce = secrets .token_bytes (16 )
28- self .manager = RoborockMqttManager ()
2940 self ._message_id_types : dict [int , DeviceTrait ] = {}
3041 self ._command_to_trait = {}
3142 self ._all_supported_traits = []
@@ -48,6 +59,14 @@ def _send_command(
4859
4960 async def connect (self ):
5061 """Connect via MQTT and Local if possible."""
62+
63+ MqttParams (
64+ host = self ._mqtt_host ,
65+ port = self ._mqtt_port ,
66+ tls = True ,
67+ username = self ._hashed_user ,
68+ password = self ._hashed_password ,
69+ )
5170 await self .manager .subscribe (self .user_data , self .device_info , self .on_message )
5271 await self .update ()
5372
@@ -93,7 +112,8 @@ async def send_message(
93112 roborock_message = RoborockMessage (timestamp = timestamp , protocol = request_protocol , payload = payload )
94113 if request_id in self ._message_id_types :
95114 raise Exception ("Duplicate id!" )
96- self ._message_id_types [request_id ] = self ._command_to_trait [method ]
115+ if method in self ._command_to_trait :
116+ self ._message_id_types [request_id ] = self ._command_to_trait [method ]
97117 local_key = self .device_info .device .local_key
98118 msg = MessageParser .build (roborock_message , local_key , False )
99119 if use_cloud :
0 commit comments