Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tuya_iot/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ def is_connect(self) -> bool:
"""Is connect to tuya cloud."""
return self.token_info is not None and len(self.token_info.access_token) > 0

def force_reconnect(self) -> bool:
self.connect(
self.__username, self.__password, self.__country_code, self.__schema
)
return self.is_connect()

def __request(
self,
method: str,
Expand Down
20 changes: 12 additions & 8 deletions tuya_iot/openmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,21 @@ def run(self):
def __run_mqtt(self):
mq_config = self._get_mqtt_config()
if mq_config is None:
logger.error("error while get mqtt config")
return
logger.error("error while get mqtt config, trying to reconnect")
self.api.force_reconnect()
mq_config = self._get_mqtt_config()

self.mq_config = mq_config
if mq_config is None:
logger.error("permanent error while get mqtt config")
else:
self.mq_config = mq_config

logger.debug(f"connecting {mq_config.url}")
mqttc = self._start(mq_config)
logger.debug(f"connecting {mq_config.url}")
mqttc = self._start(mq_config)

if self.client:
self.client.disconnect()
self.client = mqttc
if self.client:
self.client.disconnect()
self.client = mqttc

def _start(self, mq_config: TuyaMQConfig) -> mqtt.Client:
mqttc = mqtt.Client(mq_config.client_id)
Expand Down