2016-01-21 18:31:44 +00:00
|
|
|
"""
|
|
|
|
tests.components.sensor.template
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Tests template sensor.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import homeassistant.core as ha
|
|
|
|
import homeassistant.components.sensor as sensor
|
|
|
|
|
|
|
|
|
2016-01-22 09:37:20 +00:00
|
|
|
class TestTemplateSensor:
|
|
|
|
""" Test the Template sensor. """
|
2016-01-21 18:31:44 +00:00
|
|
|
|
|
|
|
def setup_method(self, method):
|
|
|
|
self.hass = ha.HomeAssistant()
|
|
|
|
|
|
|
|
def teardown_method(self, method):
|
|
|
|
""" Stop down stuff we started. """
|
|
|
|
self.hass.stop()
|
|
|
|
|
2016-01-22 09:37:20 +00:00
|
|
|
def test_template(self):
|
2016-01-21 23:17:19 +00:00
|
|
|
assert sensor.setup(self.hass, {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template',
|
|
|
|
'sensors': {
|
|
|
|
'test_template_sensor': {
|
|
|
|
'value_template':
|
2016-01-22 09:37:20 +00:00
|
|
|
"{{ states.sensor.test_state.state }}"
|
2016-01-21 18:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-21 23:17:19 +00:00
|
|
|
}
|
|
|
|
})
|
2016-01-21 18:31:44 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('sensor.test_template_sensor')
|
|
|
|
assert state.state == ''
|
|
|
|
|
|
|
|
self.hass.states.set('sensor.test_state', 'Works')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
state = self.hass.states.get('sensor.test_template_sensor')
|
|
|
|
assert state.state == 'Works'
|
2016-01-22 09:37:20 +00:00
|
|
|
|
|
|
|
def test_template_syntax_error(self):
|
|
|
|
assert sensor.setup(self.hass, {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template',
|
|
|
|
'sensors': {
|
|
|
|
'test_template_sensor': {
|
|
|
|
'value_template':
|
|
|
|
"{% if rubbish %}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('sensor.test_state', 'Works')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
state = self.hass.states.get('sensor.test_template_sensor')
|
2016-01-22 16:30:02 +00:00
|
|
|
assert state.state == 'error'
|
2016-02-01 18:18:51 +00:00
|
|
|
|
|
|
|
def test_invalid_name_does_not_create(self):
|
|
|
|
assert sensor.setup(self.hass, {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template',
|
|
|
|
'sensors': {
|
|
|
|
'test INVALID sensor': {
|
|
|
|
'value_template':
|
|
|
|
"{{ states.sensor.test_state.state }}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
2016-02-01 18:30:39 +00:00
|
|
|
def test_invalid_sensor_does_not_create(self):
|
2016-02-01 18:18:51 +00:00
|
|
|
assert sensor.setup(self.hass, {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template',
|
|
|
|
'sensors': {
|
2016-02-01 18:30:39 +00:00
|
|
|
'test_template_sensor': 'invalid'
|
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):
|
|
|
|
assert sensor.setup(self.hass, {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
2016-02-01 18:18:51 +00:00
|
|
|
def test_missing_template_does_not_create(self):
|
|
|
|
assert sensor.setup(self.hass, {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'template',
|
|
|
|
'sensors': {
|
|
|
|
'test_template_sensor': {
|
|
|
|
'not_value_template':
|
|
|
|
"{{ states.sensor.test_state.state }}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert self.hass.states.all() == []
|