WIP: Migrate scene to async + homeassistant scene async (#4665)

* Migrate scene to async + homeassistant scene async

* fix lint

* Update state.py

* Fix tests
This commit is contained in:
Pascal Vizeli 2016-12-02 06:38:12 +01:00 committed by Paulus Schoutsen
parent 49cfe38cca
commit 2e6a48ff5f
5 changed files with 51 additions and 20 deletions

View file

@ -4,6 +4,7 @@ Allow users to set and activate scenes.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/scene/
"""
import asyncio
import logging
from collections import namedtuple
@ -39,7 +40,8 @@ def activate(hass, entity_id=None):
hass.services.call(DOMAIN, SERVICE_TURN_ON, data)
def setup(hass, config):
@asyncio.coroutine
def async_setup(hass, config):
"""Setup scenes."""
logger = logging.getLogger(__name__)
@ -59,17 +61,21 @@ def setup(hass, config):
component = EntityComponent(logger, DOMAIN, hass)
component.setup(config)
yield from component.async_setup(config)
def handle_scene_service(service):
@asyncio.coroutine
def async_handle_scene_service(service):
"""Handle calls to the switch services."""
target_scenes = component.extract_from_service(service)
target_scenes = component.async_extract_from_service(service)
print(target_scenes)
print(component.entities)
tasks = [scene.async_activate() for scene in target_scenes]
if tasks:
yield from asyncio.wait(tasks, loop=hass.loop)
for scene in target_scenes:
scene.activate()
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_scene_service,
schema=SCENE_SERVICE_SCHEMA)
hass.services.async_register(
DOMAIN, SERVICE_TURN_ON, async_handle_scene_service,
schema=SCENE_SERVICE_SCHEMA)
return True
@ -89,4 +95,12 @@ class Scene(Entity):
def activate(self):
"""Activate scene. Try to get entities into requested state."""
raise NotImplementedError
raise NotImplementedError()
@asyncio.coroutine
def async_activate(self):
"""Activate scene. Try to get entities into requested state.
This method is a coroutine.
"""
yield from self.hass.loop.run_in_executor(None, self.activate)