Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions homeassistant/components/mqtt/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['hbmqtt==0.9.2']
REQUIREMENTS = ['hbmqtt==0.9.4', 'hbmqtt-auth-home-assistant==1.0.2']
DEPENDENCIES = ['http']

# None allows custom config to be created through generate_config
Expand All @@ -38,21 +38,26 @@ def async_start(hass, password, server_config):
"""
from hbmqtt.broker import Broker, BrokerException

passwd = tempfile.NamedTemporaryFile()
passwd = None

try:
if server_config is None:
server_config, client_config = generate_config(
hass, passwd, password)
else:
if server_config is not None:
client_config = None
elif hass.auth.active:
server_config, client_config = generate_config(hass)
else:
passwd = tempfile.NamedTemporaryFile()
server_config, client_config = generate_config_legacy(
hass, passwd, password)

broker = Broker(server_config, hass.loop)
yield from broker.start()
except BrokerException:
_LOGGER.exception("Error initializing MQTT server")
return False, None
finally:
passwd.close()
if passwd is not None:
passwd.close()

@asyncio.coroutine
def async_shutdown_mqtt_server(event):
Expand All @@ -65,7 +70,41 @@ def async_shutdown_mqtt_server(event):
return True, client_config


def generate_config(hass, passwd, password):
def generate_config(hass):
"""Generate a configuration based on current Home Assistant instance."""
from homeassistant.components.mqtt import PROTOCOL_311
config = {
'listeners': {
'default': {
'max-connections': 50000,
'bind': '0.0.0.0:1883',
'type': 'tcp',
},
'ws-1': {
'bind': '0.0.0.0:8080',
'type': 'ws',
},
},
'auth': {
'allow-anonymous': hass.config.api.api_password is None,
'home-assistant': hass,
'plugins': ['auth_home_assistant', 'auth_anonymous']
},
}

if hass.config.api.api_password:
username = 'homeassistant'
password = hass.config.api.api_password
else:
username = None
password = None

client_config = ('localhost', 1883, username, password, None, PROTOCOL_311)

return config, client_config


def generate_config_legacy(hass, passwd, password):
"""Generate a configuration based on current Home Assistant instance."""
from . import PROTOCOL_311

Expand Down