Bugfix mqtt socket memory error (#6501)

* Bugfix mqtt socket memory error

* Fix tests

* Fix lint
This commit is contained in:
Pascal Vizeli authored and Paulus Schoutsen committed 2017-03-09 06:31:43 -08:00
1 parent 855756cb2a
commit 20fcd1f0e2
3 files changed
+15 -34

No files matched your search

+9 -23
View File
@@ -23,8 +23,8 @@ from homeassistant.helpers.dispatcher import (
from homeassistant.util.async import (
run_coroutine_threadsafe, run_callback_threadsafe)
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, CONF_VALUE_TEMPLATE,
CONF_USERNAME, CONF_PASSWORD, CONF_PORT, CONF_PROTOCOL, CONF_PAYLOAD)
EVENT_HOMEASSISTANT_STOP, CONF_VALUE_TEMPLATE, CONF_USERNAME,
CONF_PASSWORD, CONF_PORT, CONF_PROTOCOL, CONF_PAYLOAD)
from homeassistant.components.mqtt.server import HBMQTT_CONFIG_SCHEMA
_LOGGER = logging.getLogger(__name__)
@@ -331,18 +331,11 @@ def async_setup(hass, config):
@asyncio.coroutine
def async_stop_mqtt(event):
"""Stop MQTT component."""
yield from hass.data[DATA_MQTT].async_stop()
yield from hass.data[DATA_MQTT].async_disconnect()
@asyncio.coroutine
def async_start_mqtt(event):
"""Launch MQTT component when Home Assistant starts up."""
yield from hass.data[DATA_MQTT].async_start()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop_mqtt)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, async_start_mqtt)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop_mqtt)
success = yield from hass.data[DATA_MQTT].async_connect()
if not success:
return False
@@ -442,13 +435,12 @@ class MQTT(object):
with (yield from self._paho_lock):
yield from self.hass.loop.run_in_executor(
None, self._mqttc.publish, topic, payload, qos, retain)
yield from asyncio.sleep(0, loop=self.hass.loop)
@asyncio.coroutine
def async_connect(self):
"""Connect to the host. Does not process messages yet.
"""Connect to the host. Does process messages yet.
This method must be run in the event loop and returns a coroutine.
This method is a coroutine.
"""
result = yield from self.hass.loop.run_in_executor(
None, self._mqttc.connect, self.broker, self.port, self.keepalive)
@@ -456,17 +448,12 @@ class MQTT(object):
if result != 0:
import paho.mqtt.client as mqtt
_LOGGER.error('Failed to connect: %s', mqtt.error_string(result))
else:
self._mqttc.loop_start()
return not result
def async_start(self):
"""Run the MQTT client.
This method must be run in the event loop and returns a coroutine.
"""
return self.hass.loop.run_in_executor(None, self._mqttc.loop_start)
def async_stop(self):
def async_disconnect(self):
"""Stop the MQTT client.
This method must be run in the event loop and returns a coroutine.
@@ -493,7 +480,6 @@ class MQTT(object):
result, mid = yield from self.hass.loop.run_in_executor(
None, self._mqttc.subscribe, topic, qos)
yield from asyncio.sleep(0, loop=self.hass.loop)
_raise_on_error(result)
self.progress[mid] = topic
+1 -1
View File
@@ -51,7 +51,7 @@ def async_dispatcher_connect(hass, signal, target):
def dispatcher_send(hass, signal, *args):
"""Send signal and data."""
hass.add_job(async_dispatcher_send, hass, signal, *args)
hass.loop.call_soon_threadsafe(async_dispatcher_send, hass, signal, *args)
@callback