core/tests/components/scene/test_init.py

139 lines
4.4 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the Scene component."""
import io
2015-10-12 06:48:17 +00:00
import unittest
from homeassistant.components import light, scene
from homeassistant.setup import async_setup_component, setup_component
from homeassistant.util.yaml import loader as yaml_loader
2015-10-12 06:48:17 +00:00
from tests.common import get_test_home_assistant
from tests.components.light import common as common_light
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-07-31 19:25:30 +00:00
test_light = getattr(self.hass.components, "test.light")
test_light.init()
2019-07-31 19:25:30 +00:00
assert setup_component(
self.hass, light.DOMAIN, {light.DOMAIN: {"platform": "test"}}
)
self.light_1, self.light_2 = test_light.ENTITIES[0:2]
common_light.turn_off(
2019-07-31 19:25:30 +00:00
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
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()
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:
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.
"""
2019-07-31 19:25:30 +00:00
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
2019-07-31 19:25:30 +00:00
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 = (
2019-07-31 19:25:30 +00:00
"scene:\n"
" - name: test\n"
" entities:\n"
f" {self.light_1.entity_id}: on\n"
f" {self.light_2.entity_id}:\n"
2019-07-31 19:25:30 +00:00
" state: on\n"
" brightness: 100\n"
)
with io.StringIO(config) as file:
doc = yaml_loader.yaml.safe_load(file)
assert setup_component(self.hass, scene.DOMAIN, doc)
2019-07-31 19:25:30 +00:00
common.activate(self.hass, "scene.test")
self.hass.block_till_done()
assert self.light_1.is_on
assert self.light_2.is_on
2019-07-31 19:25:30 +00:00
assert 100 == self.light_2.last_call("turn_on")[1].get("brightness")
2015-10-12 06:48:17 +00:00
def test_activate_scene(self):
2016-03-09 09:25:50 +00:00
"""Test active scene."""
2019-07-31 19:25:30 +00:00
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},
},
2015-10-12 06:48:17 +00:00
}
2019-07-31 19:25:30 +00:00
]
},
)
2015-10-12 06:48:17 +00:00
2019-07-31 19:25:30 +00:00
common.activate(self.hass, "scene.test")
self.hass.block_till_done()
2015-10-12 06:48:17 +00:00
assert self.light_1.is_on
assert self.light_2.is_on
2019-07-31 19:25:30 +00:00
assert 100 == self.light_2.last_call("turn_on")[1].get("brightness")
async def test_services_registered(hass):
"""Test we register services with empty config."""
assert await async_setup_component(hass, "scene", {})
assert hass.services.has_service("scene", "reload")
assert hass.services.has_service("scene", "turn_on")
assert hass.services.has_service("scene", "apply")