MQTT.Server will use HASS eventloop (#3429)
This commit is contained in:
1 parent
20af5cb5b4
commit
2bfded7153
2 files changed
+19
-48
No files matched your search
@@ -4,41 +4,21 @@ Support for a local MQTT broker.
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/mqtt/#use-the-embedded-broker
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
import tempfile
|
||||
import threading
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.components.mqtt import PROTOCOL_311
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.util.async import run_coroutine_threadsafe
|
||||
|
||||
REQUIREMENTS = ['hbmqtt==0.7.1']
|
||||
DEPENDENCIES = ['http']
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def broker_coro(loop, config):
|
||||
"""Start broker coroutine."""
|
||||
from hbmqtt.broker import Broker
|
||||
broker = Broker(config, loop)
|
||||
yield from broker.start()
|
||||
return broker
|
||||
|
||||
|
||||
def loop_run(loop, broker, shutdown_complete):
|
||||
"""Run broker and clean up when done."""
|
||||
loop.run_forever()
|
||||
# run_forever ends when stop is called because we're shutting down
|
||||
loop.run_until_complete(broker.shutdown())
|
||||
loop.close()
|
||||
shutdown_complete.set()
|
||||
|
||||
|
||||
def start(hass, server_config):
|
||||
"""Initialize MQTT Server."""
|
||||
from hbmqtt.broker import BrokerException
|
||||
|
||||
loop = asyncio.new_event_loop()
|
||||
from hbmqtt.broker import Broker, BrokerException
|
||||
|
||||
try:
|
||||
passwd = tempfile.NamedTemporaryFile()
|
||||
@@ -48,29 +28,20 @@ def start(hass, server_config):
|
||||
else:
|
||||
client_config = None
|
||||
|
||||
start_server = asyncio.gather(broker_coro(loop, server_config),
|
||||
loop=loop)
|
||||
loop.run_until_complete(start_server)
|
||||
# Result raises exception if one was raised during startup
|
||||
broker = start_server.result()[0]
|
||||
broker = Broker(server_config, hass.loop)
|
||||
run_coroutine_threadsafe(broker.start(), hass.loop).result()
|
||||
except BrokerException:
|
||||
logging.getLogger(__name__).exception('Error initializing MQTT server')
|
||||
loop.close()
|
||||
return False, None
|
||||
finally:
|
||||
passwd.close()
|
||||
|
||||
shutdown_complete = threading.Event()
|
||||
@callback
|
||||
def shutdown_mqtt_server(event):
|
||||
"""Shut down the MQTT server."""
|
||||
hass.async_add_job(broker.shutdown())
|
||||
|
||||
def shutdown(event):
|
||||
"""Gracefully shutdown MQTT broker."""
|
||||
loop.call_soon_threadsafe(loop.stop)
|
||||
shutdown_complete.wait()
|
||||
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, shutdown)
|
||||
|
||||
threading.Thread(target=loop_run, args=(loop, broker, shutdown_complete),
|
||||
name="MQTT-server").start()
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, shutdown_mqtt_server)
|
||||
|
||||
return True, client_config
|
||||
|
||||
|
||||
Reference in new issue
Block a user