From 65be458ce0265663477be6082341e60d2512b745 Mon Sep 17 00:00:00 2001 From: Adam Mills Date: Wed, 7 Nov 2018 03:56:32 -0500 Subject: [PATCH] Update input component tests to async (#18290) --- tests/components/test_input_boolean.py | 168 ++++------- tests/components/test_input_datetime.py | 44 +-- tests/components/test_input_number.py | 273 ++++++++--------- tests/components/test_input_select.py | 386 ++++++++++++------------ tests/components/test_input_text.py | 151 +++++---- 5 files changed, 473 insertions(+), 549 deletions(-) diff --git a/tests/components/test_input_boolean.py b/tests/components/test_input_boolean.py index 9fc9ceaefc1..a77e0a8c010 100644 --- a/tests/components/test_input_boolean.py +++ b/tests/components/test_input_boolean.py @@ -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 diff --git a/tests/components/test_input_datetime.py b/tests/components/test_input_datetime.py index e7f6b50c43d..9649531a8a1 100644 --- a/tests/components/test_input_datetime.py +++ b/tests/components/test_input_datetime.py @@ -1,15 +1,14 @@ """Tests for the Input slider component.""" # pylint: disable=protected-access import asyncio -import unittest import datetime 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_datetime import ( DOMAIN, ATTR_ENTITY_ID, ATTR_DATE, ATTR_TIME, SERVICE_SET_DATETIME) -from tests.common import get_test_home_assistant, mock_restore_cache +from tests.common import mock_restore_cache async def async_set_datetime(hass, entity_id, dt_value): @@ -21,32 +20,19 @@ async def async_set_datetime(hass, entity_id, dt_value): }, blocking=True) -class TestInputDatetime(unittest.TestCase): - """Test the input datetime component.""" - - # pylint: disable=invalid-name - def setUp(self): - """Set up things to be run when tests are started.""" - self.hass = get_test_home_assistant() - - # pylint: disable=invalid-name - def tearDown(self): - """Stop everything that was started.""" - self.hass.stop() - - def test_invalid_configs(self): - """Test config.""" - invalid_configs = [ - None, - {}, - {'name with space': None}, - {'test_no_value': { - 'has_time': False, - 'has_date': False - }}, - ] - for cfg in invalid_configs: - assert not setup_component(self.hass, DOMAIN, {DOMAIN: cfg}) +async def test_invalid_configs(hass): + """Test config.""" + invalid_configs = [ + None, + {}, + {'name with space': None}, + {'test_no_value': { + 'has_time': False, + 'has_date': False + }}, + ] + for cfg in invalid_configs: + assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg}) @asyncio.coroutine diff --git a/tests/components/test_input_number.py b/tests/components/test_input_number.py index 3129b4445c7..354c67b4d1b 100644 --- a/tests/components/test_input_number.py +++ b/tests/components/test_input_number.py @@ -1,7 +1,6 @@ """The tests for the Input number component.""" # pylint: disable=protected-access import asyncio -import unittest from homeassistant.core import CoreState, State, Context from homeassistant.components.input_number import ( @@ -9,9 +8,9 @@ from homeassistant.components.input_number import ( SERVICE_SET_VALUE) from homeassistant.const import ATTR_ENTITY_ID from homeassistant.loader import bind_hass -from homeassistant.setup import setup_component, async_setup_component +from homeassistant.setup import async_setup_component -from tests.common import get_test_home_assistant, mock_restore_cache +from tests.common import mock_restore_cache @bind_hass @@ -20,10 +19,11 @@ def set_value(hass, entity_id, value): This is a legacy helper method. Do not use it for new tests. """ - hass.services.call(DOMAIN, SERVICE_SET_VALUE, { - ATTR_ENTITY_ID: entity_id, - ATTR_VALUE: value, - }) + hass.async_create_task(hass.services.async_call( + DOMAIN, SERVICE_SET_VALUE, { + ATTR_ENTITY_ID: entity_id, + ATTR_VALUE: value, + })) @bind_hass @@ -32,9 +32,10 @@ def increment(hass, entity_id): This is a legacy helper method. Do not use it for new tests. """ - hass.services.call(DOMAIN, SERVICE_INCREMENT, { - ATTR_ENTITY_ID: entity_id - }) + hass.async_create_task(hass.services.async_call( + DOMAIN, SERVICE_INCREMENT, { + ATTR_ENTITY_ID: entity_id + })) @bind_hass @@ -43,152 +44,144 @@ def decrement(hass, entity_id): This is a legacy helper method. Do not use it for new tests. """ - hass.services.call(DOMAIN, SERVICE_DECREMENT, { - ATTR_ENTITY_ID: entity_id - }) + hass.async_create_task(hass.services.async_call( + DOMAIN, SERVICE_DECREMENT, { + ATTR_ENTITY_ID: entity_id + })) -class TestInputNumber(unittest.TestCase): - """Test the input number component.""" +async def test_config(hass): + """Test config.""" + invalid_configs = [ + None, + {}, + {'name with space': None}, + {'test_1': { + 'min': 50, + 'max': 50, + }}, + ] + for cfg in invalid_configs: + assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg}) - # pylint: disable=invalid-name - def setUp(self): - """Set up things to be run when tests are started.""" - self.hass = get_test_home_assistant() - # pylint: disable=invalid-name - def tearDown(self): - """Stop everything that was started.""" - self.hass.stop() +async def test_set_value(hass): + """Test set_value method.""" + assert await async_setup_component(hass, DOMAIN, {DOMAIN: { + 'test_1': { + 'initial': 50, + 'min': 0, + 'max': 100, + }, + }}) + entity_id = 'input_number.test_1' - def test_config(self): - """Test config.""" - invalid_configs = [ - None, - {}, - {'name with space': None}, - {'test_1': { - 'min': 50, - 'max': 50, - }}, - ] - for cfg in invalid_configs: - assert not setup_component(self.hass, DOMAIN, {DOMAIN: cfg}) + state = hass.states.get(entity_id) + assert 50 == float(state.state) - def test_set_value(self): - """Test set_value method.""" - assert setup_component(self.hass, DOMAIN, {DOMAIN: { - 'test_1': { - 'initial': 50, + set_value(hass, entity_id, '30.4') + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 30.4 == float(state.state) + + set_value(hass, entity_id, '70') + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 70 == float(state.state) + + set_value(hass, entity_id, '110') + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 70 == float(state.state) + + +async def test_increment(hass): + """Test increment method.""" + assert await async_setup_component(hass, DOMAIN, {DOMAIN: { + 'test_2': { + 'initial': 50, + 'min': 0, + 'max': 51, + }, + }}) + entity_id = 'input_number.test_2' + + state = hass.states.get(entity_id) + assert 50 == float(state.state) + + increment(hass, entity_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 51 == float(state.state) + + increment(hass, entity_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 51 == float(state.state) + + +async def test_decrement(hass): + """Test decrement method.""" + assert await async_setup_component(hass, DOMAIN, {DOMAIN: { + 'test_3': { + 'initial': 50, + 'min': 49, + 'max': 100, + }, + }}) + entity_id = 'input_number.test_3' + + state = hass.states.get(entity_id) + assert 50 == float(state.state) + + decrement(hass, entity_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 49 == float(state.state) + + decrement(hass, entity_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 49 == float(state.state) + + +async def test_mode(hass): + """Test mode settings.""" + assert await async_setup_component(hass, DOMAIN, {DOMAIN: { + 'test_default_slider': { 'min': 0, 'max': 100, }, - }}) - entity_id = 'input_number.test_1' - - state = self.hass.states.get(entity_id) - assert 50 == float(state.state) - - set_value(self.hass, entity_id, '30.4') - self.hass.block_till_done() - - state = self.hass.states.get(entity_id) - assert 30.4 == float(state.state) - - set_value(self.hass, entity_id, '70') - self.hass.block_till_done() - - state = self.hass.states.get(entity_id) - assert 70 == float(state.state) - - set_value(self.hass, entity_id, '110') - self.hass.block_till_done() - - state = self.hass.states.get(entity_id) - assert 70 == float(state.state) - - def test_increment(self): - """Test increment method.""" - assert setup_component(self.hass, DOMAIN, {DOMAIN: { - 'test_2': { - 'initial': 50, + 'test_explicit_box': { 'min': 0, - 'max': 51, - }, - }}) - entity_id = 'input_number.test_2' - - state = self.hass.states.get(entity_id) - assert 50 == float(state.state) - - increment(self.hass, entity_id) - self.hass.block_till_done() - - state = self.hass.states.get(entity_id) - assert 51 == float(state.state) - - increment(self.hass, entity_id) - self.hass.block_till_done() - - state = self.hass.states.get(entity_id) - assert 51 == float(state.state) - - def test_decrement(self): - """Test decrement method.""" - assert setup_component(self.hass, DOMAIN, {DOMAIN: { - 'test_3': { - 'initial': 50, - 'min': 49, 'max': 100, + 'mode': 'box', + }, + 'test_explicit_slider': { + 'min': 0, + 'max': 100, + 'mode': 'slider', }, }}) - entity_id = 'input_number.test_3' - state = self.hass.states.get(entity_id) - assert 50 == float(state.state) + state = hass.states.get('input_number.test_default_slider') + assert state + assert 'slider' == state.attributes['mode'] - decrement(self.hass, entity_id) - self.hass.block_till_done() + state = hass.states.get('input_number.test_explicit_box') + assert state + assert 'box' == state.attributes['mode'] - state = self.hass.states.get(entity_id) - assert 49 == float(state.state) - - decrement(self.hass, entity_id) - self.hass.block_till_done() - - state = self.hass.states.get(entity_id) - assert 49 == float(state.state) - - def test_mode(self): - """Test mode settings.""" - assert setup_component(self.hass, DOMAIN, {DOMAIN: { - 'test_default_slider': { - 'min': 0, - 'max': 100, - }, - 'test_explicit_box': { - 'min': 0, - 'max': 100, - 'mode': 'box', - }, - 'test_explicit_slider': { - 'min': 0, - 'max': 100, - 'mode': 'slider', - }, - }}) - - state = self.hass.states.get('input_number.test_default_slider') - assert state - assert 'slider' == state.attributes['mode'] - - state = self.hass.states.get('input_number.test_explicit_box') - assert state - assert 'box' == state.attributes['mode'] - - state = self.hass.states.get('input_number.test_explicit_slider') - assert state - assert 'slider' == state.attributes['mode'] + state = hass.states.get('input_number.test_explicit_slider') + assert state + assert 'slider' == state.attributes['mode'] @asyncio.coroutine diff --git a/tests/components/test_input_select.py b/tests/components/test_input_select.py index 684c526cbeb..f37566ffd73 100644 --- a/tests/components/test_input_select.py +++ b/tests/components/test_input_select.py @@ -1,7 +1,6 @@ """The tests for the Input select component.""" # pylint: disable=protected-access import asyncio -import unittest from homeassistant.loader import bind_hass from homeassistant.components.input_select import ( @@ -10,9 +9,9 @@ from homeassistant.components.input_select import ( from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, ATTR_ICON) from homeassistant.core import State, Context -from homeassistant.setup import setup_component, async_setup_component +from homeassistant.setup import async_setup_component -from tests.common import get_test_home_assistant, mock_restore_cache +from tests.common import mock_restore_cache @bind_hass @@ -21,10 +20,11 @@ def select_option(hass, entity_id, option): This is a legacy helper method. Do not use it for new tests. """ - hass.services.call(DOMAIN, SERVICE_SELECT_OPTION, { - ATTR_ENTITY_ID: entity_id, - ATTR_OPTION: option, - }) + hass.async_create_task(hass.services.async_call( + DOMAIN, SERVICE_SELECT_OPTION, { + ATTR_ENTITY_ID: entity_id, + ATTR_OPTION: option, + })) @bind_hass @@ -33,9 +33,10 @@ def select_next(hass, entity_id): This is a legacy helper method. Do not use it for new tests. """ - hass.services.call(DOMAIN, SERVICE_SELECT_NEXT, { - ATTR_ENTITY_ID: entity_id, - }) + hass.async_create_task(hass.services.async_call( + DOMAIN, SERVICE_SELECT_NEXT, { + ATTR_ENTITY_ID: entity_id, + })) @bind_hass @@ -44,205 +45,198 @@ def select_previous(hass, entity_id): This is a legacy helper method. Do not use it for new tests. """ - hass.services.call(DOMAIN, SERVICE_SELECT_PREVIOUS, { - ATTR_ENTITY_ID: entity_id, + hass.async_create_task(hass.services.async_call( + DOMAIN, SERVICE_SELECT_PREVIOUS, { + ATTR_ENTITY_ID: entity_id, + })) + + +async def test_config(hass): + """Test config.""" + invalid_configs = [ + None, + {}, + {'name with space': None}, + # {'bad_options': {'options': None}}, + {'bad_initial': { + 'options': [1, 2], + 'initial': 3, + }}, + ] + + for cfg in invalid_configs: + assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg}) + + +async def test_select_option(hass): + """Test select_option methods.""" + assert await async_setup_component(hass, DOMAIN, {DOMAIN: { + 'test_1': { + 'options': [ + 'some option', + 'another option', + ], + }, + }}) + entity_id = 'input_select.test_1' + + state = hass.states.get(entity_id) + assert 'some option' == state.state + + select_option(hass, entity_id, 'another option') + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 'another option' == state.state + + select_option(hass, entity_id, 'non existing option') + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 'another option' == state.state + + +async def test_select_next(hass): + """Test select_next methods.""" + assert await async_setup_component(hass, DOMAIN, {DOMAIN: { + 'test_1': { + 'options': [ + 'first option', + 'middle option', + 'last option', + ], + 'initial': 'middle option', + }, + }}) + entity_id = 'input_select.test_1' + + state = hass.states.get(entity_id) + assert 'middle option' == state.state + + select_next(hass, entity_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 'last option' == state.state + + select_next(hass, entity_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 'first option' == state.state + + +async def test_select_previous(hass): + """Test select_previous methods.""" + assert await async_setup_component(hass, DOMAIN, {DOMAIN: { + 'test_1': { + 'options': [ + 'first option', + 'middle option', + 'last option', + ], + 'initial': 'middle option', + }, + }}) + entity_id = 'input_select.test_1' + + state = hass.states.get(entity_id) + assert 'middle option' == state.state + + select_previous(hass, entity_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 'first option' == state.state + + select_previous(hass, entity_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 'last option' == state.state + + +async def test_config_options(hass): + """Test configuration options.""" + count_start = len(hass.states.async_entity_ids()) + + test_2_options = [ + 'Good Option', + 'Better Option', + 'Best Option', + ] + + assert await async_setup_component(hass, DOMAIN, { + DOMAIN: { + 'test_1': { + 'options': [ + 1, + 2, + ], + }, + 'test_2': { + 'name': 'Hello World', + 'icon': 'mdi:work', + 'options': test_2_options, + 'initial': 'Better Option', + }, + } }) + assert count_start + 2 == len(hass.states.async_entity_ids()) -class TestInputSelect(unittest.TestCase): - """Test the input select component.""" + state_1 = hass.states.get('input_select.test_1') + state_2 = hass.states.get('input_select.test_2') - # pylint: disable=invalid-name - def setUp(self): - """Set up things to be run when tests are started.""" - self.hass = get_test_home_assistant() + assert state_1 is not None + assert state_2 is not None - # pylint: disable=invalid-name - def tearDown(self): - """Stop everything that was started.""" - self.hass.stop() + assert '1' == state_1.state + assert ['1', '2'] == \ + state_1.attributes.get(ATTR_OPTIONS) + assert ATTR_ICON not in state_1.attributes - def test_config(self): - """Test config.""" - invalid_configs = [ - None, - {}, - {'name with space': None}, - # {'bad_options': {'options': None}}, - {'bad_initial': { - 'options': [1, 2], - 'initial': 3, - }}, - ] + assert 'Better Option' == state_2.state + assert test_2_options == \ + state_2.attributes.get(ATTR_OPTIONS) + assert 'Hello World' == \ + state_2.attributes.get(ATTR_FRIENDLY_NAME) + assert 'mdi:work' == state_2.attributes.get(ATTR_ICON) - for cfg in invalid_configs: - assert not setup_component(self.hass, DOMAIN, {DOMAIN: cfg}) - def test_select_option(self): - """Test select_option methods.""" - assert setup_component(self.hass, DOMAIN, {DOMAIN: { - 'test_1': { - 'options': [ - 'some option', - 'another option', - ], - }, - }}) - entity_id = 'input_select.test_1' +async def test_set_options_service(hass): + """Test set_options service.""" + assert await async_setup_component(hass, DOMAIN, {DOMAIN: { + 'test_1': { + 'options': [ + 'first option', + 'middle option', + 'last option', + ], + 'initial': 'middle option', + }, + }}) + entity_id = 'input_select.test_1' - state = self.hass.states.get(entity_id) - assert 'some option' == state.state + state = hass.states.get(entity_id) + assert 'middle option' == state.state - select_option(self.hass, entity_id, 'another option') - self.hass.block_till_done() + data = {ATTR_OPTIONS: ["test1", "test2"], "entity_id": entity_id} + await hass.services.async_call(DOMAIN, SERVICE_SET_OPTIONS, data) + await hass.async_block_till_done() - state = self.hass.states.get(entity_id) - assert 'another option' == state.state + state = hass.states.get(entity_id) + assert 'test1' == state.state - select_option(self.hass, entity_id, 'non existing option') - self.hass.block_till_done() + select_option(hass, entity_id, 'first option') + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert 'test1' == state.state - state = self.hass.states.get(entity_id) - assert 'another option' == state.state - - def test_select_next(self): - """Test select_next methods.""" - assert setup_component(self.hass, DOMAIN, {DOMAIN: { - 'test_1': { - 'options': [ - 'first option', - 'middle option', - 'last option', - ], - 'initial': 'middle option', - }, - }}) - entity_id = 'input_select.test_1' - - state = self.hass.states.get(entity_id) - assert 'middle option' == state.state - - select_next(self.hass, entity_id) - self.hass.block_till_done() - - state = self.hass.states.get(entity_id) - assert 'last option' == state.state - - select_next(self.hass, entity_id) - self.hass.block_till_done() - - state = self.hass.states.get(entity_id) - assert 'first option' == state.state - - def test_select_previous(self): - """Test select_previous methods.""" - assert setup_component(self.hass, DOMAIN, {DOMAIN: { - 'test_1': { - 'options': [ - 'first option', - 'middle option', - 'last option', - ], - 'initial': 'middle option', - }, - }}) - entity_id = 'input_select.test_1' - - state = self.hass.states.get(entity_id) - assert 'middle option' == state.state - - select_previous(self.hass, entity_id) - self.hass.block_till_done() - - state = self.hass.states.get(entity_id) - assert 'first option' == state.state - - select_previous(self.hass, entity_id) - self.hass.block_till_done() - - state = self.hass.states.get(entity_id) - assert 'last option' == state.state - - def test_config_options(self): - """Test configuration options.""" - count_start = len(self.hass.states.entity_ids()) - - test_2_options = [ - 'Good Option', - 'Better Option', - 'Best Option', - ] - - assert setup_component(self.hass, DOMAIN, { - DOMAIN: { - 'test_1': { - 'options': [ - 1, - 2, - ], - }, - 'test_2': { - 'name': 'Hello World', - 'icon': 'mdi:work', - 'options': test_2_options, - 'initial': 'Better Option', - }, - } - }) - - assert count_start + 2 == len(self.hass.states.entity_ids()) - - state_1 = self.hass.states.get('input_select.test_1') - state_2 = self.hass.states.get('input_select.test_2') - - assert state_1 is not None - assert state_2 is not None - - assert '1' == state_1.state - assert ['1', '2'] == \ - state_1.attributes.get(ATTR_OPTIONS) - assert ATTR_ICON not in state_1.attributes - - assert 'Better Option' == state_2.state - assert test_2_options == \ - state_2.attributes.get(ATTR_OPTIONS) - assert 'Hello World' == \ - state_2.attributes.get(ATTR_FRIENDLY_NAME) - assert 'mdi:work' == state_2.attributes.get(ATTR_ICON) - - def test_set_options_service(self): - """Test set_options service.""" - assert setup_component(self.hass, DOMAIN, {DOMAIN: { - 'test_1': { - 'options': [ - 'first option', - 'middle option', - 'last option', - ], - 'initial': 'middle option', - }, - }}) - entity_id = 'input_select.test_1' - - state = self.hass.states.get(entity_id) - assert 'middle option' == state.state - - data = {ATTR_OPTIONS: ["test1", "test2"], "entity_id": entity_id} - self.hass.services.call(DOMAIN, SERVICE_SET_OPTIONS, data) - self.hass.block_till_done() - - state = self.hass.states.get(entity_id) - assert 'test1' == state.state - - select_option(self.hass, entity_id, 'first option') - self.hass.block_till_done() - state = self.hass.states.get(entity_id) - assert 'test1' == state.state - - select_option(self.hass, entity_id, 'test2') - self.hass.block_till_done() - state = self.hass.states.get(entity_id) - assert 'test2' == state.state + select_option(hass, entity_id, 'test2') + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert 'test2' == state.state @asyncio.coroutine diff --git a/tests/components/test_input_text.py b/tests/components/test_input_text.py index 110a3190b1f..7e8cec6ff80 100644 --- a/tests/components/test_input_text.py +++ b/tests/components/test_input_text.py @@ -1,16 +1,15 @@ """The tests for the Input text component.""" # pylint: disable=protected-access import asyncio -import unittest from homeassistant.components.input_text import ( ATTR_VALUE, DOMAIN, SERVICE_SET_VALUE) from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import CoreState, State, Context from homeassistant.loader import bind_hass -from homeassistant.setup import setup_component, async_setup_component +from homeassistant.setup import async_setup_component -from tests.common import get_test_home_assistant, mock_restore_cache +from tests.common import mock_restore_cache @bind_hass @@ -19,98 +18,88 @@ def set_value(hass, entity_id, value): This is a legacy helper method. Do not use it for new tests. """ - hass.services.call(DOMAIN, SERVICE_SET_VALUE, { - ATTR_ENTITY_ID: entity_id, - ATTR_VALUE: value, - }) + hass.async_create_task(hass.services.async_call( + DOMAIN, SERVICE_SET_VALUE, { + ATTR_ENTITY_ID: entity_id, + ATTR_VALUE: value, + })) -class TestInputText(unittest.TestCase): - """Test the input slider component.""" +async def test_config(hass): + """Test config.""" + invalid_configs = [ + None, + {}, + {'name with space': None}, + {'test_1': { + 'min': 50, + 'max': 50, + }}, + ] + for cfg in invalid_configs: + assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg}) - # pylint: disable=invalid-name - def setUp(self): - """Set up things to be run when tests are started.""" - self.hass = get_test_home_assistant() - # pylint: disable=invalid-name - def tearDown(self): - """Stop everything that was started.""" - self.hass.stop() +async def test_set_value(hass): + """Test set_value method.""" + assert await async_setup_component(hass, DOMAIN, {DOMAIN: { + 'test_1': { + 'initial': 'test', + 'min': 3, + 'max': 10, + }, + }}) + entity_id = 'input_text.test_1' - def test_config(self): - """Test config.""" - invalid_configs = [ - None, - {}, - {'name with space': None}, - {'test_1': { - 'min': 50, - 'max': 50, - }}, - ] - for cfg in invalid_configs: - assert not setup_component(self.hass, DOMAIN, {DOMAIN: cfg}) + state = hass.states.get(entity_id) + assert 'test' == str(state.state) - def test_set_value(self): - """Test set_value method.""" - assert setup_component(self.hass, DOMAIN, {DOMAIN: { - 'test_1': { + set_value(hass, entity_id, 'testing') + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 'testing' == str(state.state) + + set_value(hass, entity_id, 'testing too long') + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert 'testing' == str(state.state) + + +async def test_mode(hass): + """Test mode settings.""" + assert await async_setup_component(hass, DOMAIN, {DOMAIN: { + 'test_default_text': { 'initial': 'test', 'min': 3, 'max': 10, }, + 'test_explicit_text': { + 'initial': 'test', + 'min': 3, + 'max': 10, + 'mode': 'text', + }, + 'test_explicit_password': { + 'initial': 'test', + 'min': 3, + 'max': 10, + 'mode': 'password', + }, }}) - entity_id = 'input_text.test_1' - state = self.hass.states.get(entity_id) - assert 'test' == str(state.state) + state = hass.states.get('input_text.test_default_text') + assert state + assert 'text' == state.attributes['mode'] - set_value(self.hass, entity_id, 'testing') - self.hass.block_till_done() + state = hass.states.get('input_text.test_explicit_text') + assert state + assert 'text' == state.attributes['mode'] - state = self.hass.states.get(entity_id) - assert 'testing' == str(state.state) - - set_value(self.hass, entity_id, 'testing too long') - self.hass.block_till_done() - - state = self.hass.states.get(entity_id) - assert 'testing' == str(state.state) - - def test_mode(self): - """Test mode settings.""" - assert setup_component(self.hass, DOMAIN, {DOMAIN: { - 'test_default_text': { - 'initial': 'test', - 'min': 3, - 'max': 10, - }, - 'test_explicit_text': { - 'initial': 'test', - 'min': 3, - 'max': 10, - 'mode': 'text', - }, - 'test_explicit_password': { - 'initial': 'test', - 'min': 3, - 'max': 10, - 'mode': 'password', - }, - }}) - - state = self.hass.states.get('input_text.test_default_text') - assert state - assert 'text' == state.attributes['mode'] - - state = self.hass.states.get('input_text.test_explicit_text') - assert state - assert 'text' == state.attributes['mode'] - - state = self.hass.states.get('input_text.test_explicit_password') - assert state - assert 'password' == state.attributes['mode'] + state = hass.states.get('input_text.test_explicit_password') + assert state + assert 'password' == state.attributes['mode'] @asyncio.coroutine