mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Add mqtt.async_subscribe
This commit is contained in:
parent
b8504f8fc8
commit
16ff68ca84
@ -21,6 +21,7 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP,
|
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP,
|
||||||
CONF_PLATFORM, CONF_SCAN_INTERVAL, CONF_VALUE_TEMPLATE)
|
CONF_PLATFORM, CONF_SCAN_INTERVAL, CONF_VALUE_TEMPLATE)
|
||||||
|
from homeassistant.util.async import run_callback_threadsafe
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -165,6 +166,18 @@ def publish_template(hass, topic, payload_template, qos=None, retain=None):
|
|||||||
|
|
||||||
|
|
||||||
def subscribe(hass, topic, callback, qos=DEFAULT_QOS):
|
def subscribe(hass, topic, callback, qos=DEFAULT_QOS):
|
||||||
|
"""Subscribe to an MQTT topic."""
|
||||||
|
async_remove = run_callback_threadsafe(
|
||||||
|
hass.loop, async_subscribe, hass, topic, callback, qos).result()
|
||||||
|
|
||||||
|
def remove_mqtt():
|
||||||
|
"""Remove MQTT subscription."""
|
||||||
|
run_callback_threadsafe(hass.loop, async_remove).result()
|
||||||
|
|
||||||
|
return remove_mqtt
|
||||||
|
|
||||||
|
|
||||||
|
def async_subscribe(hass, topic, callback, qos=DEFAULT_QOS):
|
||||||
"""Subscribe to an MQTT topic."""
|
"""Subscribe to an MQTT topic."""
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def mqtt_topic_subscriber(event):
|
def mqtt_topic_subscriber(event):
|
||||||
@ -181,13 +194,13 @@ def subscribe(hass, topic, callback, qos=DEFAULT_QOS):
|
|||||||
event.data[ATTR_PAYLOAD], event.data[ATTR_QOS],
|
event.data[ATTR_PAYLOAD], event.data[ATTR_QOS],
|
||||||
priority=JobPriority.EVENT_CALLBACK)
|
priority=JobPriority.EVENT_CALLBACK)
|
||||||
|
|
||||||
remove = hass.bus.listen(EVENT_MQTT_MESSAGE_RECEIVED,
|
async_remove = hass.bus.async_listen(EVENT_MQTT_MESSAGE_RECEIVED,
|
||||||
mqtt_topic_subscriber)
|
mqtt_topic_subscriber)
|
||||||
|
|
||||||
# Future: track subscriber count and unsubscribe in remove
|
# Future: track subscriber count and unsubscribe in remove
|
||||||
MQTT_CLIENT.subscribe(topic, qos)
|
MQTT_CLIENT.subscribe(topic, qos)
|
||||||
|
|
||||||
return remove
|
return async_remove
|
||||||
|
|
||||||
|
|
||||||
def _setup_server(hass, config):
|
def _setup_server(hass, config):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user