2016-03-09 10:25:50 +01:00
|
|
|
"""The tests for the Template switch platform."""
|
2017-03-02 14:09:53 +01:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from homeassistant.core import callback, State, CoreState
|
2016-08-22 23:05:45 +01:00
|
|
|
import homeassistant.bootstrap as bootstrap
|
2016-02-03 14:29:25 +00:00
|
|
|
import homeassistant.components as core
|
2017-03-02 14:09:53 +01:00
|
|
|
from homeassistant.const import STATE_ON, STATE_OFF
|
|
|
|
from homeassistant.helpers.restore_state import DATA_RESTORE_CACHE
|
2016-02-03 13:16:13 +00:00
|
|
|
|
2017-03-02 14:09:53 +01:00
|
|
|
from tests.common import (
|
|
|
|
get_test_home_assistant, assert_setup_component, mock_component)
|
2016-02-14 15:08:23 -08:00
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
|
|
|
|
class TestTemplateSwitch:
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test the Template switch."""
|
2016-02-02 19:25:17 +00:00
|
|
|
|
2016-10-08 20:27:35 +02:00
|
|
|
hass = None
|
|
|
|
calls = None
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
def setup_method(self, method):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-02-14 15:08:23 -08:00
|
|
|
self.hass = get_test_home_assistant()
|
2016-02-03 14:29:25 +00:00
|
|
|
self.calls = []
|
|
|
|
|
2016-11-05 16:36:20 -07:00
|
|
|
@callback
|
2016-02-03 14:29:25 +00:00
|
|
|
def record_call(service):
|
2016-08-22 23:05:45 +01:00
|
|
|
"""Track function calls.."""
|
2016-02-03 14:29:25 +00:00
|
|
|
self.calls.append(service)
|
|
|
|
|
|
|
|
self.hass.services.register('test', 'automation', record_call)
|
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
def teardown_method(self, method):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Stop everything that was started."""
|
2016-02-02 19:25:17 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2016-02-03 13:16:13 +00:00
|
|
|
def test_template_state_text(self):
|
2016-03-09 10:25:50 +01:00
|
|
|
""""Test the state text of a template."""
|
2016-10-08 20:27:35 +02:00
|
|
|
with assert_setup_component(1):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'template',
|
|
|
|
'switches': {
|
|
|
|
'test_template_switch': {
|
|
|
|
'value_template':
|
|
|
|
"{{ states.switch.test_state.state }}",
|
|
|
|
'turn_on': {
|
|
|
|
'service': 'switch.turn_on',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
'turn_off': {
|
|
|
|
'service': 'switch.turn_off',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
}
|
2016-02-02 19:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 20:27:35 +02:00
|
|
|
})
|
2016-02-02 19:25:17 +00:00
|
|
|
|
2017-03-02 14:09:53 +01:00
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-03 13:16:13 +00:00
|
|
|
state = self.hass.states.set('switch.test_state', STATE_ON)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2016-02-02 19:25:17 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test_template_switch')
|
2016-02-03 13:16:13 +00:00
|
|
|
assert state.state == STATE_ON
|
2016-02-02 19:25:17 +00:00
|
|
|
|
2016-02-03 13:16:13 +00:00
|
|
|
state = self.hass.states.set('switch.test_state', STATE_OFF)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2016-02-02 19:25:17 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test_template_switch')
|
2016-02-03 13:16:13 +00:00
|
|
|
assert state.state == STATE_OFF
|
|
|
|
|
|
|
|
def test_template_state_boolean_on(self):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test the setting of the state with boolean on."""
|
2016-10-08 20:27:35 +02:00
|
|
|
with assert_setup_component(1):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'template',
|
|
|
|
'switches': {
|
|
|
|
'test_template_switch': {
|
|
|
|
'value_template':
|
|
|
|
"{{ 1 == 1 }}",
|
|
|
|
'turn_on': {
|
|
|
|
'service': 'switch.turn_on',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
'turn_off': {
|
|
|
|
'service': 'switch.turn_off',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
}
|
2016-02-03 13:16:13 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 20:27:35 +02:00
|
|
|
})
|
2016-02-02 19:25:17 +00:00
|
|
|
|
2017-03-02 14:09:53 +01:00
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-03 13:16:13 +00:00
|
|
|
state = self.hass.states.get('switch.test_template_switch')
|
|
|
|
assert state.state == STATE_ON
|
|
|
|
|
|
|
|
def test_template_state_boolean_off(self):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test the setting of the state with off."""
|
2016-10-08 20:27:35 +02:00
|
|
|
with assert_setup_component(1):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'template',
|
|
|
|
'switches': {
|
|
|
|
'test_template_switch': {
|
|
|
|
'value_template':
|
|
|
|
"{{ 1 == 2 }}",
|
|
|
|
'turn_on': {
|
|
|
|
'service': 'switch.turn_on',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
'turn_off': {
|
|
|
|
'service': 'switch.turn_off',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
}
|
2016-02-03 13:16:13 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 20:27:35 +02:00
|
|
|
})
|
2016-02-03 13:16:13 +00:00
|
|
|
|
2017-03-02 14:09:53 +01:00
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-03 13:16:13 +00:00
|
|
|
state = self.hass.states.get('switch.test_template_switch')
|
|
|
|
assert state.state == STATE_OFF
|
2016-02-02 19:25:17 +00:00
|
|
|
|
|
|
|
def test_template_syntax_error(self):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test templating syntax error."""
|
2016-10-08 20:27:35 +02:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'template',
|
|
|
|
'switches': {
|
|
|
|
'test_template_switch': {
|
|
|
|
'value_template':
|
|
|
|
"{% if rubbish %}",
|
|
|
|
'turn_on': {
|
|
|
|
'service': 'switch.turn_on',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
'turn_off': {
|
|
|
|
'service': 'switch.turn_off',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
}
|
2016-02-02 19:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 20:27:35 +02:00
|
|
|
})
|
2017-03-02 14:09:53 +01:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-08-22 23:05:45 +01:00
|
|
|
assert self.hass.states.all() == []
|
2016-02-02 19:25:17 +00:00
|
|
|
|
|
|
|
def test_invalid_name_does_not_create(self):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test invalid name."""
|
2016-10-08 20:27:35 +02:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'template',
|
|
|
|
'switches': {
|
|
|
|
'test INVALID switch': {
|
|
|
|
'value_template':
|
|
|
|
"{{ rubbish }",
|
|
|
|
'turn_on': {
|
|
|
|
'service': 'switch.turn_on',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
'turn_off': {
|
|
|
|
'service': 'switch.turn_off',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
}
|
2016-02-02 19:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 20:27:35 +02:00
|
|
|
})
|
2017-03-02 14:09:53 +01:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
|
|
|
def test_invalid_switch_does_not_create(self):
|
2016-08-22 23:05:45 +01:00
|
|
|
"""Test invalid switch."""
|
2016-10-08 20:27:35 +02:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'template',
|
|
|
|
'switches': {
|
|
|
|
'test_template_switch': 'Invalid'
|
|
|
|
}
|
2016-02-02 19:25:17 +00:00
|
|
|
}
|
2016-10-08 20:27:35 +02:00
|
|
|
})
|
2017-03-02 14:09:53 +01:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
|
|
|
def test_no_switches_does_not_create(self):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test if there are no switches no creation."""
|
2016-10-08 20:27:35 +02:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'template'
|
|
|
|
}
|
|
|
|
})
|
2017-03-02 14:09:53 +01:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
|
|
|
def test_missing_template_does_not_create(self):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test missing template."""
|
2016-10-08 20:27:35 +02:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'template',
|
|
|
|
'switches': {
|
|
|
|
'test_template_switch': {
|
|
|
|
'not_value_template':
|
|
|
|
"{{ states.switch.test_state.state }}",
|
|
|
|
'turn_on': {
|
|
|
|
'service': 'switch.turn_on',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
'turn_off': {
|
|
|
|
'service': 'switch.turn_off',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
}
|
2016-02-02 19:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 20:27:35 +02:00
|
|
|
})
|
2017-03-02 14:09:53 +01:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
|
|
|
def test_missing_on_does_not_create(self):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test missing on."""
|
2016-10-08 20:27:35 +02:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'template',
|
|
|
|
'switches': {
|
|
|
|
'test_template_switch': {
|
|
|
|
'value_template':
|
|
|
|
"{{ states.switch.test_state.state }}",
|
|
|
|
'not_on': {
|
|
|
|
'service': 'switch.turn_on',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
'turn_off': {
|
|
|
|
'service': 'switch.turn_off',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
}
|
2016-02-02 19:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 20:27:35 +02:00
|
|
|
})
|
2017-03-02 14:09:53 +01:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
|
|
|
def test_missing_off_does_not_create(self):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test missing off."""
|
2016-10-08 20:27:35 +02:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'template',
|
|
|
|
'switches': {
|
|
|
|
'test_template_switch': {
|
|
|
|
'value_template':
|
|
|
|
"{{ states.switch.test_state.state }}",
|
|
|
|
'turn_on': {
|
|
|
|
'service': 'switch.turn_on',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
'not_off': {
|
|
|
|
'service': 'switch.turn_off',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
}
|
2016-02-02 19:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 20:27:35 +02:00
|
|
|
})
|
2017-03-02 14:09:53 +01:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
assert self.hass.states.all() == []
|
2016-02-03 14:29:25 +00:00
|
|
|
|
|
|
|
def test_on_action(self):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test on action."""
|
2016-08-22 23:05:45 +01:00
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
2016-02-03 14:29:25 +00:00
|
|
|
'switch': {
|
|
|
|
'platform': 'template',
|
|
|
|
'switches': {
|
|
|
|
'test_template_switch': {
|
|
|
|
'value_template':
|
|
|
|
"{{ states.switch.test_state.state }}",
|
|
|
|
'turn_on': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
},
|
|
|
|
'turn_off': {
|
|
|
|
'service': 'switch.turn_off',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2017-03-02 14:09:53 +01:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-03 14:29:25 +00:00
|
|
|
self.hass.states.set('switch.test_state', STATE_OFF)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2016-02-03 14:29:25 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test_template_switch')
|
|
|
|
assert state.state == STATE_OFF
|
|
|
|
|
|
|
|
core.switch.turn_on(self.hass, 'switch.test_template_switch')
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2016-02-03 14:29:25 +00:00
|
|
|
|
2016-08-22 23:05:45 +01:00
|
|
|
assert len(self.calls) == 1
|
2016-02-03 14:29:25 +00:00
|
|
|
|
|
|
|
def test_off_action(self):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test off action."""
|
2016-08-22 23:05:45 +01:00
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
2016-02-03 14:29:25 +00:00
|
|
|
'switch': {
|
|
|
|
'platform': 'template',
|
|
|
|
'switches': {
|
|
|
|
'test_template_switch': {
|
|
|
|
'value_template':
|
|
|
|
"{{ states.switch.test_state.state }}",
|
|
|
|
'turn_on': {
|
|
|
|
'service': 'switch.turn_on',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
|
|
|
|
},
|
|
|
|
'turn_off': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2017-03-02 14:09:53 +01:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-03 14:29:25 +00:00
|
|
|
self.hass.states.set('switch.test_state', STATE_ON)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2016-02-03 14:29:25 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test_template_switch')
|
|
|
|
assert state.state == STATE_ON
|
|
|
|
|
|
|
|
core.switch.turn_off(self.hass, 'switch.test_template_switch')
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2016-02-03 14:29:25 +00:00
|
|
|
|
2016-08-22 23:05:45 +01:00
|
|
|
assert len(self.calls) == 1
|
2017-03-02 14:09:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_restore_state(hass):
|
|
|
|
"""Ensure states are restored on startup."""
|
|
|
|
hass.data[DATA_RESTORE_CACHE] = {
|
|
|
|
'switch.test_template_switch':
|
|
|
|
State('switch.test_template_switch', 'on'),
|
|
|
|
}
|
|
|
|
|
|
|
|
hass.state = CoreState.starting
|
|
|
|
mock_component(hass, 'recorder')
|
|
|
|
|
|
|
|
yield from bootstrap.async_setup_component(hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'template',
|
|
|
|
'switches': {
|
|
|
|
'test_template_switch': {
|
|
|
|
'value_template':
|
|
|
|
"{{ states.switch.test_state.state }}",
|
|
|
|
'turn_on': {
|
|
|
|
'service': 'switch.turn_on',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
'turn_off': {
|
|
|
|
'service': 'switch.turn_off',
|
|
|
|
'entity_id': 'switch.test_state'
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
state = hass.states.get('switch.test_template_switch')
|
|
|
|
assert state.state == 'on'
|
|
|
|
|
|
|
|
yield from hass.async_start()
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get('switch.test_template_switch')
|
|
|
|
assert state.state == 'unavailable'
|