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:
1 parent
d1b519a418
commit
7c614a6738
3 files changed
+115
-75
No files matched your search
@@ -6,28 +6,55 @@ https://home-assistant.io/components/scene/
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
from collections import namedtuple
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, SERVICE_TURN_ON, CONF_PLATFORM)
|
||||
from homeassistant.helpers import extract_domain_configs
|
||||
ATTR_ENTITY_ID, CONF_PLATFORM, SERVICE_TURN_ON)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.state import HASS_DOMAIN
|
||||
from homeassistant.loader import get_platform
|
||||
|
||||
DOMAIN = 'scene'
|
||||
STATE = 'scening'
|
||||
STATES = 'states'
|
||||
|
||||
CONF_ENTITIES = "entities"
|
||||
|
||||
def _hass_domain_validator(config):
|
||||
"""Validate platform in config for homeassistant domain."""
|
||||
if CONF_PLATFORM not in config:
|
||||
config = {
|
||||
CONF_PLATFORM: HASS_DOMAIN, STATES: config}
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def _platform_validator(config):
|
||||
"""Validate it is a valid platform."""
|
||||
p_name = config[CONF_PLATFORM]
|
||||
platform = get_platform(DOMAIN, p_name)
|
||||
|
||||
if not hasattr(platform, 'PLATFORM_SCHEMA'):
|
||||
return config
|
||||
|
||||
return getattr(platform, 'PLATFORM_SCHEMA')(config)
|
||||
|
||||
|
||||
PLATFORM_SCHEMA = vol.Schema(
|
||||
vol.All(
|
||||
_hass_domain_validator,
|
||||
vol.Schema({
|
||||
vol.Required(CONF_PLATFORM): cv.platform_validator(DOMAIN)
|
||||
}, extra=vol.ALLOW_EXTRA),
|
||||
_platform_validator
|
||||
), extra=vol.ALLOW_EXTRA)
|
||||
|
||||
SCENE_SERVICE_SCHEMA = vol.Schema({
|
||||
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
||||
})
|
||||
|
||||
SceneConfig = namedtuple('SceneConfig', ['name', 'states'])
|
||||
|
||||
|
||||
def activate(hass, entity_id=None):
|
||||
"""Activate a scene."""
|
||||
@@ -43,21 +70,6 @@ def activate(hass, entity_id=None):
|
||||
def async_setup(hass, config):
|
||||
"""Setup scenes."""
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# You are not allowed to mutate the original config so make a copy
|
||||
config = dict(config)
|
||||
|
||||
for config_key in extract_domain_configs(config, DOMAIN):
|
||||
platform_config = config[config_key]
|
||||
if not isinstance(platform_config, list):
|
||||
platform_config = [platform_config]
|
||||
|
||||
if not any(CONF_PLATFORM in entry for entry in platform_config):
|
||||
platform_config = [{'platform': 'homeassistant', 'states': entry}
|
||||
for entry in platform_config]
|
||||
|
||||
config[config_key] = platform_config
|
||||
|
||||
component = EntityComponent(logger, DOMAIN, hass)
|
||||
|
||||
yield from component.async_setup(config)
|
||||
|
||||
Reference in new issue
Block a user