Spread async love (#3575)
* Convert Entity.update_ha_state to be async * Make Service.call async * Update entity.py * Add Entity.async_update * Make automation zone trigger async * Fix linting * Reduce flakiness in hass.block_till_done * Make automation.numeric_state async * Make mqtt.subscribe async * Make automation.mqtt async * Make automation.time async * Make automation.sun async * Add async_track_point_in_utc_time * Make helpers.track_sunrise/set async * Add async_track_state_change * Make automation.state async * Clean up helpers/entity.py tests * Lint * Lint * Core.is_state and Core.is_state_attr are async friendly * Lint * Lint
This commit is contained in:
1 parent
7e50ccd32a
commit
b650b2b0db
17 files changed
+323
-151
No files matched your search
@@ -4,6 +4,7 @@ Support for MQTT message handling.
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/mqtt/
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
@@ -11,6 +12,7 @@ import time
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import JobPriority
|
||||
from homeassistant.bootstrap import prepare_setup_platform
|
||||
from homeassistant.config import load_yaml_config_file
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
@@ -164,11 +166,20 @@ def publish_template(hass, topic, payload_template, qos=None, retain=None):
|
||||
|
||||
def subscribe(hass, topic, callback, qos=DEFAULT_QOS):
|
||||
"""Subscribe to an MQTT topic."""
|
||||
@asyncio.coroutine
|
||||
def mqtt_topic_subscriber(event):
|
||||
"""Match subscribed MQTT topic."""
|
||||
if _match_topic(topic, event.data[ATTR_TOPIC]):
|
||||
callback(event.data[ATTR_TOPIC], event.data[ATTR_PAYLOAD],
|
||||
event.data[ATTR_QOS])
|
||||
if not _match_topic(topic, event.data[ATTR_TOPIC]):
|
||||
return
|
||||
|
||||
if asyncio.iscoroutinefunction(callback):
|
||||
yield from callback(
|
||||
event.data[ATTR_TOPIC], event.data[ATTR_PAYLOAD],
|
||||
event.data[ATTR_QOS])
|
||||
else:
|
||||
hass.add_job(callback, event.data[ATTR_TOPIC],
|
||||
event.data[ATTR_PAYLOAD], event.data[ATTR_QOS],
|
||||
priority=JobPriority.EVENT_CALLBACK)
|
||||
|
||||
remove = hass.bus.listen(EVENT_MQTT_MESSAGE_RECEIVED,
|
||||
mqtt_topic_subscriber)
|
||||
|
||||
Reference in new issue
Block a user