Convert mqtt platforms to async (#6145)
* Convert mqtt platforms to async * fix lint * add more platforms * convert mqtt_eventstream * fix lint / add mqtt_room * fix lint * fix test part 1 * fix test part 2 * fix out of memory bug * address comments
This commit is contained in:
1 parent
65ed85c6eb
commit
b0d3bbed79
15 files changed
+509
-291
No files matched your search
@@ -172,10 +172,16 @@ def _build_publish_data(topic, qos, retain):
|
||||
|
||||
|
||||
def publish(hass, topic, payload, qos=None, retain=None):
|
||||
"""Publish message to an MQTT topic."""
|
||||
hass.add_job(async_publish, hass, topic, payload, qos, retain)
|
||||
|
||||
|
||||
@callback
|
||||
def async_publish(hass, topic, payload, qos=None, retain=None):
|
||||
"""Publish message to an MQTT topic."""
|
||||
data = _build_publish_data(topic, qos, retain)
|
||||
data[ATTR_PAYLOAD] = payload
|
||||
hass.services.call(DOMAIN, SERVICE_PUBLISH, data)
|
||||
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_PUBLISH, data))
|
||||
|
||||
|
||||
def publish_template(hass, topic, payload_template, qos=None, retain=None):
|
||||
@@ -387,6 +393,8 @@ class MQTT(object):
|
||||
self.progress = {}
|
||||
self.birth_message = birth_message
|
||||
self._mqttc = None
|
||||
self._subscribe_lock = asyncio.Lock(loop=hass.loop)
|
||||
self._publish_lock = asyncio.Lock(loop=hass.loop)
|
||||
|
||||
if protocol == PROTOCOL_31:
|
||||
proto = mqtt.MQTTv31
|
||||
@@ -426,8 +434,9 @@ class MQTT(object):
|
||||
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
yield from self.hass.loop.run_in_executor(
|
||||
None, self._mqttc.publish, topic, payload, qos, retain)
|
||||
with (yield from self._publish_lock):
|
||||
yield from self.hass.loop.run_in_executor(
|
||||
None, self._mqttc.publish, topic, payload, qos, retain)
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_connect(self):
|
||||
@@ -474,8 +483,10 @@ class MQTT(object):
|
||||
|
||||
if topic in self.topics:
|
||||
return
|
||||
result, mid = yield from self.hass.loop.run_in_executor(
|
||||
None, self._mqttc.subscribe, topic, qos)
|
||||
|
||||
with (yield from self._subscribe_lock):
|
||||
result, mid = yield from self.hass.loop.run_in_executor(
|
||||
None, self._mqttc.subscribe, topic, qos)
|
||||
|
||||
_raise_on_error(result)
|
||||
self.progress[mid] = topic
|
||||
|
||||
Reference in new issue
Block a user