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

-16
View File
@@ -1,16 +0,0 @@
"""The tests for the everlights component."""
from homeassistant.components.light import everlights
def test_color_rgb_to_int():
"""Test RGB to integer conversion."""
assert everlights.color_rgb_to_int(0x00, 0x00, 0x00) == 0x000000
assert everlights.color_rgb_to_int(0xff, 0xff, 0xff) == 0xffffff
assert everlights.color_rgb_to_int(0x12, 0x34, 0x56) == 0x123456
def test_int_to_rgb():
"""Test integer to RGB conversion."""
assert everlights.color_int_to_rgb(0x000000) == (0x00, 0x00, 0x00)
assert everlights.color_int_to_rgb(0xffffff) == (0xff, 0xff, 0xff)
assert everlights.color_int_to_rgb(0x123456) == (0x12, 0x34, 0x56)
-168
View File
@@ -1,168 +0,0 @@
"""The tests for the litejet component."""
import logging
import unittest
from unittest import mock
from homeassistant import setup
from homeassistant.components import litejet
import homeassistant.components.light as light
from tests.common import get_test_home_assistant
from tests.components.light import common
_LOGGER = logging.getLogger(__name__)
ENTITY_LIGHT = 'light.mock_load_1'
ENTITY_LIGHT_NUMBER = 1
ENTITY_OTHER_LIGHT = 'light.mock_load_2'
ENTITY_OTHER_LIGHT_NUMBER = 2
class TestLiteJetLight(unittest.TestCase):
"""Test the litejet component."""
@mock.patch('pylitejet.LiteJet')
def setup_method(self, method, mock_pylitejet):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.hass.start()
self.load_activated_callbacks = {}
self.load_deactivated_callbacks = {}
def get_load_name(number):
return "Mock Load #"+str(number)
def on_load_activated(number, callback):
self.load_activated_callbacks[number] = callback
def on_load_deactivated(number, callback):
self.load_deactivated_callbacks[number] = callback
self.mock_lj = mock_pylitejet.return_value
self.mock_lj.loads.return_value = range(1, 3)
self.mock_lj.button_switches.return_value = range(0)
self.mock_lj.all_switches.return_value = range(0)
self.mock_lj.scenes.return_value = range(0)
self.mock_lj.get_load_level.return_value = 0
self.mock_lj.get_load_name.side_effect = get_load_name
self.mock_lj.on_load_activated.side_effect = on_load_activated
self.mock_lj.on_load_deactivated.side_effect = on_load_deactivated
assert setup.setup_component(
self.hass,
litejet.DOMAIN,
{
'litejet': {
'port': '/tmp/this_will_be_mocked'
}
})
self.hass.block_till_done()
self.mock_lj.get_load_level.reset_mock()
def light(self):
"""Test for main light entity."""
return self.hass.states.get(ENTITY_LIGHT)
def other_light(self):
"""Test the other light."""
return self.hass.states.get(ENTITY_OTHER_LIGHT)
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_on_brightness(self):
"""Test turning the light on with brightness."""
assert self.light().state == 'off'
assert self.other_light().state == 'off'
assert not light.is_on(self.hass, ENTITY_LIGHT)
common.turn_on(self.hass, ENTITY_LIGHT, brightness=102)
self.hass.block_till_done()
self.mock_lj.activate_load_at.assert_called_with(
ENTITY_LIGHT_NUMBER, 39, 0)
def test_on_off(self):
"""Test turning the light on and off."""
assert self.light().state == 'off'
assert self.other_light().state == 'off'
assert not light.is_on(self.hass, ENTITY_LIGHT)
common.turn_on(self.hass, ENTITY_LIGHT)
self.hass.block_till_done()
self.mock_lj.activate_load.assert_called_with(ENTITY_LIGHT_NUMBER)
common.turn_off(self.hass, ENTITY_LIGHT)
self.hass.block_till_done()
self.mock_lj.deactivate_load.assert_called_with(ENTITY_LIGHT_NUMBER)
def test_activated_event(self):
"""Test handling an event from LiteJet."""
self.mock_lj.get_load_level.return_value = 99
# Light 1
_LOGGER.info(self.load_activated_callbacks[ENTITY_LIGHT_NUMBER])
self.load_activated_callbacks[ENTITY_LIGHT_NUMBER]()
self.hass.block_till_done()
self.mock_lj.get_load_level.assert_called_once_with(
ENTITY_LIGHT_NUMBER)
assert light.is_on(self.hass, ENTITY_LIGHT)
assert not light.is_on(self.hass, ENTITY_OTHER_LIGHT)
assert self.light().state == 'on'
assert self.other_light().state == 'off'
assert self.light().attributes.get(light.ATTR_BRIGHTNESS) == 255
# Light 2
self.mock_lj.get_load_level.return_value = 40
self.mock_lj.get_load_level.reset_mock()
self.load_activated_callbacks[ENTITY_OTHER_LIGHT_NUMBER]()
self.hass.block_till_done()
self.mock_lj.get_load_level.assert_called_once_with(
ENTITY_OTHER_LIGHT_NUMBER)
assert light.is_on(self.hass, ENTITY_OTHER_LIGHT)
assert light.is_on(self.hass, ENTITY_LIGHT)
assert self.light().state == 'on'
assert self.other_light().state == 'on'
assert int(self.other_light().attributes[light.ATTR_BRIGHTNESS]) == 103
def test_deactivated_event(self):
"""Test handling an event from LiteJet."""
# Initial state is on.
self.mock_lj.get_load_level.return_value = 99
self.load_activated_callbacks[ENTITY_OTHER_LIGHT_NUMBER]()
self.hass.block_till_done()
assert light.is_on(self.hass, ENTITY_OTHER_LIGHT)
# Event indicates it is off now.
self.mock_lj.get_load_level.reset_mock()
self.mock_lj.get_load_level.return_value = 0
self.load_deactivated_callbacks[ENTITY_OTHER_LIGHT_NUMBER]()
self.hass.block_till_done()
# (Requesting the level is not strictly needed with a deactivated
# event but the implementation happens to do it. This could be
# changed to an assert_not_called in the future.)
self.mock_lj.get_load_level.assert_called_with(
ENTITY_OTHER_LIGHT_NUMBER)
assert not light.is_on(self.hass, ENTITY_OTHER_LIGHT)
assert not light.is_on(self.hass, ENTITY_LIGHT)
assert self.light().state == 'off'
assert self.other_light().state == 'off'
-642
View File
@@ -1,642 +0,0 @@
"""Test for RFLink light components.
Test setup of RFLink lights component/platform. State tracking and
control of RFLink switch devices.
"""
from homeassistant.components.light import ATTR_BRIGHTNESS
from homeassistant.components.rflink import EVENT_BUTTON_PRESSED
from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_ON, STATE_OFF)
from homeassistant.core import callback, State, CoreState
from tests.common import mock_restore_cache
from tests.components.rflink.test_init import mock_rflink
DOMAIN = 'light'
CONFIG = {
'rflink': {
'port': '/dev/ttyABC0',
'ignore_devices': ['ignore_wildcard_*', 'ignore_light'],
},
DOMAIN: {
'platform': 'rflink',
'devices': {
'protocol_0_0': {
'name': 'test',
'aliases': ['test_alias_0_0'],
},
'dimmable_0_0': {
'name': 'dim_test',
'type': 'dimmable',
},
'switchable_0_0': {
'name': 'switch_test',
'type': 'switchable',
}
},
},
}
async def test_default_setup(hass, monkeypatch):
"""Test all basic functionality of the RFLink switch component."""
# setup mocking rflink module
event_callback, create, protocol, _ = await mock_rflink(
hass, CONFIG, DOMAIN, monkeypatch)
# make sure arguments are passed
assert create.call_args_list[0][1]['ignore']
# test default state of light loaded from config
light_initial = hass.states.get(DOMAIN + '.test')
assert light_initial.state == 'off'
assert light_initial.attributes['assumed_state']
# light should follow state of the hardware device by interpreting
# incoming events for its name and aliases
# mock incoming command event for this device
event_callback({
'id': 'protocol_0_0',
'command': 'on',
})
await hass.async_block_till_done()
light_after_first_command = hass.states.get(DOMAIN + '.test')
assert light_after_first_command.state == 'on'
# also after receiving first command state not longer has to be assumed
assert not light_after_first_command.attributes.get('assumed_state')
# mock incoming command event for this device
event_callback({
'id': 'protocol_0_0',
'command': 'off',
})
await hass.async_block_till_done()
assert hass.states.get(DOMAIN + '.test').state == 'off'
# should respond to group command
event_callback({
'id': 'protocol_0_0',
'command': 'allon',
})
await hass.async_block_till_done()
light_after_first_command = hass.states.get(DOMAIN + '.test')
assert light_after_first_command.state == 'on'
# should respond to group command
event_callback({
'id': 'protocol_0_0',
'command': 'alloff',
})
await hass.async_block_till_done()
assert hass.states.get(DOMAIN + '.test').state == 'off'
# test following aliases
# mock incoming command event for this device alias
event_callback({
'id': 'test_alias_0_0',
'command': 'on',
})
await hass.async_block_till_done()
assert hass.states.get(DOMAIN + '.test').state == 'on'
# test event for new unconfigured sensor
event_callback({
'id': 'protocol2_0_1',
'command': 'on',
})
await hass.async_block_till_done()
assert hass.states.get(DOMAIN + '.protocol2_0_1').state == 'on'
# test changing state from HA propagates to RFLink
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: DOMAIN + '.test'}))
await hass.async_block_till_done()
assert hass.states.get(DOMAIN + '.test').state == 'off'
assert protocol.send_command_ack.call_args_list[0][0][0] == 'protocol_0_0'
assert protocol.send_command_ack.call_args_list[0][0][1] == 'off'
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_ON,
{ATTR_ENTITY_ID: DOMAIN + '.test'}))
await hass.async_block_till_done()
assert hass.states.get(DOMAIN + '.test').state == 'on'
assert protocol.send_command_ack.call_args_list[1][0][1] == 'on'
# protocols supporting dimming and on/off should create hybrid light entity
event_callback({
'id': 'newkaku_0_1',
'command': 'off',
})
await hass.async_block_till_done()
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_ON,
{ATTR_ENTITY_ID: DOMAIN + '.newkaku_0_1'}))
await hass.async_block_till_done()
# dimmable should send highest dim level when turning on
assert protocol.send_command_ack.call_args_list[2][0][1] == '15'
# and send on command for fallback
assert protocol.send_command_ack.call_args_list[3][0][1] == 'on'
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_ON,
{
ATTR_ENTITY_ID: DOMAIN + '.newkaku_0_1',
ATTR_BRIGHTNESS: 128,
}))
await hass.async_block_till_done()
assert protocol.send_command_ack.call_args_list[4][0][1] == '7'
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_ON,
{
ATTR_ENTITY_ID: DOMAIN + '.dim_test',
ATTR_BRIGHTNESS: 128,
}))
await hass.async_block_till_done()
assert protocol.send_command_ack.call_args_list[5][0][1] == '7'
async def test_firing_bus_event(hass, monkeypatch):
"""Incoming RFLink command events should be put on the HA event bus."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'devices': {
'protocol_0_0': {
'name': 'test',
'aliases': ['test_alias_0_0'],
'fire_event': True,
},
},
},
}
# setup mocking rflink module
event_callback, _, _, _ = await mock_rflink(
hass, config, DOMAIN, monkeypatch)
calls = []
@callback
def listener(event):
calls.append(event)
hass.bus.async_listen_once(EVENT_BUTTON_PRESSED, listener)
# test event for new unconfigured sensor
event_callback({
'id': 'protocol_0_0',
'command': 'off',
})
await hass.async_block_till_done()
assert calls[0].data == {'state': 'off', 'entity_id': DOMAIN + '.test'}
async def test_signal_repetitions(hass, monkeypatch):
"""Command should be sent amount of configured repetitions."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'device_defaults': {
'signal_repetitions': 3,
},
'devices': {
'protocol_0_0': {
'name': 'test',
'signal_repetitions': 2,
},
'protocol_0_1': {
'name': 'test1',
},
'newkaku_0_1': {
'type': 'hybrid',
}
},
},
}
# setup mocking rflink module
event_callback, _, protocol, _ = await mock_rflink(
hass, config, DOMAIN, monkeypatch)
# test if signal repetition is performed according to configuration
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: DOMAIN + '.test'}))
# wait for commands and repetitions to finish
await hass.async_block_till_done()
assert protocol.send_command_ack.call_count == 2
# test if default apply to configured devices
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: DOMAIN + '.test1'}))
# wait for commands and repetitions to finish
await hass.async_block_till_done()
assert protocol.send_command_ack.call_count == 5
# test if device defaults apply to newly created devices
event_callback({
'id': 'protocol_0_2',
'command': 'off',
})
# make sure entity is created before setting state
await hass.async_block_till_done()
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: DOMAIN + '.protocol_0_2'}))
# wait for commands and repetitions to finish
await hass.async_block_till_done()
assert protocol.send_command_ack.call_count == 8
async def test_signal_repetitions_alternation(hass, monkeypatch):
"""Simultaneously switching entities must alternate repetitions."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'devices': {
'protocol_0_0': {
'name': 'test',
'signal_repetitions': 2,
},
'protocol_0_1': {
'name': 'test1',
'signal_repetitions': 2,
},
},
},
}
# setup mocking rflink module
_, _, protocol, _ = await mock_rflink(
hass, config, DOMAIN, monkeypatch)
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: DOMAIN + '.test'}))
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: DOMAIN + '.test1'}))
await hass.async_block_till_done()
assert protocol.send_command_ack.call_args_list[0][0][0] == 'protocol_0_0'
assert protocol.send_command_ack.call_args_list[1][0][0] == 'protocol_0_1'
assert protocol.send_command_ack.call_args_list[2][0][0] == 'protocol_0_0'
assert protocol.send_command_ack.call_args_list[3][0][0] == 'protocol_0_1'
async def test_signal_repetitions_cancelling(hass, monkeypatch):
"""Cancel outstanding repetitions when state changed."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'devices': {
'protocol_0_0': {
'name': 'test',
'signal_repetitions': 3,
},
},
},
}
# setup mocking rflink module
_, _, protocol, _ = await mock_rflink(
hass, config, DOMAIN, monkeypatch)
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: DOMAIN + '.test'}))
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_ON,
{ATTR_ENTITY_ID: DOMAIN + '.test'}))
await hass.async_block_till_done()
assert protocol.send_command_ack.call_args_list[0][0][1] == 'on'
assert protocol.send_command_ack.call_args_list[1][0][1] == 'off'
assert protocol.send_command_ack.call_args_list[2][0][1] == 'off'
assert protocol.send_command_ack.call_args_list[3][0][1] == 'off'
async def test_type_toggle(hass, monkeypatch):
"""Test toggle type lights (on/on)."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'devices': {
'toggle_0_0': {
'name': 'toggle_test',
'type': 'toggle',
},
},
},
}
# setup mocking rflink module
event_callback, _, _, _ = await mock_rflink(
hass, config, DOMAIN, monkeypatch)
# default value = 'off'
assert hass.states.get(DOMAIN + '.toggle_test').state == 'off'
# test sending 'on' command, must set state = 'on'
event_callback({
'id': 'toggle_0_0',
'command': 'on',
})
await hass.async_block_till_done()
assert hass.states.get(DOMAIN + '.toggle_test').state == 'on'
# test sending 'on' command again, must set state = 'off'
event_callback({
'id': 'toggle_0_0',
'command': 'on',
})
await hass.async_block_till_done()
assert hass.states.get(DOMAIN + '.toggle_test').state == 'off'
# test async_turn_off, must set state = 'on' ('off' + toggle)
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: DOMAIN + '.toggle_test'}))
await hass.async_block_till_done()
assert hass.states.get(DOMAIN + '.toggle_test').state == 'on'
# test async_turn_on, must set state = 'off' (yes, sounds crazy)
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_ON,
{ATTR_ENTITY_ID: DOMAIN + '.toggle_test'}))
await hass.async_block_till_done()
assert hass.states.get(DOMAIN + '.toggle_test').state == 'off'
async def test_group_alias(hass, monkeypatch):
"""Group aliases should only respond to group commands (allon/alloff)."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'devices': {
'protocol_0_0': {
'name': 'test',
'group_aliases': ['test_group_0_0'],
},
},
},
}
# setup mocking rflink module
event_callback, _, _, _ = await mock_rflink(
hass, config, DOMAIN, monkeypatch)
assert hass.states.get(DOMAIN + '.test').state == 'off'
# test sending group command to group alias
event_callback({
'id': 'test_group_0_0',
'command': 'allon',
})
await hass.async_block_till_done()
assert hass.states.get(DOMAIN + '.test').state == 'on'
# test sending group command to group alias
event_callback({
'id': 'test_group_0_0',
'command': 'off',
})
await hass.async_block_till_done()
assert hass.states.get(DOMAIN + '.test').state == 'on'
async def test_nogroup_alias(hass, monkeypatch):
"""Non group aliases should not respond to group commands."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'devices': {
'protocol_0_0': {
'name': 'test',
'nogroup_aliases': ['test_nogroup_0_0'],
},
},
},
}
# setup mocking rflink module
event_callback, _, _, _ = await mock_rflink(
hass, config, DOMAIN, monkeypatch)
assert hass.states.get(DOMAIN + '.test').state == 'off'
# test sending group command to nogroup alias
event_callback({
'id': 'test_nogroup_0_0',
'command': 'allon',
})
await hass.async_block_till_done()
# should not affect state
assert hass.states.get(DOMAIN + '.test').state == 'off'
# test sending group command to nogroup alias
event_callback({
'id': 'test_nogroup_0_0',
'command': 'on',
})
await hass.async_block_till_done()
# should affect state
assert hass.states.get(DOMAIN + '.test').state == 'on'
async def test_nogroup_device_id(hass, monkeypatch):
"""Device id that do not respond to group commands (allon/alloff)."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'devices': {
'test_nogroup_0_0': {
'name': 'test',
'group': False,
},
},
},
}
# setup mocking rflink module
event_callback, _, _, _ = await mock_rflink(
hass, config, DOMAIN, monkeypatch)
assert hass.states.get(DOMAIN + '.test').state == 'off'
# test sending group command to nogroup
event_callback({
'id': 'test_nogroup_0_0',
'command': 'allon',
})
await hass.async_block_till_done()
# should not affect state
assert hass.states.get(DOMAIN + '.test').state == 'off'
# test sending group command to nogroup
event_callback({
'id': 'test_nogroup_0_0',
'command': 'on',
})
await hass.async_block_till_done()
# should affect state
assert hass.states.get(DOMAIN + '.test').state == 'on'
async def test_disable_automatic_add(hass, monkeypatch):
"""If disabled new devices should not be automatically added."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'automatic_add': False,
},
}
# setup mocking rflink module
event_callback, _, _, _ = await mock_rflink(
hass, config, DOMAIN, monkeypatch)
# test event for new unconfigured sensor
event_callback({
'id': 'protocol_0_0',
'command': 'off',
})
await hass.async_block_till_done()
# make sure new device is not added
assert not hass.states.get(DOMAIN + '.protocol_0_0')
async def test_restore_state(hass, monkeypatch):
"""Ensure states are restored on startup."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'devices': {
'NewKaku_12345678_0': {
'name': 'l1',
'type': 'hybrid',
},
'test_restore_2': {
'name': 'l2',
},
'test_restore_3': {
'name': 'l3',
},
'test_restore_4': {
'name': 'l4',
'type': 'dimmable',
},
'test_restore_5': {
'name': 'l5',
'type': 'dimmable',
},
},
},
}
mock_restore_cache(hass, (
State(DOMAIN + '.l1', STATE_ON, {ATTR_BRIGHTNESS: "123", }),
State(DOMAIN + '.l2', STATE_ON, {ATTR_BRIGHTNESS: "321", }),
State(DOMAIN + '.l3', STATE_OFF, ),
State(DOMAIN + '.l5', STATE_ON, {ATTR_BRIGHTNESS: "222", }),
))
hass.state = CoreState.starting
# setup mocking rflink module
_, _, _, _ = await mock_rflink(hass, config, DOMAIN, monkeypatch)
# hybrid light must restore brightness
state = hass.states.get(DOMAIN + '.l1')
assert state
assert state.state == STATE_ON
assert state.attributes[ATTR_BRIGHTNESS] == 123
# normal light do NOT must restore brightness
state = hass.states.get(DOMAIN + '.l2')
assert state
assert state.state == STATE_ON
assert not state.attributes.get(ATTR_BRIGHTNESS)
# OFF state also restores (or not)
state = hass.states.get(DOMAIN + '.l3')
assert state
assert state.state == STATE_OFF
# not cached light must default values
state = hass.states.get(DOMAIN + '.l4')
assert state
assert state.state == STATE_OFF
assert state.attributes[ATTR_BRIGHTNESS] == 255
assert state.attributes['assumed_state']
# test coverage for dimmable light
state = hass.states.get(DOMAIN + '.l5')
assert state
assert state.state == STATE_ON
assert state.attributes[ATTR_BRIGHTNESS] == 222
-722
View File
@@ -1,722 +0,0 @@
"""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'