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,13 +4,14 @@ 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
from collections import namedtuple
from homeassistant.components.scene import Scene
from homeassistant.const import (
ATTR_ENTITY_ID, STATE_OFF, STATE_ON)
from homeassistant.core import State
from homeassistant.helpers.state import reproduce_state
from homeassistant.helpers.state import async_reproduce_state
DEPENDENCIES = ['group']
STATE = 'scening'
@ -20,21 +21,24 @@ CONF_ENTITIES = "entities"
SceneConfig = namedtuple('SceneConfig', ['name', 'states'])
def setup_platform(hass, config, add_devices, discovery_info=None):
@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Setup home assistant scene entries."""
scene_config = config.get("states")
if not isinstance(scene_config, list):
scene_config = [scene_config]
add_devices(HomeAssistantScene(hass, _process_config(scene))
for scene in scene_config)
yield from async_add_devices(HomeAssistantScene(
hass, _process_config(scene)) for scene in scene_config)
return True
def _process_config(scene_config):
"""Process passed in config into a format to work with."""
"""Process passed in config into a format to work with.
Async friendly.
"""
name = scene_config.get('name')
states = {}
@ -81,6 +85,8 @@ class HomeAssistantScene(Scene):
ATTR_ENTITY_ID: list(self.scene_config.states.keys()),
}
def activate(self):
@asyncio.coroutine
def async_activate(self):
"""Activate scene. Try to get entities into requested state."""
reproduce_state(self.hass, self.scene_config.states.values(), True)
yield from async_reproduce_state(
self.hass, self.scene_config.states.values(), True)