9
9
from dataclasses import dataclass
10
10
from datetime import datetime
11
11
12
+ import ssl
12
13
import aio_mqtt
13
14
14
15
from .__version__ import version
@@ -39,6 +40,9 @@ def __init__(
39
40
port : int = None ,
40
41
user : ty .Optional [str ] = None ,
41
42
password : ty .Optional [str ] = None ,
43
+ ca : ty .Optional [str ] = None ,
44
+ cert : ty .Optional [str ] = None ,
45
+ key : ty .Optional [str ] = None ,
42
46
reconnection_interval : int = 10 ,
43
47
* ,
44
48
auto_discovery : bool ,
@@ -55,6 +59,9 @@ def __init__(
55
59
self ._mqtt_port = port
56
60
self ._mqtt_user = user
57
61
self ._mqtt_password = password
62
+ self ._mqtt_ca = ca
63
+ self ._mqtt_cert = cert
64
+ self ._mqtt_key = key
58
65
59
66
self ._will_message = aio_mqtt .PublishableMessage (
60
67
topic_name = self ._topic_lwt ,
@@ -465,9 +472,16 @@ async def _connect_forever(self) -> None:
465
472
while True :
466
473
try :
467
474
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 )
468
481
connect_result = await self ._client .connect (
469
482
host = self ._mqtt_host ,
470
483
port = self ._mqtt_port ,
484
+ ssl = context ,
471
485
username = self ._mqtt_user ,
472
486
password = self ._mqtt_password ,
473
487
client_id = client_id ,
0 commit comments