Update input component tests to async (#18290)
This commit is contained in:
parent
ce069be16e
commit
65be458ce0
5 changed files with 473 additions and 549 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue