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:
Martin Hjelmare 2017-03-29 08:39:53 +02:00 committed by Paulus Schoutsen
parent d1b519a418
commit 7c614a6738
3 changed files with 115 additions and 75 deletions

View file

@ -1,9 +1,11 @@
"""The tests for the Scene component."""
import io
import unittest
from homeassistant.setup import setup_component
from homeassistant import loader
from homeassistant.components import light, scene
from homeassistant.util import yaml
from tests.common import get_test_home_assistant
@ -14,6 +16,22 @@ class TestScene(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
test_light = loader.get_component('light.test')
test_light.init()
self.assertTrue(setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {'platform': 'test'}
}))
self.light_1, self.light_2 = test_light.DEVICES[0:2]
light.turn_off(
self.hass, [self.light_1.entity_id, self.light_2.entity_id])
self.hass.block_till_done()
self.assertFalse(self.light_1.is_on)
self.assertFalse(self.light_2.is_on)
def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""
@ -36,19 +54,6 @@ class TestScene(unittest.TestCase):
reference to the original dictionary, instead of creating a copy, so
care needs to be taken to not modify the original.
"""
test_light = loader.get_component('light.test')
test_light.init()
self.assertTrue(setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {'platform': 'test'}
}))
light_1, light_2 = test_light.DEVICES[0:2]
light.turn_off(self.hass, [light_1.entity_id, light_2.entity_id])
self.hass.block_till_done()
entity_state = {
'state': 'on',
'brightness': 100,
@ -57,8 +62,8 @@ class TestScene(unittest.TestCase):
'scene': [{
'name': 'test',
'entities': {
light_1.entity_id: entity_state,
light_2.entity_id: entity_state,
self.light_1.entity_id: entity_state,
self.light_2.entity_id: entity_state,
}
}]
}))
@ -66,34 +71,45 @@ class TestScene(unittest.TestCase):
scene.activate(self.hass, 'scene.test')
self.hass.block_till_done()
self.assertTrue(light_1.is_on)
self.assertTrue(light_2.is_on)
self.assertEqual(100,
light_1.last_call('turn_on')[1].get('brightness'))
self.assertEqual(100,
light_2.last_call('turn_on')[1].get('brightness'))
self.assertTrue(self.light_1.is_on)
self.assertTrue(self.light_2.is_on)
self.assertEqual(
100, self.light_1.last_call('turn_on')[1].get('brightness'))
self.assertEqual(
100, self.light_2.last_call('turn_on')[1].get('brightness'))
def test_config_yaml_bool(self):
"""Test parsing of booleans in yaml config."""
config = (
'scene:\n'
' - name: test\n'
' entities:\n'
' {0}: on\n'
' {1}:\n'
' state: on\n'
' brightness: 100\n').format(
self.light_1.entity_id, self.light_2.entity_id)
with io.StringIO(config) as file:
doc = yaml.yaml.safe_load(file)
self.assertTrue(setup_component(self.hass, scene.DOMAIN, doc))
scene.activate(self.hass, 'scene.test')
self.hass.block_till_done()
self.assertTrue(self.light_1.is_on)
self.assertTrue(self.light_2.is_on)
self.assertEqual(
100, self.light_2.last_call('turn_on')[1].get('brightness'))
def test_activate_scene(self):
"""Test active scene."""
test_light = loader.get_component('light.test')
test_light.init()
self.assertTrue(setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {'platform': 'test'}
}))
light_1, light_2 = test_light.DEVICES[0:2]
light.turn_off(self.hass, [light_1.entity_id, light_2.entity_id])
self.hass.block_till_done()
self.assertTrue(setup_component(self.hass, scene.DOMAIN, {
'scene': [{
'name': 'test',
'entities': {
light_1.entity_id: 'on',
light_2.entity_id: {
self.light_1.entity_id: 'on',
self.light_2.entity_id: {
'state': 'on',
'brightness': 100,
}
@ -104,7 +120,7 @@ class TestScene(unittest.TestCase):
scene.activate(self.hass, 'scene.test')
self.hass.block_till_done()
self.assertTrue(light_1.is_on)
self.assertTrue(light_2.is_on)
self.assertEqual(100,
light_2.last_call('turn_on')[1].get('brightness'))
self.assertTrue(self.light_1.is_on)
self.assertTrue(self.light_2.is_on)
self.assertEqual(
100, self.light_2.last_call('turn_on')[1].get('brightness'))