Automation: call prepare_setup_platform in executor

This commit is contained in:
Paulus Schoutsen 2016-10-01 14:11:07 -07:00
parent e18825ba20
commit 56fdc2a625

View File

@ -198,10 +198,10 @@ class AutomationEntity(ToggleEntity):
"""Initialize an automation entity.""" """Initialize an automation entity."""
self._name = name self._name = name
self._async_attach_triggers = async_attach_triggers self._async_attach_triggers = async_attach_triggers
self._async_detach_triggers = async_attach_triggers(self.async_trigger) self._async_detach_triggers = None
self._cond_func = cond_func self._cond_func = cond_func
self._async_action = async_action self._async_action = async_action
self._enabled = True self._enabled = False
self._last_triggered = None self._last_triggered = None
self._hidden = hidden self._hidden = hidden
@ -234,13 +234,8 @@ class AutomationEntity(ToggleEntity):
@asyncio.coroutine @asyncio.coroutine
def async_turn_on(self, **kwargs) -> None: def async_turn_on(self, **kwargs) -> None:
"""Turn the entity on.""" """Turn the entity on and update the state."""
if self._enabled: yield from self.async_enable()
return
self._async_detach_triggers = self._async_attach_triggers(
self.async_trigger)
self._enabled = True
yield from self.async_update_ha_state() yield from self.async_update_ha_state()
@asyncio.coroutine @asyncio.coroutine
@ -276,6 +271,16 @@ class AutomationEntity(ToggleEntity):
self.hass.loop).result() self.hass.loop).result()
super().remove() super().remove()
@asyncio.coroutine
def async_enable(self):
"""Enable this automation entity."""
if self._enabled:
return
self._async_detach_triggers = yield from self._async_attach_triggers(
self.async_trigger)
self._enabled = True
@asyncio.coroutine @asyncio.coroutine
def _async_process_config(hass, config, component): def _async_process_config(hass, config, component):
@ -307,8 +312,10 @@ def _async_process_config(hass, config, component):
async_attach_triggers = partial( async_attach_triggers = partial(
_async_process_trigger, hass, config, _async_process_trigger, hass, config,
config_block.get(CONF_TRIGGER, []), name) config_block.get(CONF_TRIGGER, []), name)
entities.append(AutomationEntity(name, async_attach_triggers, entity = AutomationEntity(name, async_attach_triggers, cond_func,
cond_func, action, hidden)) action, hidden)
yield from entity.async_enable()
entities.append(entity)
yield from hass.loop.run_in_executor( yield from hass.loop.run_in_executor(
None, component.add_entities, entities) None, component.add_entities, entities)
@ -349,13 +356,16 @@ def _async_process_if(hass, config, p_config):
return if_action return if_action
@asyncio.coroutine
def _async_process_trigger(hass, config, trigger_configs, name, action): def _async_process_trigger(hass, config, trigger_configs, name, action):
"""Setup the triggers.""" """Setup the triggers."""
removes = [] removes = []
for conf in trigger_configs: for conf in trigger_configs:
platform = prepare_setup_platform(hass, config, DOMAIN, platform = yield from hass.loop.run_in_executor(
conf.get(CONF_PLATFORM)) None, prepare_setup_platform, hass, config, DOMAIN,
conf.get(CONF_PLATFORM))
if platform is None: if platform is None:
return None return None