2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Input select component."""
|
2016-01-31 20:40:34 +00:00
|
|
|
# pylint: disable=too-many-public-methods,protected-access
|
|
|
|
import unittest
|
|
|
|
|
2016-09-23 07:12:11 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
|
|
|
from homeassistant.bootstrap import setup_component
|
|
|
|
from homeassistant.components.input_select import (
|
2016-10-14 04:53:47 +00:00
|
|
|
ATTR_OPTIONS, DOMAIN, select_option, select_next, select_previous)
|
2016-01-31 20:40:34 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ICON, ATTR_FRIENDLY_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
class TestInputSelect(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the input select component."""
|
2016-01-31 20:40:34 +00:00
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-01-31 20:40:34 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2016-01-31 20:40:34 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_config(self):
|
|
|
|
"""Test config."""
|
2016-09-23 07:12:11 +00:00
|
|
|
invalid_configs = [
|
|
|
|
None,
|
|
|
|
{},
|
|
|
|
{'name with space': None},
|
|
|
|
# {'bad_options': {'options': None}},
|
|
|
|
{'bad_initial': {
|
|
|
|
'options': [1, 2],
|
|
|
|
'initial': 3,
|
|
|
|
}},
|
|
|
|
]
|
2016-01-31 20:40:34 +00:00
|
|
|
|
2016-09-23 07:12:11 +00:00
|
|
|
for cfg in invalid_configs:
|
|
|
|
self.assertFalse(
|
|
|
|
setup_component(self.hass, DOMAIN, {DOMAIN: cfg}))
|
2016-01-31 20:40:34 +00:00
|
|
|
|
|
|
|
def test_select_option(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test select_option methods."""
|
2016-09-23 07:12:11 +00:00
|
|
|
self.assertTrue(
|
|
|
|
setup_component(self.hass, DOMAIN, {DOMAIN: {
|
2016-01-31 20:40:34 +00:00
|
|
|
'test_1': {
|
|
|
|
'options': [
|
|
|
|
'some option',
|
|
|
|
'another option',
|
|
|
|
],
|
|
|
|
},
|
2016-09-23 07:12:11 +00:00
|
|
|
}}))
|
2016-01-31 20:40:34 +00:00
|
|
|
entity_id = 'input_select.test_1'
|
|
|
|
|
|
|
|
state = self.hass.states.get(entity_id)
|
|
|
|
self.assertEqual('some option', state.state)
|
|
|
|
|
2016-09-23 07:12:11 +00:00
|
|
|
select_option(self.hass, entity_id, 'another option')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-31 20:40:34 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get(entity_id)
|
|
|
|
self.assertEqual('another option', state.state)
|
|
|
|
|
2016-09-23 07:12:11 +00:00
|
|
|
select_option(self.hass, entity_id, 'non existing option')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-31 20:40:34 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get(entity_id)
|
|
|
|
self.assertEqual('another option', state.state)
|
|
|
|
|
2016-10-14 04:53:47 +00:00
|
|
|
def test_select_next(self):
|
|
|
|
"""Test select_next methods."""
|
|
|
|
self.assertTrue(
|
|
|
|
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)
|
|
|
|
self.assertEqual('middle option', state.state)
|
|
|
|
|
|
|
|
select_next(self.hass, entity_id)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get(entity_id)
|
|
|
|
self.assertEqual('last option', state.state)
|
|
|
|
|
|
|
|
select_next(self.hass, entity_id)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get(entity_id)
|
|
|
|
self.assertEqual('first option', state.state)
|
|
|
|
|
|
|
|
def test_select_previous(self):
|
|
|
|
"""Test select_previous methods."""
|
|
|
|
self.assertTrue(
|
|
|
|
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)
|
|
|
|
self.assertEqual('middle option', state.state)
|
|
|
|
|
|
|
|
select_previous(self.hass, entity_id)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get(entity_id)
|
|
|
|
self.assertEqual('first option', state.state)
|
|
|
|
|
|
|
|
select_previous(self.hass, entity_id)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get(entity_id)
|
|
|
|
self.assertEqual('last option', state.state)
|
|
|
|
|
2016-01-31 20:40:34 +00:00
|
|
|
def test_config_options(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test configuration options."""
|
2016-01-31 20:40:34 +00:00
|
|
|
count_start = len(self.hass.states.entity_ids())
|
|
|
|
|
|
|
|
test_2_options = [
|
|
|
|
'Good Option',
|
|
|
|
'Better Option',
|
|
|
|
'Best Option',
|
|
|
|
]
|
|
|
|
|
2016-09-23 07:12:11 +00:00
|
|
|
self.assertTrue(setup_component(self.hass, DOMAIN, {
|
|
|
|
DOMAIN: {
|
2016-01-31 20:40:34 +00:00
|
|
|
'test_1': {
|
|
|
|
'options': [
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
'test_2': {
|
|
|
|
'name': 'Hello World',
|
2016-09-23 07:12:11 +00:00
|
|
|
'icon': 'mdi:work',
|
2016-01-31 20:40:34 +00:00
|
|
|
'options': test_2_options,
|
|
|
|
'initial': 'Better Option',
|
|
|
|
},
|
2016-09-23 07:12:11 +00:00
|
|
|
}
|
2016-01-31 20:40:34 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
self.assertEqual(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')
|
|
|
|
|
|
|
|
self.assertIsNotNone(state_1)
|
|
|
|
self.assertIsNotNone(state_2)
|
|
|
|
|
|
|
|
self.assertEqual('1', state_1.state)
|
|
|
|
self.assertEqual(['1', '2'],
|
2016-09-23 07:12:11 +00:00
|
|
|
state_1.attributes.get(ATTR_OPTIONS))
|
2016-01-31 20:40:34 +00:00
|
|
|
self.assertNotIn(ATTR_ICON, state_1.attributes)
|
|
|
|
|
|
|
|
self.assertEqual('Better Option', state_2.state)
|
|
|
|
self.assertEqual(test_2_options,
|
2016-09-23 07:12:11 +00:00
|
|
|
state_2.attributes.get(ATTR_OPTIONS))
|
2016-01-31 20:40:34 +00:00
|
|
|
self.assertEqual('Hello World',
|
|
|
|
state_2.attributes.get(ATTR_FRIENDLY_NAME))
|
2016-09-23 07:12:11 +00:00
|
|
|
self.assertEqual('mdi:work', state_2.attributes.get(ATTR_ICON))
|