Add voluptuous config validation to scenes (#6830)
* Add platform schema to scene component and homeassistant platform. * Clean up code and add constants. * Add unit test and clean up tests.
This commit is contained in:
parent
d1b519a418
commit
7c614a6738
3 changed files with 115 additions and 75 deletions
|
@ -7,26 +7,38 @@ https://home-assistant.io/components/scene/
|
|||
import asyncio
|
||||
from collections import namedtuple
|
||||
|
||||
from homeassistant.components.scene import Scene
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.scene import Scene, STATES
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, STATE_OFF, STATE_ON)
|
||||
ATTR_ENTITY_ID, ATTR_STATE, CONF_ENTITIES, CONF_NAME, CONF_PLATFORM,
|
||||
STATE_OFF, STATE_ON)
|
||||
from homeassistant.core import State
|
||||
from homeassistant.helpers.state import async_reproduce_state
|
||||
from homeassistant.helpers.state import async_reproduce_state, HASS_DOMAIN
|
||||
|
||||
STATE = 'scening'
|
||||
PLATFORM_SCHEMA = vol.Schema({
|
||||
vol.Required(CONF_PLATFORM): HASS_DOMAIN,
|
||||
vol.Required(STATES): vol.All(
|
||||
cv.ensure_list,
|
||||
[
|
||||
{
|
||||
vol.Required(CONF_NAME): cv.string,
|
||||
vol.Required(CONF_ENTITIES): {
|
||||
cv.entity_id: vol.Any(str, bool, dict)
|
||||
},
|
||||
}
|
||||
]
|
||||
),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
CONF_ENTITIES = "entities"
|
||||
|
||||
SceneConfig = namedtuple('SceneConfig', ['name', 'states'])
|
||||
SCENECONFIG = namedtuple('SceneConfig', [CONF_NAME, STATES])
|
||||
|
||||
|
||||
@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]
|
||||
scene_config = config.get(STATES)
|
||||
|
||||
async_add_devices(HomeAssistantScene(
|
||||
hass, _process_config(scene)) for scene in scene_config)
|
||||
|
@ -38,7 +50,7 @@ def _process_config(scene_config):
|
|||
|
||||
Async friendly.
|
||||
"""
|
||||
name = scene_config.get('name')
|
||||
name = scene_config.get(CONF_NAME)
|
||||
|
||||
states = {}
|
||||
c_entities = dict(scene_config.get(CONF_ENTITIES, {}))
|
||||
|
@ -46,7 +58,7 @@ def _process_config(scene_config):
|
|||
for entity_id in c_entities:
|
||||
if isinstance(c_entities[entity_id], dict):
|
||||
entity_attrs = c_entities[entity_id].copy()
|
||||
state = entity_attrs.pop('state', None)
|
||||
state = entity_attrs.pop(ATTR_STATE, None)
|
||||
attributes = entity_attrs
|
||||
else:
|
||||
state = c_entities[entity_id]
|
||||
|
@ -61,7 +73,7 @@ def _process_config(scene_config):
|
|||
|
||||
states[entity_id.lower()] = State(entity_id, state, attributes)
|
||||
|
||||
return SceneConfig(name, states)
|
||||
return SCENECONFIG(name, states)
|
||||
|
||||
|
||||
class HomeAssistantScene(Scene):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue