10
10
11
11
import paho .mqtt .client as mqtt
12
12
13
- from .api import SPECIAL_COMMANDS , RoborockClient , md5hex
13
+ from .api import SPECIAL_COMMANDS , RoborockClient , md5hex , KEEPALIVE
14
14
from .containers import RoborockDeviceInfo , UserData
15
15
from .exceptions import CommandVacuumError , RoborockException , VacuumError
16
16
from .roborock_future import RoborockFuture
17
17
from .roborock_message import RoborockMessage , RoborockParser , md5bin
18
18
from .typing import RoborockCommand
19
19
20
20
_LOGGER = logging .getLogger (__name__ )
21
- MQTT_KEEPALIVE = 60
22
21
CONNECT_REQUEST_ID = 0
23
22
DISCONNECT_REQUEST_ID = 1
24
23
@@ -49,8 +48,6 @@ def __init__(self, user_data: UserData, devices_info: Mapping[str, RoborockDevic
49
48
self ._endpoint = base64 .b64encode (md5bin (rriot .k )[8 :14 ]).decode ()
50
49
self ._waiting_queue : dict [int , RoborockFuture ] = {}
51
50
self ._mutex = Lock ()
52
- self ._last_device_msg_in = mqtt .time_func ()
53
- self ._last_disconnection = mqtt .time_func ()
54
51
self .update_client_id ()
55
52
56
53
def __del__ (self ) -> None :
@@ -80,19 +77,16 @@ def on_connect(self, *args, **kwargs) -> None:
80
77
81
78
def on_message (self , * args , ** kwargs ) -> None :
82
79
_ , __ , msg = args
83
- self ._last_device_msg_in = mqtt .time_func ()
84
80
device_id = msg .topic .split ("/" ).pop ()
85
81
messages , _ = RoborockParser .decode (msg .payload , self .devices_info [device_id ].device .local_key )
86
82
super ().on_message (messages )
87
83
88
84
def on_disconnect (self , * args , ** kwargs ) -> None :
89
85
try :
90
86
_ , __ , 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 } )" ))
93
88
if rc == mqtt .MQTT_ERR_PROTOCOL :
94
89
self .update_client_id ()
95
- _LOGGER .warning (message )
96
90
connection_queue = self ._waiting_queue .get (DISCONNECT_REQUEST_ID )
97
91
if connection_queue :
98
92
connection_queue .resolve ((True , None ))
@@ -102,18 +96,10 @@ def on_disconnect(self, *args, **kwargs) -> None:
102
96
def update_client_id (self ):
103
97
self ._client_id = mqtt .base62 (uuid .uuid4 ().int , padding = 22 )
104
98
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
-
114
99
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
117
103
super ()._check_keepalive () # type: ignore[misc]
118
104
119
105
def sync_stop_loop (self ) -> None :
@@ -142,7 +128,7 @@ def sync_connect(self) -> bool:
142
128
if self ._mqtt_port is None or self ._mqtt_host is None :
143
129
raise RoborockException ("Mqtt information was not entered. Cannot connect." )
144
130
_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 )
146
132
return True
147
133
return False
148
134
0 commit comments