2016-03-09 09:25:50 +00:00
|
|
|
"""The test for the Template sensor platform."""
|
2017-03-02 05:38:19 +00:00
|
|
|
import asyncio
|
2016-01-21 18:31:44 +00:00
|
|
|
|
2017-03-02 05:38:19 +00:00
|
|
|
from homeassistant.core import CoreState, State
|
|
|
|
from homeassistant.bootstrap import setup_component, async_setup_component
|
|
|
|
from homeassistant.helpers.restore_state import DATA_RESTORE_CACHE
|
|
|
|
|
|
|
|
from tests.common import (
|
|
|
|
get_test_home_assistant, assert_setup_component, mock_component)
|
2016-02-14 23:08:23 +00:00
|
|
|
|
2016-01-21 18:31:44 +00:00
|
|
|
|
2016-01-22 09:37:20 +00:00
|
|
|
class TestTemplateSensor:
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the Template sensor."""
|
2016-01-21 18:31:44 +00:00
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
hass = None
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
|
2016-01-21 18:31:44 +00:00
|
|
|
def setup_method(self, method):
|
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-01-21 18:31:44 +00:00
|
|
|
|
|
|
|
def teardown_method(self, method):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2016-01-21 18:31:44 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2016-01-22 09:37:20 +00:00
|
|
|
def test_template(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test template."""
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(1):
|
|
|
|
assert setup_component(self.hass, 'sensor', {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template',
|
|
|
|
'sensors': {
|
|
|
|
'test_template_sensor': {
|
|
|
|
'value_template':
|
|
|
|
"It {{ states.sensor.test_state.state }}."
|
|
|
|
}
|
2016-01-21 18:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 18:27:35 +00:00
|
|
|
})
|
2016-01-21 18:31:44 +00:00
|
|
|
|
2017-03-02 05:38:19 +00:00
|
|
|
self.hass.start()
|
2017-03-02 13:09:53 +00:00
|
|
|
self.hass.block_till_done()
|
2017-03-02 05:38:19 +00:00
|
|
|
|
2016-01-21 18:31:44 +00:00
|
|
|
state = self.hass.states.get('sensor.test_template_sensor')
|
2016-02-02 14:15:06 +00:00
|
|
|
assert state.state == 'It .'
|
2016-01-21 18:31:44 +00:00
|
|
|
|
|
|
|
self.hass.states.set('sensor.test_state', 'Works')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-21 18:31:44 +00:00
|
|
|
state = self.hass.states.get('sensor.test_template_sensor')
|
2016-02-02 14:15:06 +00:00
|
|
|
assert state.state == 'It Works.'
|
2016-01-22 09:37:20 +00:00
|
|
|
|
2017-02-07 09:51:44 +00:00
|
|
|
def test_icon_template(self):
|
|
|
|
"""Test icon template."""
|
|
|
|
with assert_setup_component(1):
|
|
|
|
assert setup_component(self.hass, 'sensor', {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template',
|
|
|
|
'sensors': {
|
|
|
|
'test_template_sensor': {
|
|
|
|
'value_template': "State",
|
|
|
|
'icon_template':
|
|
|
|
"{% if states.sensor.test_state.state == "
|
|
|
|
"'Works' %}"
|
|
|
|
"mdi:check"
|
|
|
|
"{% endif %}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-03-02 05:38:19 +00:00
|
|
|
self.hass.start()
|
2017-03-02 13:09:53 +00:00
|
|
|
self.hass.block_till_done()
|
2017-03-02 05:38:19 +00:00
|
|
|
|
2017-02-07 09:51:44 +00:00
|
|
|
state = self.hass.states.get('sensor.test_template_sensor')
|
|
|
|
assert 'icon' not in state.attributes
|
|
|
|
|
|
|
|
self.hass.states.set('sensor.test_state', 'Works')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
state = self.hass.states.get('sensor.test_template_sensor')
|
|
|
|
assert state.attributes['icon'] == 'mdi:check'
|
|
|
|
|
2016-01-22 09:37:20 +00:00
|
|
|
def test_template_syntax_error(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test templating syntax error."""
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert setup_component(self.hass, 'sensor', {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template',
|
|
|
|
'sensors': {
|
|
|
|
'test_template_sensor': {
|
|
|
|
'value_template':
|
|
|
|
"{% if rubbish %}"
|
|
|
|
}
|
2016-01-22 09:37:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 18:27:35 +00:00
|
|
|
})
|
2017-03-02 05:38:19 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
2017-03-02 13:09:53 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-22 08:11:16 +00:00
|
|
|
assert self.hass.states.all() == []
|
2016-02-01 18:18:51 +00:00
|
|
|
|
2016-02-02 14:15:06 +00:00
|
|
|
def test_template_attribute_missing(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test missing attribute template."""
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(1):
|
|
|
|
assert setup_component(self.hass, 'sensor', {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template',
|
|
|
|
'sensors': {
|
|
|
|
'test_template_sensor': {
|
|
|
|
'value_template': 'It {{ states.sensor.test_state'
|
|
|
|
'.attributes.missing }}.'
|
|
|
|
}
|
2016-02-02 14:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 18:27:35 +00:00
|
|
|
})
|
2016-02-02 14:15:06 +00:00
|
|
|
|
2017-03-02 05:38:19 +00:00
|
|
|
self.hass.start()
|
2017-03-02 13:09:53 +00:00
|
|
|
self.hass.block_till_done()
|
2017-03-02 05:38:19 +00:00
|
|
|
|
2016-02-02 14:15:06 +00:00
|
|
|
state = self.hass.states.get('sensor.test_template_sensor')
|
2016-03-25 06:07:19 +00:00
|
|
|
assert state.state == 'unknown'
|
2016-02-02 14:15:06 +00:00
|
|
|
|
2016-02-01 18:18:51 +00:00
|
|
|
def test_invalid_name_does_not_create(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test invalid name."""
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert setup_component(self.hass, 'sensor', {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template',
|
|
|
|
'sensors': {
|
|
|
|
'test INVALID sensor': {
|
|
|
|
'value_template':
|
|
|
|
"{{ states.sensor.test_state.state }}"
|
|
|
|
}
|
2016-02-01 18:18:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 18:27:35 +00:00
|
|
|
})
|
2017-03-02 05:38:19 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
2017-03-02 13:09:53 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-01 18:18:51 +00:00
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
2016-02-01 18:30:39 +00:00
|
|
|
def test_invalid_sensor_does_not_create(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test invalid sensor."""
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert setup_component(self.hass, 'sensor', {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template',
|
|
|
|
'sensors': {
|
|
|
|
'test_template_sensor': 'invalid'
|
|
|
|
}
|
2016-02-01 18:18:51 +00:00
|
|
|
}
|
2016-10-08 18:27:35 +00:00
|
|
|
})
|
2017-03-02 05:38:19 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
2017-03-02 13:09:53 +00:00
|
|
|
|
2016-02-01 18:18:51 +00:00
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
2016-02-01 18:30:39 +00:00
|
|
|
def test_no_sensors_does_not_create(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test no sensors."""
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert setup_component(self.hass, 'sensor', {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template'
|
|
|
|
}
|
|
|
|
})
|
2017-03-02 05:38:19 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
2017-03-02 13:09:53 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-01 18:30:39 +00:00
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
2016-02-01 18:18:51 +00:00
|
|
|
def test_missing_template_does_not_create(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test missing template."""
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert setup_component(self.hass, 'sensor', {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template',
|
|
|
|
'sensors': {
|
|
|
|
'test_template_sensor': {
|
|
|
|
'not_value_template':
|
|
|
|
"{{ states.sensor.test_state.state }}"
|
|
|
|
}
|
2016-02-01 18:18:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 18:27:35 +00:00
|
|
|
})
|
2017-03-02 05:38:19 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
2017-03-02 13:09:53 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2016-02-01 18:18:51 +00:00
|
|
|
assert self.hass.states.all() == []
|
2017-03-02 05:38:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_restore_state(hass):
|
|
|
|
"""Ensure states are restored on startup."""
|
|
|
|
hass.data[DATA_RESTORE_CACHE] = {
|
|
|
|
'sensor.test_template_sensor':
|
|
|
|
State('sensor.test_template_sensor', 'It Test.'),
|
|
|
|
}
|
|
|
|
|
|
|
|
hass.state = CoreState.starting
|
|
|
|
mock_component(hass, 'recorder')
|
|
|
|
|
|
|
|
yield from async_setup_component(hass, 'sensor', {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template',
|
|
|
|
'sensors': {
|
|
|
|
'test_template_sensor': {
|
|
|
|
'value_template':
|
|
|
|
"It {{ states.sensor.test_state.state }}."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
state = hass.states.get('sensor.test_template_sensor')
|
|
|
|
assert state.state == 'It Test.'
|
|
|
|
|
|
|
|
yield from hass.async_start()
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get('sensor.test_template_sensor')
|
|
|
|
assert state.state == 'It .'
|