forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_init.py
123 lines (99 loc) · 3.9 KB
/
test_init.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""The tests for the Scene component."""
import io
import unittest
from homeassistant.setup import setup_component
from homeassistant.components import light, scene
from homeassistant.util.yaml import loader as yaml_loader
from tests.common import get_test_home_assistant
from tests.components.light import common as common_light
from tests.components.scene import common
class TestScene(unittest.TestCase):
"""Test the scene component."""
def setUp(self): # pylint: disable=invalid-name
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
test_light = getattr(self.hass.components, 'test.light')
test_light.init()
assert setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {'platform': 'test'}
})
self.light_1, self.light_2 = test_light.DEVICES[0:2]
common_light.turn_off(
self.hass, [self.light_1.entity_id, self.light_2.entity_id])
self.hass.block_till_done()
assert not self.light_1.is_on
assert not self.light_2.is_on
def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""
self.hass.stop()
def test_config_yaml_alias_anchor(self):
"""Test the usage of YAML aliases and anchors.
The following test scene configuration is equivalent to:
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,
}
assert setup_component(self.hass, scene.DOMAIN, {
'scene': [{
'name': 'test',
'entities': {
self.light_1.entity_id: entity_state,
self.light_2.entity_id: entity_state,
}
}]
})
common.activate(self.hass, 'scene.test')
self.hass.block_till_done()
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')
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_loader.yaml.load(file)
assert setup_component(self.hass, scene.DOMAIN, doc)
common.activate(self.hass, 'scene.test')
self.hass.block_till_done()
assert self.light_1.is_on
assert self.light_2.is_on
assert 100 == self.light_2.last_call('turn_on')[1].get('brightness')
def test_activate_scene(self):
"""Test active scene."""
assert setup_component(self.hass, scene.DOMAIN, {
'scene': [{
'name': 'test',
'entities': {
self.light_1.entity_id: 'on',
self.light_2.entity_id: {
'state': 'on',
'brightness': 100,
}
}
}]
})
common.activate(self.hass, 'scene.test')
self.hass.block_till_done()
assert self.light_1.is_on
assert self.light_2.is_on
assert 100 == self.light_2.last_call('turn_on')[1].get('brightness')