mqtt: add support for tls connections

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2024-04-28 11:52:18 -04:00
parent 5570ab02ee
commit ac550bac24
1 changed files with 3 additions and 0 deletions

View File

@ -317,6 +317,7 @@ class MQTTClient(APITransport):
self.eventloop = self.server.get_event_loop()
self.address: str = config.get('address')
self.port: int = config.getint('port', 1883)
self.tls_enabled: bool = config.getboolean("enable_tls", False)
user = config.gettemplate('username', None)
self.user_name: Optional[str] = None
if user:
@ -440,6 +441,8 @@ class MQTTClient(APITransport):
self.client.will_set(self.moonraker_status_topic,
payload=jsonw.dumps({'server': 'offline'}),
qos=self.qos, retain=True)
if self.tls_enabled:
self.client.tls_set()
self.client.connect_async(self.address, self.port)
self.connect_task = self.eventloop.create_task(
self._do_reconnect(first=True)