Skip to content

Commit

Permalink
added support for auth and TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
danielringch committed Jan 14, 2024
1 parent ddba82e commit c35a0cd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion config/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@ mqtt:
host: "127.0.0.1:1883"
topic: "foo/abc"
server2:
host: "1.2.3.4:1883"
host: "1.2.3.4:8883"
topic: "bar/def"
ca: "path/to/cert/ca/file.crt" # optional, onyl used for TLS connection
tls_insecure: false # optional, must be set to true for self signed certificates
user: "username" # optional, only for auth via username + password
password: "password" # optional, only for auth via username + password
public_key: "path/to/cert/public/key/file.crt" # optional, only for auth via certificate
private_key: "path/to/cert/private/key/file.crt" # optional, only for auth via certificate
13 changes: 13 additions & 0 deletions tibber2mqtt/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import struct
import paho.mqtt.client as mqtt
from ssl import CERT_NONE
from helpers import get_recursive_key
from logger import *

Expand All @@ -12,6 +13,18 @@ def __init__(self, name: str, config: dict):

ip, port = get_recursive_key(config, 'host').split(':')

ca = get_recursive_key(config, 'ca', optional=True)
public_key = get_recursive_key(config, 'public_key', optional=True)
private_key = get_recursive_key(config, 'private_key', optional=True)
if ca or public_key or private_key:
insecure = get_recursive_key(config, 'tls_insecure', optional=True) == True
self.__mqtt.tls_set(ca_certs=ca, certfile=public_key, keyfile=private_key, cert_reqs=CERT_NONE if insecure else None)

user = get_recursive_key(config, 'user', optional=True)
password = get_recursive_key(config, 'password', optional=True)
if user or password:
self.__mqtt.username_pw_set(user, password)

self.__topic = get_recursive_key(config, 'topic')

self.__mqtt.connect(ip, int(port), 60)
Expand Down

0 comments on commit c35a0cd

Please sign in to comment.