Support Next/Previous for InputSelector (#38378)

* Support Next/Previous for InputSelector

* Update homeassistant/components/google_assistant/trait.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>

* Adjust to match new version of _next_selected

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Joakim Plate 2020-08-06 15:58:26 +02:00 committed by GitHub
parent aaad986002
commit 39e6bca682
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 116 additions and 2 deletions

View file

@ -24,6 +24,7 @@ from homeassistant.components import (
)
from homeassistant.components.climate import const as climate
from homeassistant.components.google_assistant import const, error, helpers, trait
from homeassistant.components.google_assistant.error import SmartHomeError
from homeassistant.components.humidifier import const as humidifier
from homeassistant.config import async_process_ha_core_config
from homeassistant.const import (
@ -1428,6 +1429,87 @@ async def test_inputselector(hass):
assert calls[0].data == {"entity_id": "media_player.living_room", "source": "media"}
@pytest.mark.parametrize(
"sources,source,source_next,source_prev",
[
(["a"], "a", "a", "a"),
(["a", "b"], "a", "b", "b"),
(["a", "b", "c"], "a", "b", "c"),
],
)
async def test_inputselector_nextprev(hass, sources, source, source_next, source_prev):
"""Test input selector trait."""
trt = trait.InputSelectorTrait(
hass,
State(
"media_player.living_room",
media_player.STATE_PLAYING,
attributes={
media_player.ATTR_INPUT_SOURCE_LIST: sources,
media_player.ATTR_INPUT_SOURCE: source,
},
),
BASIC_CONFIG,
)
assert trt.can_execute("action.devices.commands.NextInput", params={})
assert trt.can_execute("action.devices.commands.PreviousInput", params={})
calls = async_mock_service(
hass, media_player.DOMAIN, media_player.SERVICE_SELECT_SOURCE
)
await trt.execute(
"action.devices.commands.NextInput", BASIC_DATA, {}, {},
)
await trt.execute(
"action.devices.commands.PreviousInput", BASIC_DATA, {}, {},
)
assert len(calls) == 2
assert calls[0].data == {
"entity_id": "media_player.living_room",
"source": source_next,
}
assert calls[1].data == {
"entity_id": "media_player.living_room",
"source": source_prev,
}
@pytest.mark.parametrize(
"sources,source", [(None, "a"), (["a", "b"], None), (["a", "b"], "c")]
)
async def test_inputselector_nextprev_invalid(hass, sources, source):
"""Test input selector trait."""
trt = trait.InputSelectorTrait(
hass,
State(
"media_player.living_room",
media_player.STATE_PLAYING,
attributes={
media_player.ATTR_INPUT_SOURCE_LIST: sources,
media_player.ATTR_INPUT_SOURCE: source,
},
),
BASIC_CONFIG,
)
with pytest.raises(SmartHomeError):
await trt.execute(
"action.devices.commands.NextInput", BASIC_DATA, {}, {},
)
with pytest.raises(SmartHomeError):
await trt.execute(
"action.devices.commands.PreviousInput", BASIC_DATA, {}, {},
)
with pytest.raises(SmartHomeError):
await trt.execute(
"action.devices.commands.InvalidCommand", BASIC_DATA, {}, {},
)
async def test_modes_input_select(hass):
"""Test Input Select Mode trait."""
assert helpers.get_google_type(input_select.DOMAIN, None) is not None