MQTT: Allow certificates

This commit is contained in:
Paulus Schoutsen 2015-09-30 00:09:07 -07:00
parent 0dc9f2a9f8
commit 76674d4de9

View File

@ -23,6 +23,7 @@ mqtt:
keepalive: 60 keepalive: 60
username: your_username username: your_username
password: your_secret_password password: your_secret_password
certificate: path_to_certificate
Variables: Variables:
@ -44,6 +45,7 @@ keepalive
The keep alive in seconds for this client. Default is 60. The keep alive in seconds for this client. Default is 60.
""" """
import logging import logging
import os
import socket import socket
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
@ -74,6 +76,7 @@ CONF_CLIENT_ID = 'client_id'
CONF_KEEPALIVE = 'keepalive' CONF_KEEPALIVE = 'keepalive'
CONF_USERNAME = 'username' CONF_USERNAME = 'username'
CONF_PASSWORD = 'password' CONF_PASSWORD = 'password'
CONF_CERTIFICATE = 'certificate'
ATTR_TOPIC = 'topic' ATTR_TOPIC = 'topic'
ATTR_PAYLOAD = 'payload' ATTR_PAYLOAD = 'payload'
@ -119,11 +122,12 @@ def setup(hass, config):
keepalive = util.convert(conf.get(CONF_KEEPALIVE), int, DEFAULT_KEEPALIVE) keepalive = util.convert(conf.get(CONF_KEEPALIVE), int, DEFAULT_KEEPALIVE)
username = util.convert(conf.get(CONF_USERNAME), str) username = util.convert(conf.get(CONF_USERNAME), str)
password = util.convert(conf.get(CONF_PASSWORD), str) password = util.convert(conf.get(CONF_PASSWORD), str)
certificate = util.convert(conf.get(CONF_CERTIFICATE), str)
global MQTT_CLIENT global MQTT_CLIENT
try: try:
MQTT_CLIENT = MQTT(hass, broker, port, client_id, keepalive, username, MQTT_CLIENT = MQTT(hass, broker, port, client_id, keepalive, username,
password) password, certificate)
except socket.error: except socket.error:
_LOGGER.exception("Can't connect to the broker. " _LOGGER.exception("Can't connect to the broker. "
"Please check your settings and the broker " "Please check your settings and the broker "
@ -161,7 +165,7 @@ def setup(hass, config):
class MQTT(object): # pragma: no cover class MQTT(object): # pragma: no cover
""" Implements messaging service for MQTT. """ """ Implements messaging service for MQTT. """
def __init__(self, hass, broker, port, client_id, keepalive, username, def __init__(self, hass, broker, port, client_id, keepalive, username,
password): password, certificate):
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
self.hass = hass self.hass = hass
@ -172,8 +176,12 @@ class MQTT(object): # pragma: no cover
self._mqttc = mqtt.Client() self._mqttc = mqtt.Client()
else: else:
self._mqttc = mqtt.Client(client_id) self._mqttc = mqtt.Client(client_id)
if username is not None: if username is not None:
self._mqttc.username_pw_set(username, password) self._mqttc.username_pw_set(username, password)
if certificate is not None:
self._mqttc.tls_set(certificate)
self._mqttc.on_subscribe = self._mqtt_on_subscribe self._mqttc.on_subscribe = self._mqtt_on_subscribe
self._mqttc.on_unsubscribe = self._mqtt_on_unsubscribe self._mqttc.on_unsubscribe = self._mqtt_on_unsubscribe
self._mqttc.on_connect = self._mqtt_on_connect self._mqttc.on_connect = self._mqtt_on_connect