2016-03-09 09:25:50 +00:00
|
|
|
"""The tests fr the Template automation."""
|
2015-12-16 21:27:43 +00:00
|
|
|
import unittest
|
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
from homeassistant.bootstrap import _setup_component
|
2015-12-16 21:27:43 +00:00
|
|
|
import homeassistant.components.automation as automation
|
|
|
|
|
2016-02-14 23:08:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
class TestAutomationTemplate(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the event automation."""
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-02-14 23:08:23 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2016-04-04 19:18:58 +00:00
|
|
|
self.hass.config.components.append('group')
|
2015-12-16 21:27:43 +00:00
|
|
|
self.hass.states.set('test.entity', 'hello')
|
|
|
|
self.calls = []
|
|
|
|
|
|
|
|
def record_call(service):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""helper for recording calls."""
|
2015-12-16 21:27:43 +00:00
|
|
|
self.calls.append(service)
|
|
|
|
|
|
|
|
self.hass.services.register('test', 'automation', record_call)
|
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2015-12-16 21:27:43 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_if_fires_on_change_bool(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on boolean change."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': '{{ true }}',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_fires_on_change_str(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on change."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': 'true',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_fires_on_change_str_crazy(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on change."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': 'TrUE',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_not_fires_on_change_bool(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for not firing on boolean change."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': '{{ false }}',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_not_fires_on_change_str(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for not firing on string change."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': 'False',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_not_fires_on_change_str_crazy(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for not firing on string change."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': 'Anything other than "true" is false.',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_fires_on_no_change(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on no change."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': '{{ true }}',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'hello')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_fires_on_two_change(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on two changes."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': '{{ true }}',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
# Trigger once
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
# Trigger again
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_fires_on_change_with_template(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on change with template."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': '{{ is_state("test.entity", "world") }}',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_not_fires_on_change_with_template(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for not firing on change with template."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': '{{ is_state("test.entity", "hello") }}',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_fires_on_change_with_template_advanced(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on change with template advanced."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': '''{%- if is_state("test.entity", "world") -%}
|
|
|
|
true
|
|
|
|
{%- else -%}
|
|
|
|
false
|
|
|
|
{%- endif -%}''',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_fires_on_no_change_with_template_advanced(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on no change with template advanced."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': '''{%- if is_state("test.entity", "world") -%}
|
|
|
|
true
|
|
|
|
{%- else -%}
|
|
|
|
false
|
|
|
|
{%- endif -%}''',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
# Different state
|
|
|
|
self.hass.states.set('test.entity', 'worldz')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
# Different state
|
|
|
|
self.hass.states.set('test.entity', 'hello')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_fires_on_change_with_template_2(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on change with template."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
2016-02-14 21:07:21 +00:00
|
|
|
'value_template':
|
|
|
|
'{{ not is_state("test.entity", "world") }}',
|
2015-12-16 21:27:43 +00:00
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'home')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'work')
|
|
|
|
self.hass.pool.block_till_done()
|
2015-12-16 22:07:14 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'not_home')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
2015-12-16 22:07:14 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'home')
|
|
|
|
self.hass.pool.block_till_done()
|
2015-12-16 22:07:14 +00:00
|
|
|
self.assertEqual(2, len(self.calls))
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
def test_if_action(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing if action."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'condition': [{
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': '{{ is_state("test.entity", "world") }}'
|
|
|
|
}],
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
# Condition is not true yet
|
|
|
|
self.hass.bus.fire('test_event')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
# Change condition to true, but it shouldn't be triggered yet
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
# Condition is true and event is triggered
|
|
|
|
self.hass.bus.fire('test_event')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_fires_on_change_with_bad_template(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on change with bad template."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert not _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': '{{ ',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
def test_if_fires_on_change_with_bad_template_2(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on change with bad template."""
|
2016-04-04 19:18:58 +00:00
|
|
|
assert _setup_component(self.hass, automation.DOMAIN, {
|
2015-12-16 21:27:43 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'value_template': '{{ xyz | round(0) }}',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-12-16 21:27:43 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|