Update input component tests to async (#18290)

This commit is contained in:
Adam Mills authored and Paulus Schoutsen committed 2018-11-07 09:56:32 +01:00
1 parent ce069be16e
commit 65be458ce0
5 files changed
+473 -549

No files matched your search

+65 -103
View File
@@ -1,136 +1,98 @@
"""The tests for the input_boolean component."""
# pylint: disable=protected-access
import asyncio
import unittest
import logging
from homeassistant.core import CoreState, State, Context
from homeassistant.setup import setup_component, async_setup_component
from homeassistant.setup import async_setup_component
from homeassistant.components.input_boolean import (
is_on, CONF_INITIAL, DOMAIN)
from homeassistant.const import (
STATE_ON, STATE_OFF, ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, ATTR_ICON,
SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON)
from homeassistant.loader import bind_hass
from tests.common import (
get_test_home_assistant, mock_component, mock_restore_cache)
from tests.common import mock_component, mock_restore_cache
_LOGGER = logging.getLogger(__name__)
@bind_hass
def toggle(hass, entity_id):
"""Set input_boolean to False.
async def test_config(hass):
"""Test config."""
invalid_configs = [
None,
1,
{},
{'name with space': None},
]
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id})
for cfg in invalid_configs:
assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg})
@bind_hass
def turn_on(hass, entity_id):
"""Set input_boolean to True.
async def test_methods(hass):
"""Test is_on, turn_on, turn_off methods."""
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {
'test_1': None,
}})
entity_id = 'input_boolean.test_1'
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id})
assert not is_on(hass, entity_id)
await hass.services.async_call(
DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id})
await hass.async_block_till_done()
assert is_on(hass, entity_id)
await hass.services.async_call(
DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id})
await hass.async_block_till_done()
assert not is_on(hass, entity_id)
await hass.services.async_call(
DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id})
await hass.async_block_till_done()
assert is_on(hass, entity_id)
@bind_hass
def turn_off(hass, entity_id):
"""Set input_boolean to False.
async def test_config_options(hass):
"""Test configuration options."""
count_start = len(hass.states.async_entity_ids())
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id})
_LOGGER.debug('ENTITIES @ start: %s', hass.states.async_entity_ids())
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {
'test_1': None,
'test_2': {
'name': 'Hello World',
'icon': 'mdi:work',
'initial': True,
},
}})
class TestInputBoolean(unittest.TestCase):
"""Test the input boolean module."""
_LOGGER.debug('ENTITIES: %s', hass.states.async_entity_ids())
# pylint: disable=invalid-name
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
assert count_start + 2 == len(hass.states.async_entity_ids())
# pylint: disable=invalid-name
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
state_1 = hass.states.get('input_boolean.test_1')
state_2 = hass.states.get('input_boolean.test_2')
def test_config(self):
"""Test config."""
invalid_configs = [
None,
1,
{},
{'name with space': None},
]
assert state_1 is not None
assert state_2 is not None
for cfg in invalid_configs:
assert not setup_component(self.hass, DOMAIN, {DOMAIN: cfg})
assert STATE_OFF == state_1.state
assert ATTR_ICON not in state_1.attributes
assert ATTR_FRIENDLY_NAME not in state_1.attributes
def test_methods(self):
"""Test is_on, turn_on, turn_off methods."""
assert setup_component(self.hass, DOMAIN, {DOMAIN: {
'test_1': None,
}})
entity_id = 'input_boolean.test_1'
assert not is_on(self.hass, entity_id)
turn_on(self.hass, entity_id)
self.hass.block_till_done()
assert is_on(self.hass, entity_id)
turn_off(self.hass, entity_id)
self.hass.block_till_done()
assert not is_on(self.hass, entity_id)
toggle(self.hass, entity_id)
self.hass.block_till_done()
assert is_on(self.hass, entity_id)
def test_config_options(self):
"""Test configuration options."""
count_start = len(self.hass.states.entity_ids())
_LOGGER.debug('ENTITIES @ start: %s', self.hass.states.entity_ids())
assert setup_component(self.hass, DOMAIN, {DOMAIN: {
'test_1': None,
'test_2': {
'name': 'Hello World',
'icon': 'mdi:work',
'initial': True,
},
}})
_LOGGER.debug('ENTITIES: %s', self.hass.states.entity_ids())
assert count_start + 2 == len(self.hass.states.entity_ids())
state_1 = self.hass.states.get('input_boolean.test_1')
state_2 = self.hass.states.get('input_boolean.test_2')
assert state_1 is not None
assert state_2 is not None
assert STATE_OFF == state_1.state
assert ATTR_ICON not in state_1.attributes
assert ATTR_FRIENDLY_NAME not in state_1.attributes
assert STATE_ON == state_2.state
assert 'Hello World' == \
state_2.attributes.get(ATTR_FRIENDLY_NAME)
assert 'mdi:work' == state_2.attributes.get(ATTR_ICON)
assert STATE_ON == state_2.state
assert 'Hello World' == \
state_2.attributes.get(ATTR_FRIENDLY_NAME)
assert 'mdi:work' == state_2.attributes.get(ATTR_ICON)
@asyncio.coroutine