This commit is contained in:
Paulus Schoutsen 2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View file

@ -8,8 +8,14 @@ import pytest
from homeassistant.components import script
from homeassistant.components.script import DOMAIN
from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_NAME, SERVICE_RELOAD, SERVICE_TOGGLE,
SERVICE_TURN_OFF, SERVICE_TURN_ON, EVENT_SCRIPT_STARTED)
ATTR_ENTITY_ID,
ATTR_NAME,
SERVICE_RELOAD,
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
EVENT_SCRIPT_STARTED,
)
from homeassistant.core import Context, callback, split_entity_id
from homeassistant.loader import bind_hass
from homeassistant.setup import setup_component, async_setup_component
@ -18,7 +24,7 @@ from homeassistant.exceptions import ServiceNotFound
from tests.common import get_test_home_assistant
ENTITY_ID = 'script.test'
ENTITY_ID = "script.test"
@bind_hass
@ -75,30 +81,26 @@ class TestScriptComponent(unittest.TestCase):
def test_setup_with_invalid_configs(self):
"""Test setup with invalid configs."""
for value in (
{'test': {}},
{"test": {}},
{"test hello world": {"sequence": [{"event": "bla"}]}},
{
'test hello world': {
'sequence': [{'event': 'bla'}]
}
},
{
'test': {
'sequence': {
'event': 'test_event',
'service': 'homeassistant.turn_on',
"test": {
"sequence": {
"event": "test_event",
"service": "homeassistant.turn_on",
}
}
},
):
assert not setup_component(self.hass, 'script', {
'script': value
}), 'Script loaded with wrong config {}'.format(value)
assert not setup_component(
self.hass, "script", {"script": value}
), "Script loaded with wrong config {}".format(value)
assert 0 == len(self.hass.states.entity_ids('script'))
assert 0 == len(self.hass.states.entity_ids("script"))
def test_turn_on_service(self):
"""Verify that the turn_on service."""
event = 'test_event'
event = "test_event"
events = []
@callback
@ -108,19 +110,15 @@ class TestScriptComponent(unittest.TestCase):
self.hass.bus.listen(event, record_event)
assert setup_component(self.hass, 'script', {
'script': {
'test': {
'sequence': [{
'delay': {
'seconds': 5
}
}, {
'event': event,
}]
assert setup_component(
self.hass,
"script",
{
"script": {
"test": {"sequence": [{"delay": {"seconds": 5}}, {"event": event}]}
}
}
})
},
)
turn_on(self.hass, ENTITY_ID)
self.hass.block_till_done()
@ -137,13 +135,13 @@ class TestScriptComponent(unittest.TestCase):
assert not script.is_on(self.hass, ENTITY_ID)
assert 0 == len(events)
state = self.hass.states.get('group.all_scripts')
state = self.hass.states.get("group.all_scripts")
assert state is not None
assert state.attributes.get('entity_id') == (ENTITY_ID,)
assert state.attributes.get("entity_id") == (ENTITY_ID,)
def test_toggle_service(self):
"""Test the toggling of a service."""
event = 'test_event'
event = "test_event"
events = []
@callback
@ -153,19 +151,15 @@ class TestScriptComponent(unittest.TestCase):
self.hass.bus.listen(event, record_event)
assert setup_component(self.hass, 'script', {
'script': {
'test': {
'sequence': [{
'delay': {
'seconds': 5
}
}, {
'event': event,
}]
assert setup_component(
self.hass,
"script",
{
"script": {
"test": {"sequence": [{"delay": {"seconds": 5}}, {"event": event}]}
}
}
})
},
)
toggle(self.hass, ENTITY_ID)
self.hass.block_till_done()
@ -187,82 +181,72 @@ class TestScriptComponent(unittest.TestCase):
"""Add recorded event to set."""
calls.append(service)
self.hass.services.register('test', 'script', record_call)
self.hass.services.register("test", "script", record_call)
assert setup_component(self.hass, 'script', {
'script': {
'test': {
'sequence': {
'service': 'test.script',
'data_template': {
'hello': '{{ greeting }}',
},
},
},
assert setup_component(
self.hass,
"script",
{
"script": {
"test": {
"sequence": {
"service": "test.script",
"data_template": {"hello": "{{ greeting }}"},
}
}
}
},
})
)
turn_on(self.hass, ENTITY_ID, {
'greeting': 'world'
}, context=context)
turn_on(self.hass, ENTITY_ID, {"greeting": "world"}, context=context)
self.hass.block_till_done()
assert len(calls) == 1
assert calls[0].context is context
assert calls[0].data['hello'] == 'world'
assert calls[0].data["hello"] == "world"
self.hass.services.call('script', 'test', {
'greeting': 'universe',
}, context=context)
self.hass.services.call(
"script", "test", {"greeting": "universe"}, context=context
)
self.hass.block_till_done()
assert len(calls) == 2
assert calls[1].context is context
assert calls[1].data['hello'] == 'universe'
assert calls[1].data["hello"] == "universe"
def test_reload_service(self):
"""Verify that the turn_on service."""
assert setup_component(self.hass, 'script', {
'script': {
'test': {
'sequence': [{
'delay': {
'seconds': 5
}
}]
}
}
})
assert setup_component(
self.hass,
"script",
{"script": {"test": {"sequence": [{"delay": {"seconds": 5}}]}}},
)
assert self.hass.states.get(ENTITY_ID) is not None
assert self.hass.services.has_service(script.DOMAIN, 'test')
assert self.hass.services.has_service(script.DOMAIN, "test")
with patch('homeassistant.config.load_yaml_config_file', return_value={
'script': {
'test2': {
'sequence': [{
'delay': {
'seconds': 5
}
}]
}}}):
with patch('homeassistant.config.find_config_file',
return_value=''):
with patch(
"homeassistant.config.load_yaml_config_file",
return_value={
"script": {"test2": {"sequence": [{"delay": {"seconds": 5}}]}}
},
):
with patch("homeassistant.config.find_config_file", return_value=""):
reload(self.hass)
self.hass.block_till_done()
assert self.hass.states.get(ENTITY_ID) is None
assert not self.hass.services.has_service(script.DOMAIN, 'test')
assert not self.hass.services.has_service(script.DOMAIN, "test")
assert self.hass.states.get("script.test2") is not None
assert self.hass.services.has_service(script.DOMAIN, 'test2')
assert self.hass.services.has_service(script.DOMAIN, "test2")
async def test_shared_context(hass):
"""Test that the shared context is passed down the chain."""
event = 'test_event'
event = "test_event"
context = Context()
event_mock = Mock()
@ -271,19 +255,13 @@ async def test_shared_context(hass):
hass.bus.async_listen(event, event_mock)
hass.bus.async_listen(EVENT_SCRIPT_STARTED, run_mock)
assert await async_setup_component(hass, 'script', {
'script': {
'test': {
'sequence': [
{'event': event}
]
}
}
})
assert await async_setup_component(
hass, "script", {"script": {"test": {"sequence": [{"event": event}]}}}
)
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON,
{ATTR_ENTITY_ID: ENTITY_ID},
context=context)
await hass.services.async_call(
DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_ID}, context=context
)
await hass.async_block_till_done()
assert event_mock.call_count == 1
@ -292,43 +270,39 @@ async def test_shared_context(hass):
args, kwargs = run_mock.call_args
assert args[0].context == context
# Ensure event data has all attributes set
assert args[0].data.get(ATTR_NAME) == 'test'
assert args[0].data.get(ATTR_ENTITY_ID) == 'script.test'
assert args[0].data.get(ATTR_NAME) == "test"
assert args[0].data.get(ATTR_ENTITY_ID) == "script.test"
# Ensure context carries through the event
args, kwargs = event_mock.call_args
assert args[0].context == context
# Ensure the script state shares the same context
state = hass.states.get('script.test')
state = hass.states.get("script.test")
assert state is not None
assert state.context == context
async def test_logging_script_error(hass, caplog):
"""Test logging script error."""
assert await async_setup_component(hass, 'script', {
'script': {
'hello': {
'sequence': [
{'service': 'non.existing'}
]
}
}
})
assert await async_setup_component(
hass,
"script",
{"script": {"hello": {"sequence": [{"service": "non.existing"}]}}},
)
with pytest.raises(ServiceNotFound) as err:
await hass.services.async_call('script', 'hello', blocking=True)
await hass.services.async_call("script", "hello", blocking=True)
assert err.value.domain == 'non'
assert err.value.service == 'existing'
assert 'Error executing script' in caplog.text
assert err.value.domain == "non"
assert err.value.service == "existing"
assert "Error executing script" in caplog.text
async def test_turning_no_scripts_off(hass):
"""Test it is possible to turn two scripts off."""
assert await async_setup_component(hass, 'script', {})
assert await async_setup_component(hass, "script", {})
# Testing it doesn't raise
await hass.services.async_call(
DOMAIN, SERVICE_TURN_OFF, {'entity_id': []}, blocking=True
DOMAIN, SERVICE_TURN_OFF, {"entity_id": []}, blocking=True
)