1010
1111import paho .mqtt .client as mqtt
1212
13- from .api import SPECIAL_COMMANDS , RoborockClient , md5hex
13+ from .api import SPECIAL_COMMANDS , RoborockClient , md5hex , KEEPALIVE
1414from .containers import RoborockDeviceInfo , UserData
1515from .exceptions import CommandVacuumError , RoborockException , VacuumError
1616from .roborock_future import RoborockFuture
1717from .roborock_message import RoborockMessage , RoborockParser , md5bin
1818from .typing import RoborockCommand
1919
2020_LOGGER = logging .getLogger (__name__ )
21- MQTT_KEEPALIVE = 60
2221CONNECT_REQUEST_ID = 0
2322DISCONNECT_REQUEST_ID = 1
2423
@@ -49,8 +48,6 @@ def __init__(self, user_data: UserData, devices_info: Mapping[str, RoborockDevic
4948 self ._endpoint = base64 .b64encode (md5bin (rriot .k )[8 :14 ]).decode ()
5049 self ._waiting_queue : dict [int , RoborockFuture ] = {}
5150 self ._mutex = Lock ()
52- self ._last_device_msg_in = mqtt .time_func ()
53- self ._last_disconnection = mqtt .time_func ()
5451 self .update_client_id ()
5552
5653 def __del__ (self ) -> None :
@@ -80,19 +77,16 @@ def on_connect(self, *args, **kwargs) -> None:
8077
8178 def on_message (self , * args , ** kwargs ) -> None :
8279 _ , __ , msg = args
83- self ._last_device_msg_in = mqtt .time_func ()
8480 device_id = msg .topic .split ("/" ).pop ()
8581 messages , _ = RoborockParser .decode (msg .payload , self .devices_info [device_id ].device .local_key )
8682 super ().on_message (messages )
8783
8884 def on_disconnect (self , * args , ** kwargs ) -> None :
8985 try :
9086 _ , __ , rc , ___ = args
91- self ._last_disconnection = mqtt .time_func ()
92- message = f"Roborock mqtt client disconnected (rc: { rc } )"
87+ super ().on_disconnect (RoborockException (f"(rc: { rc } )" ))
9388 if rc == mqtt .MQTT_ERR_PROTOCOL :
9489 self .update_client_id ()
95- _LOGGER .warning (message )
9690 connection_queue = self ._waiting_queue .get (DISCONNECT_REQUEST_ID )
9791 if connection_queue :
9892 connection_queue .resolve ((True , None ))
@@ -102,18 +96,10 @@ def on_disconnect(self, *args, **kwargs) -> None:
10296 def update_client_id (self ):
10397 self ._client_id = mqtt .base62 (uuid .uuid4 ().int , padding = 22 )
10498
105- def _async_check_keepalive (self ) -> None :
106- now = mqtt .time_func ()
107- # noinspection PyUnresolvedReferences
108- if (
109- now - self ._last_disconnection > self ._keepalive ** 2 # type: ignore[attr-defined]
110- and now - self ._last_device_msg_in > self ._keepalive # type: ignore[attr-defined]
111- ):
112- self ._ping_t = self ._last_device_msg_in
113-
11499 def _check_keepalive (self ) -> None :
115- self ._async_check_keepalive ()
116- # noinspection PyUnresolvedReferences
100+ if not self .should_keepalive ():
101+ self ._ping_t = self .time_func () - KEEPALIVE
102+ # noinspection PyUnresolvedReferences
117103 super ()._check_keepalive () # type: ignore[misc]
118104
119105 def sync_stop_loop (self ) -> None :
@@ -142,7 +128,7 @@ def sync_connect(self) -> bool:
142128 if self ._mqtt_port is None or self ._mqtt_host is None :
143129 raise RoborockException ("Mqtt information was not entered. Cannot connect." )
144130 _LOGGER .info ("Connecting to mqtt" )
145- super ().connect_async (host = self ._mqtt_host , port = self ._mqtt_port , keepalive = MQTT_KEEPALIVE )
131+ super ().connect_async (host = self ._mqtt_host , port = self ._mqtt_port , keepalive = KEEPALIVE )
146132 return True
147133 return False
148134
0 commit comments