mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
select next and previous of input select (#3839)
This commit is contained in:
parent
4d716cec2b
commit
399a0b470a
@ -31,6 +31,18 @@ SERVICE_SELECT_OPTION_SCHEMA = vol.Schema({
|
|||||||
vol.Required(ATTR_OPTION): cv.string,
|
vol.Required(ATTR_OPTION): cv.string,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
SERVICE_SELECT_NEXT = 'select_next'
|
||||||
|
|
||||||
|
SERVICE_SELECT_NEXT_SCHEMA = vol.Schema({
|
||||||
|
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
||||||
|
})
|
||||||
|
|
||||||
|
SERVICE_SELECT_PREVIOUS = 'select_previous'
|
||||||
|
|
||||||
|
SERVICE_SELECT_PREVIOUS_SCHEMA = vol.Schema({
|
||||||
|
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def _cv_input_select(cfg):
|
def _cv_input_select(cfg):
|
||||||
"""Config validation helper for input select (Voluptuous)."""
|
"""Config validation helper for input select (Voluptuous)."""
|
||||||
@ -53,13 +65,27 @@ CONFIG_SCHEMA = vol.Schema({DOMAIN: {
|
|||||||
|
|
||||||
|
|
||||||
def select_option(hass, entity_id, option):
|
def select_option(hass, entity_id, option):
|
||||||
"""Set input_select to False."""
|
"""Set value of input_select."""
|
||||||
hass.services.call(DOMAIN, SERVICE_SELECT_OPTION, {
|
hass.services.call(DOMAIN, SERVICE_SELECT_OPTION, {
|
||||||
ATTR_ENTITY_ID: entity_id,
|
ATTR_ENTITY_ID: entity_id,
|
||||||
ATTR_OPTION: option,
|
ATTR_OPTION: option,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
def select_next(hass, entity_id):
|
||||||
|
"""Set next value of input_select."""
|
||||||
|
hass.services.call(DOMAIN, SERVICE_SELECT_NEXT, {
|
||||||
|
ATTR_ENTITY_ID: entity_id,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
def select_previous(hass, entity_id):
|
||||||
|
"""Set previous value of input_select."""
|
||||||
|
hass.services.call(DOMAIN, SERVICE_SELECT_PREVIOUS, {
|
||||||
|
ATTR_ENTITY_ID: entity_id,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Setup input select."""
|
"""Setup input select."""
|
||||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||||
@ -77,7 +103,7 @@ def setup(hass, config):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def select_option_service(call):
|
def select_option_service(call):
|
||||||
"""Handle a calls to the input select services."""
|
"""Handle a calls to the input select option service."""
|
||||||
target_inputs = component.extract_from_service(call)
|
target_inputs = component.extract_from_service(call)
|
||||||
|
|
||||||
for input_select in target_inputs:
|
for input_select in target_inputs:
|
||||||
@ -87,6 +113,28 @@ def setup(hass, config):
|
|||||||
select_option_service,
|
select_option_service,
|
||||||
schema=SERVICE_SELECT_OPTION_SCHEMA)
|
schema=SERVICE_SELECT_OPTION_SCHEMA)
|
||||||
|
|
||||||
|
def select_next_service(call):
|
||||||
|
"""Handle a calls to the input select next service."""
|
||||||
|
target_inputs = component.extract_from_service(call)
|
||||||
|
|
||||||
|
for input_select in target_inputs:
|
||||||
|
input_select.offset_index(1)
|
||||||
|
|
||||||
|
hass.services.register(DOMAIN, SERVICE_SELECT_NEXT,
|
||||||
|
select_next_service,
|
||||||
|
schema=SERVICE_SELECT_NEXT_SCHEMA)
|
||||||
|
|
||||||
|
def select_previous_service(call):
|
||||||
|
"""Handle a calls to the input select previous service."""
|
||||||
|
target_inputs = component.extract_from_service(call)
|
||||||
|
|
||||||
|
for input_select in target_inputs:
|
||||||
|
input_select.offset_index(-1)
|
||||||
|
|
||||||
|
hass.services.register(DOMAIN, SERVICE_SELECT_PREVIOUS,
|
||||||
|
select_previous_service,
|
||||||
|
schema=SERVICE_SELECT_PREVIOUS_SCHEMA)
|
||||||
|
|
||||||
component.add_entities(entities)
|
component.add_entities(entities)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
@ -139,3 +187,10 @@ class InputSelect(Entity):
|
|||||||
return
|
return
|
||||||
self._current_option = option
|
self._current_option = option
|
||||||
self.update_ha_state()
|
self.update_ha_state()
|
||||||
|
|
||||||
|
def offset_index(self, offset):
|
||||||
|
"""Offset current index."""
|
||||||
|
current_index = self._options.index(self._current_option)
|
||||||
|
new_index = (current_index + offset) % len(self._options)
|
||||||
|
self._current_option = self._options[new_index]
|
||||||
|
self.update_ha_state()
|
||||||
|
@ -6,7 +6,7 @@ from tests.common import get_test_home_assistant
|
|||||||
|
|
||||||
from homeassistant.bootstrap import setup_component
|
from homeassistant.bootstrap import setup_component
|
||||||
from homeassistant.components.input_select import (
|
from homeassistant.components.input_select import (
|
||||||
ATTR_OPTIONS, DOMAIN, select_option)
|
ATTR_OPTIONS, DOMAIN, select_option, select_next, select_previous)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ICON, ATTR_FRIENDLY_NAME)
|
ATTR_ICON, ATTR_FRIENDLY_NAME)
|
||||||
|
|
||||||
@ -67,6 +67,66 @@ class TestInputSelect(unittest.TestCase):
|
|||||||
state = self.hass.states.get(entity_id)
|
state = self.hass.states.get(entity_id)
|
||||||
self.assertEqual('another option', state.state)
|
self.assertEqual('another option', state.state)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
def test_config_options(self):
|
def test_config_options(self):
|
||||||
"""Test configuration options."""
|
"""Test configuration options."""
|
||||||
count_start = len(self.hass.states.entity_ids())
|
count_start = len(self.hass.states.entity_ids())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user