Asyncio event helpers (#3415)

* Automation - Event: Use coroutine

* Convert event helpers to coroutine

* Fix linting

* Add hass.async_add_job

* Automation - Event to use async_add_job
This commit is contained in:
Paulus Schoutsen 2016-09-17 18:28:01 -07:00 committed by GitHub
parent 4076ccf639
commit aca375c312
3 changed files with 31 additions and 10 deletions

View file

@ -6,6 +6,7 @@ of entities and react to changes.
"""
# pylint: disable=unused-import, too-many-lines
import asyncio
from concurrent.futures import ThreadPoolExecutor
import enum
import functools as ft
import logging
@ -13,8 +14,8 @@ import os
import re
import signal
import sys
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from types import MappingProxyType
@ -205,6 +206,17 @@ class HomeAssistant(object):
"""
self.pool.add_job(priority, (target,) + args)
def async_add_job(self, target: Callable[..., None], *args: Any):
"""Add a job from within the eventloop.
target: target to call.
args: parameters for method to call.
"""
if asyncio.iscoroutinefunction(target):
self.loop.create_task(target(*args))
else:
self.add_job(target, *args)
def _loop_empty(self):
"""Python 3.4.2 empty loop compatibility function."""
# pylint: disable=protected-access
@ -217,8 +229,6 @@ class HomeAssistant(object):
def block_till_done(self):
"""Block till all pending work is done."""
import threading
complete = threading.Event()
@asyncio.coroutine