Async bootstrap / component init (#3991)

* Async bootstrap

* Adress comments

* Fix tests

* More fixes

* Tests fixes
This commit is contained in:
Pascal Vizeli authored and Paulus Schoutsen committed 2016-10-27 00:16:23 -07:00
1 parent d9999f36e8
commit d5368f6f78
54 files changed
+1431 -1131

No files matched your search

@@ -10,12 +10,13 @@ import logging
import voluptuous as vol
from homeassistant.core import callback
from homeassistant.exceptions import TemplateError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.util import slugify
from homeassistant.config import load_yaml_config_file
from homeassistant.util.async import run_coroutine_threadsafe
from homeassistant.util.async import run_callback_threadsafe
DOMAIN = 'persistent_notification'
ENTITY_ID_FORMAT = DOMAIN + '.{}'
@@ -38,12 +39,12 @@ _LOGGER = logging.getLogger(__name__)
def create(hass, message, title=None, notification_id=None):
"""Generate a notification."""
run_coroutine_threadsafe(
async_create(hass, message, title, notification_id), hass.loop
run_callback_threadsafe(
hass.loop, async_create, hass, message, title, notification_id
).result()
@asyncio.coroutine
@callback
def async_create(hass, message, title=None, notification_id=None):
"""Generate a notification."""
data = {
@@ -54,11 +55,14 @@ def async_create(hass, message, title=None, notification_id=None):
] if value is not None
}
yield from hass.services.async_call(DOMAIN, SERVICE_CREATE, data)
hass.loop.create_task(
hass.services.async_call(DOMAIN, SERVICE_CREATE, data))
def setup(hass, config):
@asyncio.coroutine
def async_setup(hass, config):
"""Setup the persistent notification component."""
@callback
def create_service(call):
"""Handle a create notification service call."""
title = call.data.get(ATTR_TITLE)
@@ -68,13 +72,13 @@ def setup(hass, config):
if notification_id is not None:
entity_id = ENTITY_ID_FORMAT.format(slugify(notification_id))
else:
entity_id = generate_entity_id(ENTITY_ID_FORMAT, DEFAULT_OBJECT_ID,
hass=hass)
entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, DEFAULT_OBJECT_ID, hass=hass)
attr = {}
if title is not None:
try:
title.hass = hass
title = title.render()
title = title.async_render()
except TemplateError as ex:
_LOGGER.error('Error rendering title %s: %s', title, ex)
title = title.template
@@ -83,17 +87,19 @@ def setup(hass, config):
try:
message.hass = hass
message = message.render()
message = message.async_render()
except TemplateError as ex:
_LOGGER.error('Error rendering message %s: %s', message, ex)
message = message.template
hass.states.set(entity_id, message, attr)
hass.states.async_set(entity_id, message, attr)
descriptions = load_yaml_config_file(
os.path.join(os.path.dirname(__file__), 'services.yaml'))
hass.services.register(DOMAIN, SERVICE_CREATE, create_service,
descriptions[DOMAIN][SERVICE_CREATE],
SCHEMA_SERVICE_CREATE)
descriptions = yield from hass.loop.run_in_executor(
None, load_yaml_config_file, os.path.join(
os.path.dirname(__file__), 'services.yaml')
)
hass.services.async_register(DOMAIN, SERVICE_CREATE, create_service,
descriptions[DOMAIN][SERVICE_CREATE],
SCHEMA_SERVICE_CREATE)
return True