Skip to content

Commit a8d4d0d

Browse files
M66Bdevbis
authored andcommitted
Added SSL
1 parent 445ed87 commit a8d4d0d

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Diff for: lumimqtt/__main__.py

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ async def amain():
7272
port=config['mqtt_port'],
7373
user=config.get('mqtt_user'),
7474
password=config.get('mqtt_password'),
75+
ca=config.get('mqtt_ca'),
76+
cert=config.get('mqtt_cert'),
77+
key=config.get('mqtt_key'),
7578
auto_discovery=config['auto_discovery'],
7679
sensor_retain=config.get('sensor_retain', False),
7780
sensor_threshold=int(config['sensor_threshold']),

Diff for: lumimqtt/lumimqtt.py

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from dataclasses import dataclass
1010
from datetime import datetime
1111

12+
import ssl
1213
import aio_mqtt
1314

1415
from .__version__ import version
@@ -39,6 +40,9 @@ def __init__(
3940
port: int = None,
4041
user: ty.Optional[str] = None,
4142
password: ty.Optional[str] = None,
43+
ca: ty.Optional[str] = None,
44+
cert: ty.Optional[str] = None,
45+
key: ty.Optional[str] = None,
4246
reconnection_interval: int = 10,
4347
*,
4448
auto_discovery: bool,
@@ -55,6 +59,9 @@ def __init__(
5559
self._mqtt_port = port
5660
self._mqtt_user = user
5761
self._mqtt_password = password
62+
self._mqtt_ca = ca
63+
self._mqtt_cert = cert
64+
self._mqtt_key = key
5865

5966
self._will_message = aio_mqtt.PublishableMessage(
6067
topic_name=self._topic_lwt,
@@ -465,9 +472,16 @@ async def _connect_forever(self) -> None:
465472
while True:
466473
try:
467474
client_id = f'lumimqtt_{self.dev_id}'
475+
context = None
476+
if self._mqtt_cert is not None and self._mqtt_key is not None:
477+
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
478+
if self._mqtt_ca is not None:
479+
context.load_verify_locations(self._mqtt_ca)
480+
context.load_cert_chain(self._mqtt_cert, self._mqtt_key)
468481
connect_result = await self._client.connect(
469482
host=self._mqtt_host,
470483
port=self._mqtt_port,
484+
ssl=context,
471485
username=self._mqtt_user,
472486
password=self._mqtt_password,
473487
client_id=client_id,

0 commit comments

Comments
 (0)