2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Scene component."""
|
2017-03-29 06:39:53 +00:00
|
|
|
import io
|
2015-10-12 06:48:17 +00:00
|
|
|
import unittest
|
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2015-10-12 06:48:17 +00:00
|
|
|
from homeassistant.components import light, scene
|
2017-03-29 06:39:53 +00:00
|
|
|
from homeassistant.util import yaml
|
2015-10-12 06:48:17 +00:00
|
|
|
|
|
|
|
from tests.common import get_test_home_assistant
|
2018-09-27 21:13:11 +00:00
|
|
|
from tests.components.light import common as common_light
|
2018-09-26 16:02:05 +00:00
|
|
|
from tests.components.scene import common
|
2015-10-12 06:48:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestScene(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the scene component."""
|
2015-10-12 06:48:17 +00:00
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2015-10-12 06:48:17 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2019-04-15 05:31:01 +00:00
|
|
|
test_light = getattr(self.hass.components, 'test.light')
|
2017-03-29 06:39:53 +00:00
|
|
|
test_light.init()
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert setup_component(self.hass, light.DOMAIN, {
|
2017-03-29 06:39:53 +00:00
|
|
|
light.DOMAIN: {'platform': 'test'}
|
2018-10-24 10:10:05 +00:00
|
|
|
})
|
2017-03-29 06:39:53 +00:00
|
|
|
|
|
|
|
self.light_1, self.light_2 = test_light.DEVICES[0:2]
|
|
|
|
|
2018-09-27 21:13:11 +00:00
|
|
|
common_light.turn_off(
|
2017-03-29 06:39:53 +00:00
|
|
|
self.hass, [self.light_1.entity_id, self.light_2.entity_id])
|
|
|
|
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert not self.light_1.is_on
|
|
|
|
assert not self.light_2.is_on
|
2015-10-12 06:48:17 +00:00
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2015-10-12 06:48:17 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2015-12-27 19:24:34 +00:00
|
|
|
def test_config_yaml_alias_anchor(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the usage of YAML aliases and anchors.
|
|
|
|
|
|
|
|
The following test scene configuration is equivalent to:
|
2015-12-27 19:24:34 +00:00
|
|
|
|
|
|
|
scene:
|
|
|
|
- name: test
|
|
|
|
entities:
|
|
|
|
light_1: &light_1_state
|
|
|
|
state: 'on'
|
|
|
|
brightness: 100
|
|
|
|
light_2: *light_1_state
|
|
|
|
|
|
|
|
When encountering a YAML alias/anchor, the PyYAML parser will use a
|
|
|
|
reference to the original dictionary, instead of creating a copy, so
|
|
|
|
care needs to be taken to not modify the original.
|
|
|
|
"""
|
|
|
|
entity_state = {
|
|
|
|
'state': 'on',
|
|
|
|
'brightness': 100,
|
|
|
|
}
|
2018-10-24 10:10:05 +00:00
|
|
|
assert setup_component(self.hass, scene.DOMAIN, {
|
2015-12-27 19:24:34 +00:00
|
|
|
'scene': [{
|
|
|
|
'name': 'test',
|
|
|
|
'entities': {
|
2017-03-29 06:39:53 +00:00
|
|
|
self.light_1.entity_id: entity_state,
|
|
|
|
self.light_2.entity_id: entity_state,
|
2015-12-27 19:24:34 +00:00
|
|
|
}
|
|
|
|
}]
|
2018-10-24 10:10:05 +00:00
|
|
|
})
|
2015-12-27 19:24:34 +00:00
|
|
|
|
2018-09-26 16:02:05 +00:00
|
|
|
common.activate(self.hass, 'scene.test')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-12-27 19:24:34 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.light_1.is_on
|
|
|
|
assert self.light_2.is_on
|
|
|
|
assert 100 == self.light_1.last_call('turn_on')[1].get('brightness')
|
|
|
|
assert 100 == self.light_2.last_call('turn_on')[1].get('brightness')
|
2017-03-29 06:39:53 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert setup_component(self.hass, scene.DOMAIN, doc)
|
2018-09-26 16:02:05 +00:00
|
|
|
common.activate(self.hass, 'scene.test')
|
2017-03-29 06:39:53 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.light_1.is_on
|
|
|
|
assert self.light_2.is_on
|
|
|
|
assert 100 == self.light_2.last_call('turn_on')[1].get('brightness')
|
2015-12-27 19:24:34 +00:00
|
|
|
|
2015-10-12 06:48:17 +00:00
|
|
|
def test_activate_scene(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test active scene."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert setup_component(self.hass, scene.DOMAIN, {
|
2015-10-12 06:48:17 +00:00
|
|
|
'scene': [{
|
|
|
|
'name': 'test',
|
|
|
|
'entities': {
|
2017-03-29 06:39:53 +00:00
|
|
|
self.light_1.entity_id: 'on',
|
|
|
|
self.light_2.entity_id: {
|
2015-10-12 06:48:17 +00:00
|
|
|
'state': 'on',
|
|
|
|
'brightness': 100,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
2018-10-24 10:10:05 +00:00
|
|
|
})
|
2015-10-12 06:48:17 +00:00
|
|
|
|
2018-09-26 16:02:05 +00:00
|
|
|
common.activate(self.hass, 'scene.test')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-10-12 06:48:17 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.light_1.is_on
|
|
|
|
assert self.light_2.is_on
|
|
|
|
assert 100 == self.light_2.last_call('turn_on')[1].get('brightness')
|