Add set_options_service for input select (#5630)

pull/5231/merge
Daniel Høyer Iversen 2017-01-30 18:17:57 +01:00 committed by Paulus Schoutsen
parent 1bf3eba603
commit 3a1607500e
2 changed files with 75 additions and 1 deletions

View File

@ -45,6 +45,15 @@ SERVICE_SELECT_PREVIOUS_SCHEMA = vol.Schema({
})
SERVICE_SET_OPTIONS = 'set_options'
SERVICE_SET_OPTIONS_SCHEMA = vol.Schema({
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
vol.Required(ATTR_OPTIONS):
vol.All(cv.ensure_list, vol.Length(min=1), [cv.string]),
})
def _cv_input_select(cfg):
"""Config validation helper for input select (Voluptuous)."""
options = cfg[CONF_OPTIONS]
@ -89,6 +98,14 @@ def select_previous(hass, entity_id):
})
def set_options(hass, entity_id, options):
"""Set options of input_select."""
hass.services.call(DOMAIN, SERVICE_SET_OPTIONS, {
ATTR_ENTITY_ID: entity_id,
ATTR_OPTIONS: options,
})
@asyncio.coroutine
def async_setup(hass, config):
"""Setup input select."""
@ -148,6 +165,20 @@ def async_setup(hass, config):
DOMAIN, SERVICE_SELECT_PREVIOUS, async_select_previous_service,
schema=SERVICE_SELECT_PREVIOUS_SCHEMA)
@asyncio.coroutine
def async_set_options_service(call):
"""Handle a calls to the set options service."""
target_inputs = component.async_extract_from_service(call)
tasks = [input_select.async_set_options(call.data[ATTR_OPTIONS])
for input_select in target_inputs]
if tasks:
yield from asyncio.wait(tasks, loop=hass.loop)
hass.services.async_register(
DOMAIN, SERVICE_SET_OPTIONS, async_set_options_service,
schema=SERVICE_SET_OPTIONS_SCHEMA)
yield from component.async_add_entities(entities)
return True
@ -207,3 +238,10 @@ class InputSelect(Entity):
new_index = (current_index + offset) % len(self._options)
self._current_option = self._options[new_index]
yield from self.async_update_ha_state()
@asyncio.coroutine
def async_set_options(self, options):
"""Set options."""
self._current_option = options[0]
self._options = options
yield from self.async_update_ha_state()

View File

@ -6,7 +6,8 @@ from tests.common import get_test_home_assistant
from homeassistant.bootstrap import setup_component
from homeassistant.components.input_select import (
ATTR_OPTIONS, DOMAIN, select_option, select_next, select_previous)
ATTR_OPTIONS, DOMAIN, SERVICE_SET_OPTIONS,
select_option, select_next, select_previous)
from homeassistant.const import (
ATTR_ICON, ATTR_FRIENDLY_NAME)
@ -175,3 +176,38 @@ class TestInputSelect(unittest.TestCase):
self.assertEqual('Hello World',
state_2.attributes.get(ATTR_FRIENDLY_NAME))
self.assertEqual('mdi:work', state_2.attributes.get(ATTR_ICON))
def test_set_options_service(self):
"""Test set_options service."""
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)
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)
self.assertEqual('test1', state.state)
select_option(self.hass, entity_id, 'first option')
self.hass.block_till_done()
state = self.hass.states.get(entity_id)
self.assertEqual('test1', state.state)
select_option(self.hass, entity_id, 'test2')
self.hass.block_till_done()
state = self.hass.states.get(entity_id)
self.assertEqual('test2', state.state)