Add timeout to platform/component

pull/4300/head
Pascal Vizeli 2016-11-07 22:22:57 +01:00
parent b4159c7dc9
commit 280a311e48
2 changed files with 21 additions and 15 deletions

View File

@ -9,6 +9,7 @@ from collections import defaultdict
from types import ModuleType
from typing import Any, Optional, Dict
import async_timeout
import voluptuous as vol
from voluptuous.humanize import humanize_error
@ -142,11 +143,12 @@ def _async_setup_component(hass: core.HomeAssistant,
async_comp = hasattr(component, 'async_setup')
try:
if async_comp:
result = yield from component.async_setup(hass, config)
else:
result = yield from hass.loop.run_in_executor(
None, component.setup, hass, config)
with async_timeout.timeout(30, loop=hass.loop):
if async_comp:
result = yield from component.async_setup(hass, config)
else:
result = yield from hass.loop.run_in_executor(
None, component.setup, hass, config)
except Exception: # pylint: disable=broad-except
_LOGGER.exception('Error during setup of component %s', domain)
_async_persistent_notification(hass, domain, True)

View File

@ -1,6 +1,8 @@
"""Helpers for components that manage entities."""
import asyncio
import async_timeout
from homeassistant import config as conf_util
from homeassistant.bootstrap import (
async_prepare_setup_platform, async_prepare_setup_component)
@ -138,16 +140,18 @@ class EntityComponent(object):
entity_platform = self._platforms[key]
try:
if getattr(platform, 'async_setup_platform', None):
yield from platform.async_setup_platform(
self.hass, platform_config,
entity_platform.async_add_entities, discovery_info
)
else:
yield from self.hass.loop.run_in_executor(
None, platform.setup_platform, self.hass, platform_config,
entity_platform.add_entities, discovery_info
)
with async_timeout.timeout(30, loop=self.hass.loop):
if getattr(platform, 'async_setup_platform', None):
yield from platform.async_setup_platform(
self.hass, platform_config,
entity_platform.async_add_entities, discovery_info
)
else:
yield from self.hass.loop.run_in_executor(
None, platform.setup_platform, self.hass,
platform_config, entity_platform.add_entities,
discovery_info
)
self.hass.config.components.append(
'{}.{}'.format(self.domain, platform_type))