Consolidate all platforms that have tests (#22109)

* Moved climate components with tests into platform dirs.

* Updated tests from climate component.

* Moved binary_sensor components with tests into platform dirs.

* Updated tests from binary_sensor component.

* Moved calendar components with tests into platform dirs.

* Updated tests from calendar component.

* Moved camera components with tests into platform dirs.

* Updated tests from camera component.

* Moved cover components with tests into platform dirs.

* Updated tests from cover component.

* Moved device_tracker components with tests into platform dirs.

* Updated tests from device_tracker component.

* Moved fan components with tests into platform dirs.

* Updated tests from fan component.

* Moved geo_location components with tests into platform dirs.

* Updated tests from geo_location component.

* Moved image_processing components with tests into platform dirs.

* Updated tests from image_processing component.

* Moved light components with tests into platform dirs.

* Updated tests from light component.

* Moved lock components with tests into platform dirs.

* Moved media_player components with tests into platform dirs.

* Updated tests from media_player component.

* Moved scene components with tests into platform dirs.

* Moved sensor components with tests into platform dirs.

* Updated tests from sensor component.

* Moved switch components with tests into platform dirs.

* Updated tests from sensor component.

* Moved vacuum components with tests into platform dirs.

* Updated tests from vacuum component.

* Moved weather components with tests into platform dirs.

* Fixed __init__.py files

* Fixes for stuff moved as part of this branch.

* Fix stuff needed to merge with balloob's branch.

* Formatting issues.

* Missing __init__.py files.

* Fix-ups

* Fixup

* Regenerated requirements.

* Linting errors fixed.

* Fixed more broken tests.

* Missing init files.

* Fix broken tests.

* More broken tests

* There seems to be a thread race condition.
I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages.
Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe.

* Disabled tests, will remove sensor in #22147

* Updated coverage and codeowners.
This commit is contained in:
Penny Wood authored and Paulus Schoutsen committed 2019-03-18 23:07:39 -07:00
1 parent 46ece3603f
commit f195ecca4b
486 files changed
+662 -455

No files matched your search

+1
View File
@@ -0,0 +1 @@
"""Tests for template component."""
@@ -0,0 +1,436 @@
"""The tests for the Template Binary sensor platform."""
from datetime import timedelta
import unittest
from unittest import mock
from homeassistant.const import MATCH_ALL, EVENT_HOMEASSISTANT_START
from homeassistant import setup
from homeassistant.components.template import binary_sensor as template
from homeassistant.exceptions import TemplateError
from homeassistant.helpers import template as template_hlpr
from homeassistant.util.async_ import run_callback_threadsafe
import homeassistant.util.dt as dt_util
from tests.common import (
get_test_home_assistant, assert_setup_component, async_fire_time_changed)
class TestBinarySensorTemplate(unittest.TestCase):
"""Test for Binary sensor template platform."""
hass = None
# pylint: disable=invalid-name
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_setup(self):
"""Test the setup."""
config = {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test': {
'friendly_name': 'virtual thingy',
'value_template': '{{ foo }}',
'device_class': 'motion',
},
},
},
}
with assert_setup_component(1):
assert setup.setup_component(
self.hass, 'binary_sensor', config)
def test_setup_no_sensors(self):
"""Test setup with no sensors."""
with assert_setup_component(0):
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template'
}
})
def test_setup_invalid_device(self):
"""Test the setup with invalid devices."""
with assert_setup_component(0):
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template',
'sensors': {
'foo bar': {},
},
}
})
def test_setup_invalid_device_class(self):
"""Test setup with invalid sensor class."""
with assert_setup_component(0):
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test': {
'value_template': '{{ foo }}',
'device_class': 'foobarnotreal',
},
},
}
})
def test_setup_invalid_missing_template(self):
"""Test setup with invalid and missing template."""
with assert_setup_component(0):
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test': {
'device_class': 'motion',
},
}
}
})
def test_icon_template(self):
"""Test icon template."""
with assert_setup_component(1):
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test_template_sensor': {
'value_template': "{{ states.sensor.xyz.state }}",
'icon_template':
"{% if "
"states.binary_sensor.test_state.state == "
"'Works' %}"
"mdi:check"
"{% endif %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_template_sensor')
assert state.attributes.get('icon') == ''
self.hass.states.set('binary_sensor.test_state', 'Works')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_template_sensor')
assert state.attributes['icon'] == 'mdi:check'
def test_entity_picture_template(self):
"""Test entity_picture template."""
with assert_setup_component(1):
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test_template_sensor': {
'value_template': "{{ states.sensor.xyz.state }}",
'entity_picture_template':
"{% if "
"states.binary_sensor.test_state.state == "
"'Works' %}"
"/local/sensor.png"
"{% endif %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_template_sensor')
assert state.attributes.get('entity_picture') == ''
self.hass.states.set('binary_sensor.test_state', 'Works')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_template_sensor')
assert state.attributes['entity_picture'] == '/local/sensor.png'
@mock.patch('homeassistant.components.template.binary_sensor.'
'BinarySensorTemplate._async_render')
def test_match_all(self, _async_render):
"""Test MATCH_ALL in template."""
with assert_setup_component(1):
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template',
'sensors': {
'match_all_template_sensor': {
'value_template': "{{ 42 }}",
},
}
}
})
self.hass.start()
self.hass.block_till_done()
init_calls = len(_async_render.mock_calls)
self.hass.states.set('sensor.any_state', 'update')
self.hass.block_till_done()
assert len(_async_render.mock_calls) == init_calls
def test_attributes(self):
"""Test the attributes."""
vs = run_callback_threadsafe(
self.hass.loop, template.BinarySensorTemplate,
self.hass, 'parent', 'Parent', 'motion',
template_hlpr.Template('{{ 1 > 1 }}', self.hass),
None, None, MATCH_ALL, None, None
).result()
assert not vs.should_poll
assert 'motion' == vs.device_class
assert 'Parent' == vs.name
run_callback_threadsafe(self.hass.loop, vs.async_check_state).result()
assert not vs.is_on
# pylint: disable=protected-access
vs._template = template_hlpr.Template("{{ 2 > 1 }}", self.hass)
run_callback_threadsafe(self.hass.loop, vs.async_check_state).result()
assert vs.is_on
def test_event(self):
"""Test the event."""
config = {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test': {
'friendly_name': 'virtual thingy',
'value_template':
"{{ states.sensor.test_state.state == 'on' }}",
'device_class': 'motion',
},
},
},
}
with assert_setup_component(1):
assert setup.setup_component(
self.hass, 'binary_sensor', config)
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test')
assert state.state == 'off'
self.hass.states.set('sensor.test_state', 'on')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test')
assert state.state == 'on'
@mock.patch('homeassistant.helpers.template.Template.render')
def test_update_template_error(self, mock_render):
"""Test the template update error."""
vs = run_callback_threadsafe(
self.hass.loop, template.BinarySensorTemplate,
self.hass, 'parent', 'Parent', 'motion',
template_hlpr.Template('{{ 1 > 1 }}', self.hass),
None, None, MATCH_ALL, None, None
).result()
mock_render.side_effect = TemplateError('foo')
run_callback_threadsafe(self.hass.loop, vs.async_check_state).result()
mock_render.side_effect = TemplateError(
"UndefinedError: 'None' has no attribute")
run_callback_threadsafe(self.hass.loop, vs.async_check_state).result()
async def test_template_delay_on(hass):
"""Test binary sensor template delay on."""
config = {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test': {
'friendly_name': 'virtual thingy',
'value_template':
"{{ states.sensor.test_state.state == 'on' }}",
'device_class': 'motion',
'delay_on': 5
},
},
},
}
await setup.async_setup_component(hass, 'binary_sensor', config)
await hass.async_start()
hass.states.async_set('sensor.test_state', 'on')
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert state.state == 'off'
future = dt_util.utcnow() + timedelta(seconds=5)
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert state.state == 'on'
# check with time changes
hass.states.async_set('sensor.test_state', 'off')
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert state.state == 'off'
hass.states.async_set('sensor.test_state', 'on')
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert state.state == 'off'
hass.states.async_set('sensor.test_state', 'off')
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert state.state == 'off'
future = dt_util.utcnow() + timedelta(seconds=5)
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert state.state == 'off'
async def test_template_delay_off(hass):
"""Test binary sensor template delay off."""
config = {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test': {
'friendly_name': 'virtual thingy',
'value_template':
"{{ states.sensor.test_state.state == 'on' }}",
'device_class': 'motion',
'delay_off': 5
},
},
},
}
hass.states.async_set('sensor.test_state', 'on')
await setup.async_setup_component(hass, 'binary_sensor', config)
await hass.async_start()
hass.states.async_set('sensor.test_state', 'off')
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert state.state == 'on'
future = dt_util.utcnow() + timedelta(seconds=5)
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert state.state == 'off'
# check with time changes
hass.states.async_set('sensor.test_state', 'on')
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert state.state == 'on'
hass.states.async_set('sensor.test_state', 'off')
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert state.state == 'on'
hass.states.async_set('sensor.test_state', 'on')
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert state.state == 'on'
future = dt_util.utcnow() + timedelta(seconds=5)
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert state.state == 'on'
async def test_no_update_template_match_all(hass, caplog):
"""Test that we do not update sensors that match on all."""
hass.states.async_set('binary_sensor.test_sensor', 'true')
await setup.async_setup_component(hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template',
'sensors': {
'all_state': {
'value_template': '{{ "true" }}',
},
'all_icon': {
'value_template':
'{{ states.binary_sensor.test_sensor.state }}',
'icon_template': '{{ 1 + 1 }}',
},
'all_entity_picture': {
'value_template':
'{{ states.binary_sensor.test_sensor.state }}',
'entity_picture_template': '{{ 1 + 1 }}',
},
}
}
})
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 4
assert ('Template binary sensor all_state has no entity ids '
'configured to track nor were we able to extract the entities to '
'track from the value template') in caplog.text
assert ('Template binary sensor all_icon has no entity ids '
'configured to track nor were we able to extract the entities to '
'track from the icon template') in caplog.text
assert ('Template binary sensor all_entity_picture has no entity ids '
'configured to track nor were we able to extract the entities to '
'track from the entity_picture template') in caplog.text
assert hass.states.get('binary_sensor.all_state').state == 'off'
assert hass.states.get('binary_sensor.all_icon').state == 'off'
assert hass.states.get('binary_sensor.all_entity_picture').state == 'off'
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
assert hass.states.get('binary_sensor.all_state').state == 'on'
assert hass.states.get('binary_sensor.all_icon').state == 'on'
assert hass.states.get('binary_sensor.all_entity_picture').state == 'on'
hass.states.async_set('binary_sensor.test_sensor', 'false')
await hass.async_block_till_done()
assert hass.states.get('binary_sensor.all_state').state == 'on'
assert hass.states.get('binary_sensor.all_icon').state == 'on'
assert hass.states.get('binary_sensor.all_entity_picture').state == 'on'
await hass.helpers.entity_component.async_update_entity(
'binary_sensor.all_state')
await hass.helpers.entity_component.async_update_entity(
'binary_sensor.all_icon')
await hass.helpers.entity_component.async_update_entity(
'binary_sensor.all_entity_picture')
assert hass.states.get('binary_sensor.all_state').state == 'on'
assert hass.states.get('binary_sensor.all_icon').state == 'off'
assert hass.states.get('binary_sensor.all_entity_picture').state == 'off'
+758
View File
@@ -0,0 +1,758 @@
"""The tests the cover command line platform."""
import logging
import pytest
from homeassistant import setup
from homeassistant.components.cover import (
ATTR_POSITION, ATTR_TILT_POSITION, DOMAIN)
from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_CLOSE_COVER, SERVICE_CLOSE_COVER_TILT,
SERVICE_OPEN_COVER, SERVICE_OPEN_COVER_TILT, SERVICE_SET_COVER_POSITION,
SERVICE_SET_COVER_TILT_POSITION, SERVICE_STOP_COVER,
STATE_CLOSED, STATE_OPEN)
from tests.common import assert_setup_component, async_mock_service
_LOGGER = logging.getLogger(__name__)
ENTITY_COVER = 'cover.test_template_cover'
@pytest.fixture
def calls(hass):
"""Track calls to a mock serivce."""
return async_mock_service(hass, 'test', 'automation')
async def test_template_state_text(hass, calls):
"""Test the state text of a template."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'value_template':
"{{ states.cover.test_state.state }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test_state'
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.async_set('cover.test_state', STATE_OPEN)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.state == STATE_OPEN
state = hass.states.async_set('cover.test_state', STATE_CLOSED)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.state == STATE_CLOSED
async def test_template_state_boolean(hass, calls):
"""Test the value_template attribute."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'value_template':
"{{ 1 == 1 }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test_state'
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.state == STATE_OPEN
async def test_template_position(hass, calls):
"""Test the position_template attribute."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'position_template':
"{{ states.cover.test.attributes.position }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test'
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test'
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.async_set('cover.test', STATE_CLOSED)
await hass.async_block_till_done()
entity = hass.states.get('cover.test')
attrs = dict()
attrs['position'] = 42
hass.states.async_set(
entity.entity_id, entity.state,
attributes=attrs)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_position') == 42.0
assert state.state == STATE_OPEN
state = hass.states.async_set('cover.test', STATE_OPEN)
await hass.async_block_till_done()
entity = hass.states.get('cover.test')
attrs['position'] = 0.0
hass.states.async_set(
entity.entity_id, entity.state,
attributes=attrs)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_position') == 0.0
assert state.state == STATE_CLOSED
async def test_template_tilt(hass, calls):
"""Test the tilt_template attribute."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'value_template':
"{{ 1 == 1 }}",
'tilt_template':
"{{ 42 }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test_state'
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_tilt_position') == 42.0
async def test_template_out_of_bounds(hass, calls):
"""Test template out-of-bounds condition."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'position_template':
"{{ -1 }}",
'tilt_template':
"{{ 110 }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test_state'
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_tilt_position') is None
assert state.attributes.get('current_position') is None
async def test_template_mutex(hass, calls):
"""Test that only value or position template can be used."""
with assert_setup_component(0, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'value_template':
"{{ 1 == 1 }}",
'position_template':
"{{ 42 }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test_state'
},
'icon_template':
"{% if states.cover.test_state.state %}"
"mdi:check"
"{% endif %}"
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.async_all() == []
async def test_template_open_or_position(hass, calls):
"""Test that at least one of open_cover or set_position is used."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'value_template':
"{{ 1 == 1 }}",
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.async_all() == []
async def test_template_open_and_close(hass, calls):
"""Test that if open_cover is specified, close_cover is too."""
with assert_setup_component(0, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'value_template':
"{{ 1 == 1 }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
},
}
}
})
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.async_all() == []
async def test_template_non_numeric(hass, calls):
"""Test that tilt_template values are numeric."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'position_template':
"{{ on }}",
'tilt_template':
"{% if states.cover.test_state.state %}"
"on"
"{% else %}"
"off"
"{% endif %}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test_state'
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_tilt_position') is None
assert state.attributes.get('current_position') is None
async def test_open_action(hass, calls):
"""Test the open_cover command."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'position_template':
"{{ 0 }}",
'open_cover': {
'service': 'test.automation',
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test_state'
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.state == STATE_CLOSED
await hass.services.async_call(
DOMAIN, SERVICE_OPEN_COVER,
{ATTR_ENTITY_ID: ENTITY_COVER}, blocking=True)
await hass.async_block_till_done()
assert len(calls) == 1
async def test_close_stop_action(hass, calls):
"""Test the close-cover and stop_cover commands."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'position_template':
"{{ 100 }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
'close_cover': {
'service': 'test.automation',
},
'stop_cover': {
'service': 'test.automation',
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.state == STATE_OPEN
await hass.services.async_call(
DOMAIN, SERVICE_CLOSE_COVER,
{ATTR_ENTITY_ID: ENTITY_COVER}, blocking=True)
await hass.async_block_till_done()
await hass.services.async_call(
DOMAIN, SERVICE_STOP_COVER,
{ATTR_ENTITY_ID: ENTITY_COVER}, blocking=True)
await hass.async_block_till_done()
assert len(calls) == 2
async def test_set_position(hass, calls):
"""Test the set_position command."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'input_number', {
'input_number': {
'test': {
'min': '0',
'max': '100',
'initial': '42',
}
}
})
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'position_template':
"{{ states.input_number.test.state | int }}",
'set_cover_position': {
'service': 'input_number.set_value',
'entity_id': 'input_number.test',
'data_template': {
'value': '{{ position }}'
},
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.async_set('input_number.test', 42)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.state == STATE_OPEN
await hass.services.async_call(
DOMAIN, SERVICE_OPEN_COVER,
{ATTR_ENTITY_ID: ENTITY_COVER}, blocking=True)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_position') == 100.0
await hass.services.async_call(
DOMAIN, SERVICE_CLOSE_COVER,
{ATTR_ENTITY_ID: ENTITY_COVER}, blocking=True)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_position') == 0.0
await hass.services.async_call(
DOMAIN, SERVICE_SET_COVER_POSITION,
{ATTR_ENTITY_ID: ENTITY_COVER, ATTR_POSITION: 25}, blocking=True)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_position') == 25.0
async def test_set_tilt_position(hass, calls):
"""Test the set_tilt_position command."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'position_template':
"{{ 100 }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test_state'
},
'set_cover_tilt_position': {
'service': 'test.automation',
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
await hass.services.async_call(
DOMAIN, SERVICE_SET_COVER_TILT_POSITION,
{ATTR_ENTITY_ID: ENTITY_COVER, ATTR_TILT_POSITION: 42},
blocking=True)
await hass.async_block_till_done()
assert len(calls) == 1
async def test_open_tilt_action(hass, calls):
"""Test the open_cover_tilt command."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'position_template':
"{{ 100 }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test_state'
},
'set_cover_tilt_position': {
'service': 'test.automation',
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
await hass.services.async_call(
DOMAIN, SERVICE_OPEN_COVER_TILT,
{ATTR_ENTITY_ID: ENTITY_COVER}, blocking=True)
await hass.async_block_till_done()
assert len(calls) == 1
async def test_close_tilt_action(hass, calls):
"""Test the close_cover_tilt command."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'position_template':
"{{ 100 }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test_state'
},
'set_cover_tilt_position': {
'service': 'test.automation',
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
await hass.services.async_call(
DOMAIN, SERVICE_CLOSE_COVER_TILT,
{ATTR_ENTITY_ID: ENTITY_COVER}, blocking=True)
await hass.async_block_till_done()
assert len(calls) == 1
async def test_set_position_optimistic(hass, calls):
"""Test optimistic position mode."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'set_cover_position': {
'service': 'test.automation',
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_position') is None
await hass.services.async_call(
DOMAIN, SERVICE_SET_COVER_POSITION,
{ATTR_ENTITY_ID: ENTITY_COVER, ATTR_POSITION: 42}, blocking=True)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_position') == 42.0
await hass.services.async_call(
DOMAIN, SERVICE_CLOSE_COVER,
{ATTR_ENTITY_ID: ENTITY_COVER}, blocking=True)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.state == STATE_CLOSED
await hass.services.async_call(
DOMAIN, SERVICE_OPEN_COVER,
{ATTR_ENTITY_ID: ENTITY_COVER}, blocking=True)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.state == STATE_OPEN
async def test_set_tilt_position_optimistic(hass, calls):
"""Test the optimistic tilt_position mode."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'position_template':
"{{ 100 }}",
'set_cover_position': {
'service': 'test.automation',
},
'set_cover_tilt_position': {
'service': 'test.automation',
},
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_tilt_position') is None
await hass.services.async_call(
DOMAIN, SERVICE_SET_COVER_TILT_POSITION,
{ATTR_ENTITY_ID: ENTITY_COVER, ATTR_TILT_POSITION: 42},
blocking=True)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_tilt_position') == 42.0
await hass.services.async_call(
DOMAIN, SERVICE_CLOSE_COVER_TILT,
{ATTR_ENTITY_ID: ENTITY_COVER}, blocking=True)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_tilt_position') == 0.0
await hass.services.async_call(
DOMAIN, SERVICE_OPEN_COVER_TILT,
{ATTR_ENTITY_ID: ENTITY_COVER}, blocking=True)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('current_tilt_position') == 100.0
async def test_icon_template(hass, calls):
"""Test icon template."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'value_template':
"{{ states.cover.test_state.state }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test_state'
},
'icon_template':
"{% if states.cover.test_state.state %}"
"mdi:check"
"{% endif %}"
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('icon') == ''
state = hass.states.async_set('cover.test_state', STATE_OPEN)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes['icon'] == 'mdi:check'
async def test_entity_picture_template(hass, calls):
"""Test icon template."""
with assert_setup_component(1, 'cover'):
assert await setup.async_setup_component(hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'value_template':
"{{ states.cover.test_state.state }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test_state'
},
'entity_picture_template':
"{% if states.cover.test_state.state %}"
"/local/cover.png"
"{% endif %}"
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes.get('entity_picture') == ''
state = hass.states.async_set('cover.test_state', STATE_OPEN)
await hass.async_block_till_done()
state = hass.states.get('cover.test_template_cover')
assert state.attributes['entity_picture'] == '/local/cover.png'
+648
View File
@@ -0,0 +1,648 @@
"""The tests for the Template fan platform."""
import logging
import pytest
from homeassistant import setup
from homeassistant.const import STATE_ON, STATE_OFF
from homeassistant.components.fan import (
ATTR_SPEED, ATTR_OSCILLATING, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH,
ATTR_DIRECTION, DIRECTION_FORWARD, DIRECTION_REVERSE)
from tests.common import (
async_mock_service, assert_setup_component)
from tests.components.fan import common
_LOGGER = logging.getLogger(__name__)
_TEST_FAN = 'fan.test_fan'
# Represent for fan's state
_STATE_INPUT_BOOLEAN = 'input_boolean.state'
# Represent for fan's speed
_SPEED_INPUT_SELECT = 'input_select.speed'
# Represent for fan's oscillating
_OSC_INPUT = 'input_select.osc'
# Represent for fan's direction
_DIRECTION_INPUT_SELECT = 'input_select.direction'
@pytest.fixture
def calls(hass):
"""Track calls to a mock serivce."""
return async_mock_service(hass, 'test', 'automation')
# Configuration tests #
async def test_missing_optional_config(hass, calls):
"""Test: missing optional template is ok."""
with assert_setup_component(1, 'fan'):
assert await setup.async_setup_component(hass, 'fan', {
'fan': {
'platform': 'template',
'fans': {
'test_fan': {
'value_template': "{{ 'on' }}",
'turn_on': {
'service': 'script.fan_on'
},
'turn_off': {
'service': 'script.fan_off'
}
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
_verify(hass, STATE_ON, None, None, None)
async def test_missing_value_template_config(hass, calls):
"""Test: missing 'value_template' will fail."""
with assert_setup_component(0, 'fan'):
assert await setup.async_setup_component(hass, 'fan', {
'fan': {
'platform': 'template',
'fans': {
'test_fan': {
'turn_on': {
'service': 'script.fan_on'
},
'turn_off': {
'service': 'script.fan_off'
}
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.async_all() == []
async def test_missing_turn_on_config(hass, calls):
"""Test: missing 'turn_on' will fail."""
with assert_setup_component(0, 'fan'):
assert await setup.async_setup_component(hass, 'fan', {
'fan': {
'platform': 'template',
'fans': {
'test_fan': {
'value_template': "{{ 'on' }}",
'turn_off': {
'service': 'script.fan_off'
}
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.async_all() == []
async def test_missing_turn_off_config(hass, calls):
"""Test: missing 'turn_off' will fail."""
with assert_setup_component(0, 'fan'):
assert await setup.async_setup_component(hass, 'fan', {
'fan': {
'platform': 'template',
'fans': {
'test_fan': {
'value_template': "{{ 'on' }}",
'turn_on': {
'service': 'script.fan_on'
}
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.async_all() == []
async def test_invalid_config(hass, calls):
"""Test: missing 'turn_off' will fail."""
with assert_setup_component(0, 'fan'):
assert await setup.async_setup_component(hass, 'fan', {
'platform': 'template',
'fans': {
'test_fan': {
'value_template': "{{ 'on' }}",
'turn_on': {
'service': 'script.fan_on'
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.async_all() == []
# End of configuration tests #
# Template tests #
async def test_templates_with_entities(hass, calls):
"""Test tempalates with values from other entities."""
value_template = """
{% if is_state('input_boolean.state', 'True') %}
{{ 'on' }}
{% else %}
{{ 'off' }}
{% endif %}
"""
with assert_setup_component(1, 'fan'):
assert await setup.async_setup_component(hass, 'fan', {
'fan': {
'platform': 'template',
'fans': {
'test_fan': {
'value_template': value_template,
'speed_template':
"{{ states('input_select.speed') }}",
'oscillating_template':
"{{ states('input_select.osc') }}",
'direction_template':
"{{ states('input_select.direction') }}",
'turn_on': {
'service': 'script.fan_on'
},
'turn_off': {
'service': 'script.fan_off'
}
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
_verify(hass, STATE_OFF, None, None, None)
hass.states.async_set(_STATE_INPUT_BOOLEAN, True)
hass.states.async_set(_SPEED_INPUT_SELECT, SPEED_MEDIUM)
hass.states.async_set(_OSC_INPUT, 'True')
hass.states.async_set(_DIRECTION_INPUT_SELECT, DIRECTION_FORWARD)
await hass.async_block_till_done()
_verify(hass, STATE_ON, SPEED_MEDIUM, True, DIRECTION_FORWARD)
async def test_templates_with_valid_values(hass, calls):
"""Test templates with valid values."""
with assert_setup_component(1, 'fan'):
assert await setup.async_setup_component(hass, 'fan', {
'fan': {
'platform': 'template',
'fans': {
'test_fan': {
'value_template':
"{{ 'on' }}",
'speed_template':
"{{ 'medium' }}",
'oscillating_template':
"{{ 1 == 1 }}",
'direction_template':
"{{ 'forward' }}",
'turn_on': {
'service': 'script.fan_on'
},
'turn_off': {
'service': 'script.fan_off'
}
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
_verify(hass, STATE_ON, SPEED_MEDIUM, True, DIRECTION_FORWARD)
async def test_templates_invalid_values(hass, calls):
"""Test templates with invalid values."""
with assert_setup_component(1, 'fan'):
assert await setup.async_setup_component(hass, 'fan', {
'fan': {
'platform': 'template',
'fans': {
'test_fan': {
'value_template':
"{{ 'abc' }}",
'speed_template':
"{{ '0' }}",
'oscillating_template':
"{{ 'xyz' }}",
'direction_template':
"{{ 'right' }}",
'turn_on': {
'service': 'script.fan_on'
},
'turn_off': {
'service': 'script.fan_off'
}
}
}
}
})
await hass.async_start()
await hass.async_block_till_done()
_verify(hass, STATE_OFF, None, None, None)
# End of template tests #
# Function tests #
async def test_on_off(hass, calls):
"""Test turn on and turn off."""
await _register_components(hass)
# Turn on fan
common.async_turn_on(hass, _TEST_FAN)
await hass.async_block_till_done()
# verify
assert hass.states.get(_STATE_INPUT_BOOLEAN).state == STATE_ON
_verify(hass, STATE_ON, None, None, None)
# Turn off fan
common.async_turn_off(hass, _TEST_FAN)
await hass.async_block_till_done()
# verify
assert hass.states.get(_STATE_INPUT_BOOLEAN).state == STATE_OFF
_verify(hass, STATE_OFF, None, None, None)
async def test_on_with_speed(hass, calls):
"""Test turn on with speed."""
await _register_components(hass)
# Turn on fan with high speed
common.async_turn_on(hass, _TEST_FAN, SPEED_HIGH)
await hass.async_block_till_done()
# verify
assert hass.states.get(_STATE_INPUT_BOOLEAN).state == STATE_ON
assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH
_verify(hass, STATE_ON, SPEED_HIGH, None, None)
async def test_set_speed(hass, calls):
"""Test set valid speed."""
await _register_components(hass)
# Turn on fan
common.async_turn_on(hass, _TEST_FAN)
await hass.async_block_till_done()
# Set fan's speed to high
common.async_set_speed(hass, _TEST_FAN, SPEED_HIGH)
await hass.async_block_till_done()
# verify
assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH
_verify(hass, STATE_ON, SPEED_HIGH, None, None)
# Set fan's speed to medium
common.async_set_speed(hass, _TEST_FAN, SPEED_MEDIUM)
await hass.async_block_till_done()
# verify
assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_MEDIUM
_verify(hass, STATE_ON, SPEED_MEDIUM, None, None)
async def test_set_invalid_speed_from_initial_stage(hass, calls):
"""Test set invalid speed when fan is in initial state."""
await _register_components(hass)
# Turn on fan
common.async_turn_on(hass, _TEST_FAN)
await hass.async_block_till_done()
# Set fan's speed to 'invalid'
common.async_set_speed(hass, _TEST_FAN, 'invalid')
await hass.async_block_till_done()
# verify speed is unchanged
assert hass.states.get(_SPEED_INPUT_SELECT).state == ''
_verify(hass, STATE_ON, None, None, None)
async def test_set_invalid_speed(hass, calls):
"""Test set invalid speed when fan has valid speed."""
await _register_components(hass)
# Turn on fan
common.async_turn_on(hass, _TEST_FAN)
await hass.async_block_till_done()
# Set fan's speed to high
common.async_set_speed(hass, _TEST_FAN, SPEED_HIGH)
await hass.async_block_till_done()
# verify
assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH
_verify(hass, STATE_ON, SPEED_HIGH, None, None)
# Set fan's speed to 'invalid'
common.async_set_speed(hass, _TEST_FAN, 'invalid')
await hass.async_block_till_done()
# verify speed is unchanged
assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH
_verify(hass, STATE_ON, SPEED_HIGH, None, None)
async def test_custom_speed_list(hass, calls):
"""Test set custom speed list."""
await _register_components(hass, ['1', '2', '3'])
# Turn on fan
common.async_turn_on(hass, _TEST_FAN)
await hass.async_block_till_done()
# Set fan's speed to '1'
common.async_set_speed(hass, _TEST_FAN, '1')
await hass.async_block_till_done()
# verify
assert hass.states.get(_SPEED_INPUT_SELECT).state == '1'
_verify(hass, STATE_ON, '1', None, None)
# Set fan's speed to 'medium' which is invalid
common.async_set_speed(hass, _TEST_FAN, SPEED_MEDIUM)
await hass.async_block_till_done()
# verify that speed is unchanged
assert hass.states.get(_SPEED_INPUT_SELECT).state == '1'
_verify(hass, STATE_ON, '1', None, None)
async def test_set_osc(hass, calls):
"""Test set oscillating."""
await _register_components(hass)
# Turn on fan
common.async_turn_on(hass, _TEST_FAN)
await hass.async_block_till_done()
# Set fan's osc to True
common.async_oscillate(hass, _TEST_FAN, True)
await hass.async_block_till_done()
# verify
assert hass.states.get(_OSC_INPUT).state == 'True'
_verify(hass, STATE_ON, None, True, None)
# Set fan's osc to False
common.async_oscillate(hass, _TEST_FAN, False)
await hass.async_block_till_done()
# verify
assert hass.states.get(_OSC_INPUT).state == 'False'
_verify(hass, STATE_ON, None, False, None)
async def test_set_invalid_osc_from_initial_state(hass, calls):
"""Test set invalid oscillating when fan is in initial state."""
await _register_components(hass)
# Turn on fan
common.async_turn_on(hass, _TEST_FAN)
await hass.async_block_till_done()
# Set fan's osc to 'invalid'
common.async_oscillate(hass, _TEST_FAN, 'invalid')
await hass.async_block_till_done()
# verify
assert hass.states.get(_OSC_INPUT).state == ''
_verify(hass, STATE_ON, None, None, None)
async def test_set_invalid_osc(hass, calls):
"""Test set invalid oscillating when fan has valid osc."""
await _register_components(hass)
# Turn on fan
common.async_turn_on(hass, _TEST_FAN)
await hass.async_block_till_done()
# Set fan's osc to True
common.async_oscillate(hass, _TEST_FAN, True)
await hass.async_block_till_done()
# verify
assert hass.states.get(_OSC_INPUT).state == 'True'
_verify(hass, STATE_ON, None, True, None)
# Set fan's osc to False
common.async_oscillate(hass, _TEST_FAN, None)
await hass.async_block_till_done()
# verify osc is unchanged
assert hass.states.get(_OSC_INPUT).state == 'True'
_verify(hass, STATE_ON, None, True, None)
async def test_set_direction(hass, calls):
"""Test set valid direction."""
await _register_components(hass)
# Turn on fan
common.async_turn_on(hass, _TEST_FAN)
await hass.async_block_till_done()
# Set fan's direction to forward
common.async_set_direction(hass, _TEST_FAN, DIRECTION_FORWARD)
await hass.async_block_till_done()
# verify
assert hass.states.get(_DIRECTION_INPUT_SELECT).state \
== DIRECTION_FORWARD
_verify(hass, STATE_ON, None, None, DIRECTION_FORWARD)
# Set fan's direction to reverse
common.async_set_direction(hass, _TEST_FAN, DIRECTION_REVERSE)
await hass.async_block_till_done()
# verify
assert hass.states.get(_DIRECTION_INPUT_SELECT).state \
== DIRECTION_REVERSE
_verify(hass, STATE_ON, None, None, DIRECTION_REVERSE)
async def test_set_invalid_direction_from_initial_stage(hass, calls):
"""Test set invalid direction when fan is in initial state."""
await _register_components(hass)
# Turn on fan
common.async_turn_on(hass, _TEST_FAN)
await hass.async_block_till_done()
# Set fan's direction to 'invalid'
common.async_set_direction(hass, _TEST_FAN, 'invalid')
await hass.async_block_till_done()
# verify direction is unchanged
assert hass.states.get(_DIRECTION_INPUT_SELECT).state == ''
_verify(hass, STATE_ON, None, None, None)
async def test_set_invalid_direction(hass, calls):
"""Test set invalid direction when fan has valid direction."""
await _register_components(hass)
# Turn on fan
common.async_turn_on(hass, _TEST_FAN)
await hass.async_block_till_done()
# Set fan's direction to forward
common.async_set_direction(hass, _TEST_FAN, DIRECTION_FORWARD)
await hass.async_block_till_done()
# verify
assert hass.states.get(_DIRECTION_INPUT_SELECT).state == \
DIRECTION_FORWARD
_verify(hass, STATE_ON, None, None, DIRECTION_FORWARD)
# Set fan's direction to 'invalid'
common.async_set_direction(hass, _TEST_FAN, 'invalid')
await hass.async_block_till_done()
# verify direction is unchanged
assert hass.states.get(_DIRECTION_INPUT_SELECT).state == \
DIRECTION_FORWARD
_verify(hass, STATE_ON, None, None, DIRECTION_FORWARD)
def _verify(hass, expected_state, expected_speed, expected_oscillating,
expected_direction):
"""Verify fan's state, speed and osc."""
state = hass.states.get(_TEST_FAN)
attributes = state.attributes
assert state.state == expected_state
assert attributes.get(ATTR_SPEED, None) == expected_speed
assert attributes.get(ATTR_OSCILLATING, None) == expected_oscillating
assert attributes.get(ATTR_DIRECTION, None) == expected_direction
async def _register_components(hass, speed_list=None):
"""Register basic components for testing."""
with assert_setup_component(1, 'input_boolean'):
assert await setup.async_setup_component(
hass,
'input_boolean',
{'input_boolean': {'state': None}}
)
with assert_setup_component(3, 'input_select'):
assert await setup.async_setup_component(hass, 'input_select', {
'input_select': {
'speed': {
'name': 'Speed',
'options': ['', SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH,
'1', '2', '3']
},
'osc': {
'name': 'oscillating',
'options': ['', 'True', 'False']
},
'direction': {
'name': 'Direction',
'options': ['', DIRECTION_FORWARD, DIRECTION_REVERSE]
},
}
})
with assert_setup_component(1, 'fan'):
value_template = """
{% if is_state('input_boolean.state', 'on') %}
{{ 'on' }}
{% else %}
{{ 'off' }}
{% endif %}
"""
test_fan_config = {
'value_template': value_template,
'speed_template':
"{{ states('input_select.speed') }}",
'oscillating_template':
"{{ states('input_select.osc') }}",
'direction_template':
"{{ states('input_select.direction') }}",
'turn_on': {
'service': 'input_boolean.turn_on',
'entity_id': _STATE_INPUT_BOOLEAN
},
'turn_off': {
'service': 'input_boolean.turn_off',
'entity_id': _STATE_INPUT_BOOLEAN
},
'set_speed': {
'service': 'input_select.select_option',
'data_template': {
'entity_id': _SPEED_INPUT_SELECT,
'option': '{{ speed }}'
}
},
'set_oscillating': {
'service': 'input_select.select_option',
'data_template': {
'entity_id': _OSC_INPUT,
'option': '{{ oscillating }}'
}
},
'set_direction': {
'service': 'input_select.select_option',
'data_template': {
'entity_id': _DIRECTION_INPUT_SELECT,
'option': '{{ direction }}'
}
}
}
if speed_list:
test_fan_config['speeds'] = speed_list
assert await setup.async_setup_component(hass, 'fan', {
'fan': {
'platform': 'template',
'fans': {
'test_fan': test_fan_config
}
}
})
await hass.async_start()
await hass.async_block_till_done()
+722
View File
@@ -0,0 +1,722 @@
"""The tests for the Template light platform."""
import logging
from homeassistant.core import callback
from homeassistant import setup
from homeassistant.components.light import ATTR_BRIGHTNESS
from homeassistant.const import STATE_ON, STATE_OFF
from tests.common import (
get_test_home_assistant, assert_setup_component)
from tests.components.light import common
_LOGGER = logging.getLogger(__name__)
class TestTemplateLight:
"""Test the Template light."""
hass = None
calls = None
# pylint: disable=invalid-name
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.calls = []
@callback
def record_call(service):
"""Track function calls.."""
self.calls.append(service)
self.hass.services.register('test', 'automation', record_call)
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_template_state_text(self):
"""Test the state text of a template."""
with assert_setup_component(1, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'value_template':
"{{ states.light.test_state.state }}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
}
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.set('light.test_state', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.state == STATE_ON
state = self.hass.states.set('light.test_state', STATE_OFF)
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.state == STATE_OFF
def test_template_state_boolean_on(self):
"""Test the setting of the state with boolean on."""
with assert_setup_component(1, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'value_template': "{{ 1 == 1 }}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
}
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.state == STATE_ON
def test_template_state_boolean_off(self):
"""Test the setting of the state with off."""
with assert_setup_component(1, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'value_template': "{{ 1 == 2 }}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
}
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.state == STATE_OFF
def test_template_syntax_error(self):
"""Test templating syntax error."""
with assert_setup_component(0, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'value_template': "{%- if false -%}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
}
}
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_invalid_name_does_not_create(self):
"""Test invalid name."""
with assert_setup_component(0, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'bad name here': {
'value_template': "{{ 1== 1}}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
}
}
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_invalid_light_does_not_create(self):
"""Test invalid light."""
with assert_setup_component(0, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'switches': {
'test_template_light': 'Invalid'
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_no_lights_does_not_create(self):
"""Test if there are no lights no creation."""
with assert_setup_component(0, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template'
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_missing_template_does_create(self):
"""Test missing template."""
with assert_setup_component(1, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'light_one': {
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
}
}
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() != []
def test_missing_on_does_not_create(self):
"""Test missing on."""
with assert_setup_component(0, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'bad name here': {
'value_template': "{{ 1== 1}}",
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
}
}
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_missing_off_does_not_create(self):
"""Test missing off."""
with assert_setup_component(0, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'bad name here': {
'value_template': "{{ 1== 1}}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
}
}
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_on_action(self):
"""Test on action."""
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'value_template': "{{states.light.test_state.state}}",
'turn_on': {
'service': 'test.automation',
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
}
}
}
}
})
self.hass.start()
self.hass.block_till_done()
self.hass.states.set('light.test_state', STATE_OFF)
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.state == STATE_OFF
common.turn_on(self.hass, 'light.test_template_light')
self.hass.block_till_done()
assert len(self.calls) == 1
def test_on_action_optimistic(self):
"""Test on action with optimistic state."""
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'turn_on': {
'service': 'test.automation',
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
}
}
}
}
})
self.hass.start()
self.hass.block_till_done()
self.hass.states.set('light.test_state', STATE_OFF)
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.state == STATE_OFF
common.turn_on(self.hass, 'light.test_template_light')
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert len(self.calls) == 1
assert state.state == STATE_ON
def test_off_action(self):
"""Test off action."""
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'value_template': "{{states.light.test_state.state}}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'test.automation',
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
}
}
}
}
})
self.hass.start()
self.hass.block_till_done()
self.hass.states.set('light.test_state', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.state == STATE_ON
common.turn_off(self.hass, 'light.test_template_light')
self.hass.block_till_done()
assert len(self.calls) == 1
def test_off_action_optimistic(self):
"""Test off action with optimistic state."""
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'test.automation',
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
}
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.state == STATE_OFF
common.turn_off(self.hass, 'light.test_template_light')
self.hass.block_till_done()
assert len(self.calls) == 1
state = self.hass.states.get('light.test_template_light')
assert state.state == STATE_OFF
def test_level_action_no_template(self):
"""Test setting brightness with optimistic template."""
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'value_template': '{{1 == 1}}',
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'test.automation',
'data_template': {
'entity_id': 'test.test_state',
'brightness': '{{brightness}}'
}
},
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.attributes.get('brightness') is None
common.turn_on(
self.hass, 'light.test_template_light', **{ATTR_BRIGHTNESS: 124})
self.hass.block_till_done()
assert len(self.calls) == 1
assert self.calls[0].data['brightness'] == '124'
state = self.hass.states.get('light.test_template_light')
_LOGGER.info(str(state.attributes))
assert state is not None
assert state.attributes.get('brightness') == 124
def test_level_template(self):
"""Test the template for the level."""
with assert_setup_component(1, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'value_template': "{{ 1 == 1 }}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
},
'level_template':
'{{42}}'
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state is not None
assert state.attributes.get('brightness') == 42
def test_friendly_name(self):
"""Test the accessibility of the friendly_name attribute."""
with assert_setup_component(1, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'friendly_name': 'Template light',
'value_template': "{{ 1 == 1 }}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
}
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state is not None
assert state.attributes.get('friendly_name') == 'Template light'
def test_icon_template(self):
"""Test icon template."""
with assert_setup_component(1, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'friendly_name': 'Template light',
'value_template': "{{ 1 == 1 }}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
},
'icon_template':
"{% if states.light.test_state.state %}"
"mdi:check"
"{% endif %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.attributes.get('icon') == ''
state = self.hass.states.set('light.test_state', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.attributes['icon'] == 'mdi:check'
def test_entity_picture_template(self):
"""Test entity_picture template."""
with assert_setup_component(1, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'friendly_name': 'Template light',
'value_template': "{{ 1 == 1 }}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
},
'entity_picture_template':
"{% if states.light.test_state.state %}"
"/local/light.png"
"{% endif %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.attributes.get('entity_picture') == ''
state = self.hass.states.set('light.test_state', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.attributes['entity_picture'] == '/local/light.png'
+309
View File
@@ -0,0 +1,309 @@
"""The tests for the Template lock platform."""
import logging
from homeassistant.core import callback
from homeassistant import setup
from homeassistant.components import lock
from homeassistant.const import STATE_ON, STATE_OFF
from tests.common import (get_test_home_assistant,
assert_setup_component)
_LOGGER = logging.getLogger(__name__)
class TestTemplateLock:
"""Test the Template lock."""
hass = None
calls = None
# pylint: disable=invalid-name
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.calls = []
@callback
def record_call(service):
"""Track function calls."""
self.calls.append(service)
self.hass.services.register('test', 'automation', record_call)
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_template_state(self):
"""Test template."""
with assert_setup_component(1, 'lock'):
assert setup.setup_component(self.hass, 'lock', {
'lock': {
'platform': 'template',
'name': 'Test template lock',
'value_template':
"{{ states.switch.test_state.state }}",
'lock': {
'service': 'switch.turn_on',
'entity_id': 'switch.test_state'
},
'unlock': {
'service': 'switch.turn_off',
'entity_id': 'switch.test_state'
}
}
})
self.hass.start()
self.hass.block_till_done()
self.hass.states.set('switch.test_state', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get('lock.test_template_lock')
assert state.state == lock.STATE_LOCKED
self.hass.states.set('switch.test_state', STATE_OFF)
self.hass.block_till_done()
state = self.hass.states.get('lock.test_template_lock')
assert state.state == lock.STATE_UNLOCKED
def test_template_state_boolean_on(self):
"""Test the setting of the state with boolean on."""
with assert_setup_component(1, 'lock'):
assert setup.setup_component(self.hass, 'lock', {
'lock': {
'platform': 'template',
'value_template':
"{{ 1 == 1 }}",
'lock': {
'service': 'switch.turn_on',
'entity_id': 'switch.test_state'
},
'unlock': {
'service': 'switch.turn_off',
'entity_id': 'switch.test_state'
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('lock.template_lock')
assert state.state == lock.STATE_LOCKED
def test_template_state_boolean_off(self):
"""Test the setting of the state with off."""
with assert_setup_component(1, 'lock'):
assert setup.setup_component(self.hass, 'lock', {
'lock': {
'platform': 'template',
'value_template':
"{{ 1 == 2 }}",
'lock': {
'service': 'switch.turn_on',
'entity_id': 'switch.test_state'
},
'unlock': {
'service': 'switch.turn_off',
'entity_id': 'switch.test_state'
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('lock.template_lock')
assert state.state == lock.STATE_UNLOCKED
def test_template_syntax_error(self):
"""Test templating syntax error."""
with assert_setup_component(0, 'lock'):
assert setup.setup_component(self.hass, 'lock', {
'lock': {
'platform': 'template',
'value_template':
"{% if rubbish %}",
'lock': {
'service': 'switch.turn_on',
'entity_id': 'switch.test_state'
},
'unlock': {
'service': 'switch.turn_off',
'entity_id': 'switch.test_state'
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_invalid_name_does_not_create(self):
"""Test invalid name."""
with assert_setup_component(0, 'lock'):
assert setup.setup_component(self.hass, 'lock', {
'switch': {
'platform': 'lock',
'name': '{{%}',
'value_template':
"{{ rubbish }",
'lock': {
'service': 'switch.turn_on',
'entity_id': 'switch.test_state'
},
'unlock': {
'service': 'switch.turn_off',
'entity_id': 'switch.test_state'
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_invalid_lock_does_not_create(self):
"""Test invalid lock."""
with assert_setup_component(0, 'lock'):
assert setup.setup_component(self.hass, 'lock', {
'lock': {
'platform': 'template',
'value_template': "Invalid"
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_missing_template_does_not_create(self):
"""Test missing template."""
with assert_setup_component(0, 'lock'):
assert setup.setup_component(self.hass, 'lock', {
'lock': {
'platform': 'template',
'not_value_template':
"{{ states.switch.test_state.state }}",
'lock': {
'service': 'switch.turn_on',
'entity_id': 'switch.test_state'
},
'unlock': {
'service': 'switch.turn_off',
'entity_id': 'switch.test_state'
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_no_template_match_all(self, caplog):
"""Test that we do not allow locks that match on all."""
with assert_setup_component(1, 'lock'):
assert setup.setup_component(self.hass, 'lock', {
'lock': {
'platform': 'template',
'value_template': '{{ 1 + 1 }}',
'lock': {
'service': 'switch.turn_on',
'entity_id': 'switch.test_state'
},
'unlock': {
'service': 'switch.turn_off',
'entity_id': 'switch.test_state'
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('lock.template_lock')
assert state.state == lock.STATE_UNLOCKED
assert ('Template lock Template Lock has no entity ids configured '
'to track nor were we able to extract the entities to track '
'from the value_template template. This entity will only '
'be able to be updated manually.') in caplog.text
self.hass.states.set('lock.template_lock', lock.STATE_LOCKED)
self.hass.block_till_done()
state = self.hass.states.get('lock.template_lock')
assert state.state == lock.STATE_LOCKED
def test_lock_action(self):
"""Test lock action."""
assert setup.setup_component(self.hass, 'lock', {
'lock': {
'platform': 'template',
'value_template':
"{{ states.switch.test_state.state }}",
'lock': {
'service': 'test.automation'
},
'unlock': {
'service': 'switch.turn_off',
'entity_id': 'switch.test_state'
}
}
})
self.hass.start()
self.hass.block_till_done()
self.hass.states.set('switch.test_state', STATE_OFF)
self.hass.block_till_done()
state = self.hass.states.get('lock.template_lock')
assert state.state == lock.STATE_UNLOCKED
self.hass.services.call(lock.DOMAIN, lock.SERVICE_LOCK, {
lock.ATTR_ENTITY_ID: 'lock.template_lock'
})
self.hass.block_till_done()
assert len(self.calls) == 1
def test_unlock_action(self):
"""Test unlock action."""
assert setup.setup_component(self.hass, 'lock', {
'lock': {
'platform': 'template',
'value_template':
"{{ states.switch.test_state.state }}",
'lock': {
'service': 'switch.turn_on',
'entity_id': 'switch.test_state'
},
'unlock': {
'service': 'test.automation'
}
}
})
self.hass.start()
self.hass.block_till_done()
self.hass.states.set('switch.test_state', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get('lock.template_lock')
assert state.state == lock.STATE_LOCKED
self.hass.services.call(lock.DOMAIN, lock.SERVICE_UNLOCK, {
lock.ATTR_ENTITY_ID: 'lock.template_lock'
})
self.hass.block_till_done()
assert len(self.calls) == 1
+395
View File
@@ -0,0 +1,395 @@
"""The test for the Template sensor platform."""
from homeassistant.const import EVENT_HOMEASSISTANT_START
from homeassistant.setup import setup_component, async_setup_component
from tests.common import get_test_home_assistant, assert_setup_component
class TestTemplateSensor:
"""Test the Template sensor."""
hass = None
# pylint: disable=invalid-name
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_template(self):
"""Test template."""
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 }}."
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_template_sensor')
assert state.state == 'It .'
self.hass.states.set('sensor.test_state', 'Works')
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_template_sensor')
assert state.state == 'It Works.'
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':
"{{ states.sensor.test_state.state }}",
'icon_template':
"{% if states.sensor.test_state.state == "
"'Works' %}"
"mdi:check"
"{% endif %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_template_sensor')
assert state.attributes.get('icon') == ''
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'
def test_entity_picture_template(self):
"""Test entity_picture template."""
with assert_setup_component(1):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test_template_sensor': {
'value_template':
"{{ states.sensor.test_state.state }}",
'entity_picture_template':
"{% if states.sensor.test_state.state == "
"'Works' %}"
"/local/sensor.png"
"{% endif %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_template_sensor')
assert state.attributes.get('entity_picture') == ''
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['entity_picture'] == '/local/sensor.png'
def test_friendly_name_template(self):
"""Test friendly_name template."""
with assert_setup_component(1):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test_template_sensor': {
'value_template':
"{{ states.sensor.test_state.state }}",
'friendly_name_template':
"It {{ states.sensor.test_state.state }}."
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_template_sensor')
assert state.attributes.get('friendly_name') == 'It .'
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['friendly_name'] == 'It Works.'
def test_friendly_name_template_with_unknown_state(self):
"""Test friendly_name template with an unknown value_template."""
with assert_setup_component(1):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test_template_sensor': {
'value_template': "{{ states.fourohfour.state }}",
'friendly_name_template':
"It {{ states.sensor.test_state.state }}."
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_template_sensor')
assert state.attributes['friendly_name'] == 'It .'
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['friendly_name'] == 'It Works.'
def test_template_syntax_error(self):
"""Test templating syntax error."""
with assert_setup_component(0):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test_template_sensor': {
'value_template':
"{% if rubbish %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_template_attribute_missing(self):
"""Test missing attribute template."""
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 }}.'
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_template_sensor')
assert state.state == 'unknown'
def test_invalid_name_does_not_create(self):
"""Test invalid name."""
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 }}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_invalid_sensor_does_not_create(self):
"""Test invalid sensor."""
with assert_setup_component(0):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test_template_sensor': 'invalid'
}
}
})
self.hass.start()
assert self.hass.states.all() == []
def test_no_sensors_does_not_create(self):
"""Test no sensors."""
with assert_setup_component(0):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template'
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_missing_template_does_not_create(self):
"""Test missing template."""
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 }}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_setup_invalid_device_class(self):
"""Test setup with invalid device_class."""
with assert_setup_component(0):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test': {
'value_template':
'{{ states.sensor.test_sensor.state }}',
'device_class': 'foobarnotreal',
},
},
}
})
def test_setup_valid_device_class(self):
"""Test setup with valid device_class."""
with assert_setup_component(1):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test1': {
'value_template':
'{{ states.sensor.test_sensor.state }}',
'device_class': 'temperature',
},
'test2': {
'value_template':
'{{ states.sensor.test_sensor.state }}'
},
}
}
})
self.hass.block_till_done()
state = self.hass.states.get('sensor.test1')
assert state.attributes['device_class'] == 'temperature'
state = self.hass.states.get('sensor.test2')
assert 'device_class' not in state.attributes
async def test_no_template_match_all(hass, caplog):
"""Test that we do not allow sensors that match on all."""
hass.states.async_set('sensor.test_sensor', 'startup')
await async_setup_component(hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'invalid_state': {
'value_template': '{{ 1 + 1 }}',
},
'invalid_icon': {
'value_template':
'{{ states.sensor.test_sensor.state }}',
'icon_template': '{{ 1 + 1 }}',
},
'invalid_entity_picture': {
'value_template':
'{{ states.sensor.test_sensor.state }}',
'entity_picture_template': '{{ 1 + 1 }}',
},
'invalid_friendly_name': {
'value_template':
'{{ states.sensor.test_sensor.state }}',
'friendly_name_template': '{{ 1 + 1 }}',
},
}
}
})
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 5
assert ('Template sensor invalid_state has no entity ids '
'configured to track nor were we able to extract the entities to '
'track from the value template') in caplog.text
assert ('Template sensor invalid_icon has no entity ids '
'configured to track nor were we able to extract the entities to '
'track from the icon template') in caplog.text
assert ('Template sensor invalid_entity_picture has no entity ids '
'configured to track nor were we able to extract the entities to '
'track from the entity_picture template') in caplog.text
assert ('Template sensor invalid_friendly_name has no entity ids '
'configured to track nor were we able to extract the entities to '
'track from the friendly_name template') in caplog.text
assert hass.states.get('sensor.invalid_state').state == 'unknown'
assert hass.states.get('sensor.invalid_icon').state == 'unknown'
assert hass.states.get('sensor.invalid_entity_picture').state == 'unknown'
assert hass.states.get('sensor.invalid_friendly_name').state == 'unknown'
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
assert hass.states.get('sensor.invalid_state').state == '2'
assert hass.states.get('sensor.invalid_icon').state == 'startup'
assert hass.states.get('sensor.invalid_entity_picture').state == 'startup'
assert hass.states.get('sensor.invalid_friendly_name').state == 'startup'
hass.states.async_set('sensor.test_sensor', 'hello')
await hass.async_block_till_done()
assert hass.states.get('sensor.invalid_state').state == '2'
assert hass.states.get('sensor.invalid_icon').state == 'startup'
assert hass.states.get('sensor.invalid_entity_picture').state == 'startup'
assert hass.states.get('sensor.invalid_friendly_name').state == 'startup'
await hass.helpers.entity_component.async_update_entity(
'sensor.invalid_state')
await hass.helpers.entity_component.async_update_entity(
'sensor.invalid_icon')
await hass.helpers.entity_component.async_update_entity(
'sensor.invalid_entity_picture')
await hass.helpers.entity_component.async_update_entity(
'sensor.invalid_friendly_name')
assert hass.states.get('sensor.invalid_state').state == '2'
assert hass.states.get('sensor.invalid_icon').state == 'hello'
assert hass.states.get('sensor.invalid_entity_picture').state == 'hello'
assert hass.states.get('sensor.invalid_friendly_name').state == 'hello'
+448
View File
@@ -0,0 +1,448 @@
"""The tests for the Template switch platform."""
from homeassistant.core import callback
from homeassistant import setup
from homeassistant.const import STATE_ON, STATE_OFF
from tests.common import (
get_test_home_assistant, assert_setup_component)
from tests.components.switch import common
class TestTemplateSwitch:
"""Test the Template switch."""
hass = None
calls = None
# pylint: disable=invalid-name
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.calls = []
@callback
def record_call(service):
"""Track function calls.."""
self.calls.append(service)
self.hass.services.register('test', 'automation', record_call)
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_template_state_text(self):
"""Test the state text of a template."""
with assert_setup_component(1, 'switch'):
assert setup.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'
},
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.set('switch.test_state', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.state == STATE_ON
state = self.hass.states.set('switch.test_state', STATE_OFF)
self.hass.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.state == STATE_OFF
def test_template_state_boolean_on(self):
"""Test the setting of the state with boolean on."""
with assert_setup_component(1, 'switch'):
assert setup.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'
},
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.state == STATE_ON
def test_template_state_boolean_off(self):
"""Test the setting of the state with off."""
with assert_setup_component(1, 'switch'):
assert setup.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'
},
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.state == STATE_OFF
def test_icon_template(self):
"""Test icon template."""
with assert_setup_component(1, 'switch'):
assert setup.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'
},
'icon_template':
"{% if states.switch.test_state.state %}"
"mdi:check"
"{% endif %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.attributes.get('icon') == ''
state = self.hass.states.set('switch.test_state', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.attributes['icon'] == 'mdi:check'
def test_entity_picture_template(self):
"""Test entity_picture template."""
with assert_setup_component(1, 'switch'):
assert setup.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'
},
'entity_picture_template':
"{% if states.switch.test_state.state %}"
"/local/switch.png"
"{% endif %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.attributes.get('entity_picture') == ''
state = self.hass.states.set('switch.test_state', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.attributes['entity_picture'] == '/local/switch.png'
def test_template_syntax_error(self):
"""Test templating syntax error."""
with assert_setup_component(0, 'switch'):
assert setup.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'
},
}
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_invalid_name_does_not_create(self):
"""Test invalid name."""
with assert_setup_component(0, 'switch'):
assert setup.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'
},
}
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_invalid_switch_does_not_create(self):
"""Test invalid switch."""
with assert_setup_component(0, 'switch'):
assert setup.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template',
'switches': {
'test_template_switch': 'Invalid'
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_no_switches_does_not_create(self):
"""Test if there are no switches no creation."""
with assert_setup_component(0, 'switch'):
assert setup.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template'
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_missing_template_does_not_create(self):
"""Test missing template."""
with assert_setup_component(0, 'switch'):
assert setup.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'
},
}
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_missing_on_does_not_create(self):
"""Test missing on."""
with assert_setup_component(0, 'switch'):
assert setup.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'
},
}
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_missing_off_does_not_create(self):
"""Test missing off."""
with assert_setup_component(0, 'switch'):
assert setup.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'
},
}
}
}
})
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_on_action(self):
"""Test on action."""
assert setup.setup_component(self.hass, 'switch', {
'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'
},
}
}
}
})
self.hass.start()
self.hass.block_till_done()
self.hass.states.set('switch.test_state', STATE_OFF)
self.hass.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.state == STATE_OFF
common.turn_on(self.hass, 'switch.test_template_switch')
self.hass.block_till_done()
assert len(self.calls) == 1
def test_off_action(self):
"""Test off action."""
assert setup.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': 'test.automation'
},
}
}
}
})
self.hass.start()
self.hass.block_till_done()
self.hass.states.set('switch.test_state', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.state == STATE_ON
common.turn_off(self.hass, 'switch.test_template_switch')
self.hass.block_till_done()
assert len(self.calls) == 1