New methods for input_select component (#42968)

This adds a `cycle` attribute to select_previous/next, and
select_first and select_last services.

This is quite useful for streamlining using input_select via
automations, such as when they represent a list of states to step
through; if the first option is the dimmest and the last the brightest,
one may not want to accidentally cycle from the first to the last, for
example.

Similarly, being able to directly select the first or last removes
adjustment in related automations.
This commit is contained in:
Lars Marowsky-Brée 2021-01-27 11:17:59 +01:00 committed by GitHub
parent 122a4e03f8
commit 34194da1b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 124 additions and 7 deletions

View file

@ -9,6 +9,8 @@ from homeassistant.components.input_select import (
ATTR_OPTIONS,
CONF_INITIAL,
DOMAIN,
SERVICE_SELECT_FIRST,
SERVICE_SELECT_LAST,
SERVICE_SELECT_NEXT,
SERVICE_SELECT_OPTION,
SERVICE_SELECT_PREVIOUS,
@ -104,6 +106,32 @@ def select_previous(hass, entity_id):
)
@bind_hass
def select_first(hass, entity_id):
"""Set first value of input_select.
This is a legacy helper method. Do not use it for new tests.
"""
hass.async_create_task(
hass.services.async_call(
DOMAIN, SERVICE_SELECT_FIRST, {ATTR_ENTITY_ID: entity_id}
)
)
@bind_hass
def select_last(hass, entity_id):
"""Set last value of input_select.
This is a legacy helper method. Do not use it for new tests.
"""
hass.async_create_task(
hass.services.async_call(
DOMAIN, SERVICE_SELECT_LAST, {ATTR_ENTITY_ID: entity_id}
)
)
async def test_config(hass):
"""Test config."""
invalid_configs = [
@ -207,6 +235,38 @@ async def test_select_previous(hass):
assert "last option" == state.state
async def test_select_first_last(hass):
"""Test select_first and _last 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_first(hass, entity_id)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert "first option" == state.state
select_last(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())